Skip to content

Commit

Permalink
help me jacob :((((((((((
Browse files Browse the repository at this point in the history
  • Loading branch information
vyruss-zz committed Nov 24, 2020
1 parent 7544e38 commit f904ef1
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,108 @@
import org.springframework.web.bind.annotation.RestController;

import com.staging.incubator.models.Element;
import com.staging.incubator.models.ElemixerParent;
import com.staging.incubator.service.ElementService;

@RestController
@RequestMapping("/elements")
public class ElementController {
@Autowired
private ElementService eServ;

private List<Element> eleList = new ArrayList<Element>();

public ElementController(ElementService eServ) {
this.eServ=eServ;
this.eServ = eServ;
}

@GetMapping("/")
public List<Element> getAllElements() {
// return eleList;
return eServ.getAll();
}

@PostMapping("/create")
public Element createNewElement(@RequestBody Element e) {
System.out.println(e);
System.out.println(e.getParentOne());
System.out.println(e.getParentTwo());
// System.out.println(e.getParentOne().getParentOne());

// eleList.add(e);
// return e;

return eServ.create(e);
}

@GetMapping("/{id}")
public Element getElementById(@PathVariable("id") int id) {
return eServ.getById(id);
}

@GetMapping("/{id}/ancestors")
@GetMapping("{id}/parents")
public List<Element> getParentsById(@PathVariable("id") int id) {
//Element e = eServ.getById(id);
List<Element> list = new ArrayList<Element>();
/*list.add(e);
list.add(e.getParentOne().getParentOne());
list.add(e.getParentTwo().getParentTwo());*/

for(Element ele: eleList) {
if(ele.getElementId() == id) list.add(ele);
break;
}

list.add(list.get(0).getParentOne());
list.add(list.get(0).getParentTwo());

return list;
}

@PostMapping("/save")
public List<Element> saveAll() {



return eServ.storeHierarchy(eleList);
}

@GetMapping("/{id}/ancestors")
public List<Element> getAllAncestors(@PathVariable("id") int id) {
ArrayList<Element> list = new ArrayList<>();
Element current = eServ.getById(id);
Element base = current;
list.add(current);
while(current.getParentOne()!=null && current.getParentTwo() != null) {
list.add(current.getParentOne());
list.add(current.getParentTwo());
current = current.getParentOne();
/*int badThingsHappenedCounter = 0;
while (current.getParentOne() != null && current.getParentTwo() != null) {
System.out.println(current);
System.out.println(current.getParentOne());
System.out.println(current.getParentTwo());
list.add(current.getParentOne().getParentOne());
list.add(current.getParentTwo().getParentTwo());
current = current.getParentOne().getParentOne();
badThingsHappenedCounter++;
if (badThingsHappenedCounter >= 1000)
break;
}
badThingsHappenedCounter=0;
current = base;
while(current.getParentOne()!=null && current.getParentTwo() != null) {
if(!list.contains(current.getParentOne()))
list.add(current.getParentOne());
if(!list.contains(current.getParentTwo()));
list.add(current.getParentTwo());
current = current.getParentTwo();
}
while (current.getParentOne() != null && current.getParentTwo() != null) {
System.out.println(current);
System.out.println(current);
System.out.println(current.getParentOne());
System.out.println(current.getParentTwo());
if (!list.contains(current.getParentOne()))
list.add(current.getParentOne().getParentOne());
if (!list.contains(current.getParentTwo()));
list.add(current.getParentTwo().getParentTwo());
current = current.getParentTwo().getParentTwo();
badThingsHappenedCounter++;
if (badThingsHappenedCounter >= 1000)
break;
}*/

return list;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.staging.incubator.models;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@EqualsAndHashCode
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Combinator implements Serializable {

@Column(name = "parent_one_id")
private int parentOneId;
//
@Column(name = "parent_two_id")
private int parentTwoId;

}
58 changes: 46 additions & 12 deletions incubator/src/main/java/com/staging/incubator/models/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,67 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonBackReference;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="elements_table")
@Table(name = "elements_table")
@EqualsAndHashCode(exclude = {"parentOne", "parentTwo"})
@IdClass(Combinator.class)
public class Element {


// @EmbeddedId
// @JoinColumns({ @JoinColumn(name = "parent_one_id", referencedColumnName="parent_one_id"),
// @JoinColumn(name = "parent_two_id", referencedColumnName="parent_two_id") })
// private Combinator combi;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="element_id")
private int elementId;
@Column(name="description", nullable=false)
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "element_id")
private Integer elementId;

@Column(name = "description", nullable = false)
private String description;
@Column(name="color", nullable=false,length=7)
@Column(name = "color", nullable = false, length = 7)
private String color;
@Column(name="parent_one")

@Id
@OneToOne(mappedBy = "parent_one_id")
@MapsId("parentOneId")
@JoinColumn(name = "parent_one_id")
private int parentOneId;

@Id
@OneToOne(mappedBy = "parent_two_id")
@MapsId("parentTwoId")
@JoinColumn(name = "parent_two_id")
private int parentTwoId;

@OneToOne
@JoinColumn(name = "parentOneId", nullable = true)
@JsonBackReference(value = "parentOne")
private Element parentOne;
@Column(name = "parent_two")
@OneToOne
@JoinColumn(name = "parentTwoId", nullable = true)
@JsonBackReference(value = "parentTwo")
private Element parentTwo;



@Override
public String toString() {
return "Element [elementId=" + elementId + ", description=" + description + ", color=" + color + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.staging.incubator.models;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonManagedReference;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table(name="parent_table")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ElemixerParent {

@Id
@Column(name="combination_id")
private int combinationId;

// @OneToMany(mappedBy="parentOne")
// @JoinColumn(name="element_id")
// @JsonManagedReference(value="parentOne")
// private Element parentOne;
//
// @OneToOne(mappedBy="parentTwo")
// @JoinColumn(name="element_id")
// @JsonManagedReference(value="parentTwo")
// private Element parentTwo;
//
// @OneToOne(mappedBy="elementId")
// private Element child;

/*@OneToOne(mappedBy="")
@JoinColumn(name="")
private List<Element> family;*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.staging.incubator.models.Element;

@Repository
@Transactional
public interface ElementRepository extends CrudRepository<Element, Integer>{
public List<Element> findAll();
public Element save(Element e);
public Element findByElementId(int id);
public List<Element> findAllByElementId(int id);
public List<Element> save(List<Element> list);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public Element getById(int id) {
return eRep.findByElementId(id);
}

public List<Element> storeHierarchy(List<Element> list) {
for(Element ele: list) {
eRep.save(ele);
}
return eRep.findAll();
}
}
1 change: 1 addition & 0 deletions incubator/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ spring:
show-sql: false
properties:
hibernate:
ddl-auto: update
jdbc:
lob:
non_contextual_creation: true

0 comments on commit f904ef1

Please sign in to comment.