contexts: simple primary keys

This commit is contained in:
Michele Artini 2022-06-24 15:26:47 +02:00
parent 154b27b0bb
commit a36745ab8b
17 changed files with 66 additions and 506 deletions

View File

@ -51,7 +51,7 @@ public class ContextImporter {
private void saveContext(final Node node) {
final Context ctx = new Context();
setCommonInfo(ctx, node);
setCommonInfo(ctx, null, node);
ctx.setType(node.valueOf("@type"));
contextRepository.save(ctx);
@ -62,57 +62,47 @@ public class ContextImporter {
private void saveCategory(final Node node, final String ctxId) {
final Category cat = new Category();
setCommonInfo(cat, node);
cat.setContextId(ctxId);
setCommonInfo(cat, ctxId, node);
categoryRepository.save(cat);
log.info("- Saved category: " + cat.getId());
node.selectNodes("./concept").forEach(n -> saveConceptLvl0(n, ctxId, cat.getId()));
node.selectNodes("./concept").forEach(n -> saveConceptLvl0(n, cat.getId()));
}
private void saveConceptLvl0(final Node node, final String ctxId, final String catId) {
private void saveConceptLvl0(final Node node, final String parent) {
final ConceptLevel0 c0 = new ConceptLevel0();
setCommonInfo(c0, node);
c0.setContextId(ctxId);
c0.setCategoryId(catId);
setCommonInfo(c0, parent, node);
conceptLevel0Repository.save(c0);
log.info("-- Saved concept 0: " + c0.getId());
node.selectNodes("./concept").forEach(n -> saveConceptLvl1(n, ctxId, catId, c0.getId()));
node.selectNodes("./concept").forEach(n -> saveConceptLvl1(n, c0.getId()));
}
private void saveConceptLvl1(final Node node, final String ctxId, final String catId, final String c0) {
private void saveConceptLvl1(final Node node, final String parent) {
final ConceptLevel1 c1 = new ConceptLevel1();
setCommonInfo(c1, node);
c1.setContextId(ctxId);
c1.setCategoryId(catId);
c1.setConceptLevel0Id(c0);
setCommonInfo(c1, parent, node);
conceptLevel1Repository.save(c1);
log.info("--- Saved concept 1: " + c1.getId());
node.selectNodes("./concept").forEach(n -> saveConceptLvl2(n, ctxId, catId, c0, c1.getId()));
node.selectNodes("./concept").forEach(n -> saveConceptLvl2(n, c1.getId()));
}
private void saveConceptLvl2(final Node node, final String ctxId, final String catId, final String c0, final String c1) {
private void saveConceptLvl2(final Node node, final String parent) {
final ConceptLevel2 c2 = new ConceptLevel2();
setCommonInfo(c2, node);
c2.setContextId(ctxId);
c2.setCategoryId(catId);
c2.setConceptLevel0Id(c0);
c2.setConceptLevel1Id(c1);
setCommonInfo(c2, parent, node);
conceptLevel2Repository.save(c2);
log.info("---- Saved concept 2: " + c2.getId());
}
private void setCommonInfo(final CtxInfo o, final Node n) {
private void setCommonInfo(final CtxInfo o, final String parent, final Node n) {
o.setId(n.valueOf("@id"));
o.setLabel(n.valueOf("@label"));
if (o instanceof CtxInfoWithClaim) {
((CtxInfoWithClaim) o).setParent(parent);
((CtxInfoWithClaim) o).setClaim(BooleanUtils.toBoolean(n.valueOf("@claim")));
}
o.setParameters(n.selectNodes("./param")

View File

@ -63,7 +63,7 @@ public class ContextRestController {
@GetMapping("/{ctxId}/categories")
public Iterable<Category> listCategories(@PathVariable final String ctxId) {
return categoryRepository.findByContextIdOrderById(ctxId);
return categoryRepository.findByParentOrderById(ctxId);
}
@PostMapping(value = "/load", consumes = "text/plain")

View File

@ -1,42 +1,12 @@
package eu.dnetlib.is.context.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name = "context_categories")
@IdClass(CategoryPK.class)
public class Category extends CtxInfoWithClaim {
private static final long serialVersionUID = -1847175903793410585L;
@Id
@Column(name = "id")
private String id;
@Id
@Column(name = "ctx_id")
private String contextId;
@Override
public String getId() {
return id;
}
@Override
public void setId(final String id) {
this.id = id;
}
public String getContextId() {
return contextId;
}
public void setContextId(final String contextId) {
this.contextId = contextId;
}
}

View File

@ -1,48 +0,0 @@
package eu.dnetlib.is.context.model;
import java.io.Serializable;
import java.util.Objects;
public class CategoryPK implements Serializable {
private static final long serialVersionUID = -2764222393730200265L;
private String id;
private String contextId;
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public String getContextId() {
return contextId;
}
public void setContextId(final String contextId) {
this.contextId = contextId;
}
@Override
public int hashCode() {
return Objects.hash(contextId, id);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) { return true; }
if (!(obj instanceof CategoryPK)) { return false; }
final CategoryPK other = (CategoryPK) obj;
return Objects.equals(contextId, other.contextId) && Objects.equals(id, other.id);
}
@Override
public String toString() {
return String.format("CategoryPK [id=%s, contextId=%s]", id, contextId);
}
}

View File

@ -1,53 +1,12 @@
package eu.dnetlib.is.context.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name = "context_cat_concepts_lvl_0")
@IdClass(ConceptLevel0PK.class)
public class ConceptLevel0 extends CtxInfoWithClaim {
private static final long serialVersionUID = -4775331902088912839L;
@Id
@Column(name = "id")
private String id;
@Id
@Column(name = "ctx_id")
private String contextId;
@Id
@Column(name = "cat_id")
private String categoryId;
@Override
public String getId() {
return id;
}
@Override
public void setId(final String id) {
this.id = id;
}
public String getContextId() {
return contextId;
}
public void setContextId(final String contextId) {
this.contextId = contextId;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(final String categoryId) {
this.categoryId = categoryId;
}
}

View File

@ -1,57 +0,0 @@
package eu.dnetlib.is.context.model;
import java.io.Serializable;
import java.util.Objects;
public class ConceptLevel0PK implements Serializable {
private static final long serialVersionUID = -924956037443869537L;
private String id;
private String contextId;
private String categoryId;
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public String getContextId() {
return contextId;
}
public void setContextId(final String contextId) {
this.contextId = contextId;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(final String categoryId) {
this.categoryId = categoryId;
}
@Override
public int hashCode() {
return Objects.hash(categoryId, contextId, id);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) { return true; }
if (!(obj instanceof ConceptLevel0PK)) { return false; }
final ConceptLevel0PK other = (ConceptLevel0PK) obj;
return Objects.equals(categoryId, other.categoryId) && Objects.equals(contextId, other.contextId) && Objects.equals(id, other.id);
}
@Override
public String toString() {
return String.format("ConceptLevel0PK [id=%s, contextId=%s, categoryId=%s]", id, contextId, categoryId);
}
}

View File

@ -1,66 +1,12 @@
package eu.dnetlib.is.context.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name = "context_cat_concepts_lvl_1")
@IdClass(ConceptLevel1PK.class)
public class ConceptLevel1 extends CtxInfoWithClaim {
private static final long serialVersionUID = -759398689766245784L;
@Id
@Column(name = "id")
private String id;
@Id
@Column(name = "ctx_id")
private String contextId;
@Id
@Column(name = "cat_id")
private String categoryId;
@Id
@Column(name = "c0_id")
private String conceptLevel0Id;
@Override
public String getId() {
return id;
}
@Override
public void setId(final String id) {
this.id = id;
}
public String getContextId() {
return contextId;
}
public void setContextId(final String contextId) {
this.contextId = contextId;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(final String categoryId) {
this.categoryId = categoryId;
}
public String getConceptLevel0Id() {
return conceptLevel0Id;
}
public void setConceptLevel0Id(final String conceptLevel0Id) {
this.conceptLevel0Id = conceptLevel0Id;
};
}

View File

@ -1,68 +0,0 @@
package eu.dnetlib.is.context.model;
import java.io.Serializable;
import java.util.Objects;
public class ConceptLevel1PK implements Serializable {
private static final long serialVersionUID = -7993480292203819163L;
private String id;
private String contextId;
private String categoryId;
private String conceptLevel0Id;
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public String getContextId() {
return contextId;
}
public void setContextId(final String contextId) {
this.contextId = contextId;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(final String categoryId) {
this.categoryId = categoryId;
}
public String getConceptLevel0Id() {
return conceptLevel0Id;
}
public void setConceptLevel0Id(final String conceptLevel0Id) {
this.conceptLevel0Id = conceptLevel0Id;
}
@Override
public int hashCode() {
return Objects.hash(categoryId, conceptLevel0Id, contextId, id);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) { return true; }
if (!(obj instanceof ConceptLevel1PK)) { return false; }
final ConceptLevel1PK other = (ConceptLevel1PK) obj;
return Objects.equals(categoryId, other.categoryId) && Objects.equals(conceptLevel0Id, other.conceptLevel0Id)
&& Objects.equals(contextId, other.contextId) && Objects.equals(id, other.id);
}
@Override
public String toString() {
return String.format("ConceptLevel1PK [id=%s, contextId=%s, categoryId=%s, conceptLevel0Id=%s]", id, contextId, categoryId, conceptLevel0Id);
}
}

View File

@ -1,77 +1,12 @@
package eu.dnetlib.is.context.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name = "context_cat_concepts_lvl_2")
@IdClass(ConceptLevel2PK.class)
public class ConceptLevel2 extends CtxInfoWithClaim {
private static final long serialVersionUID = 906131339592862096L;
@Id
@Column(name = "id")
private String id;
@Id
@Column(name = "ctx_id")
private String contextId;
@Id
@Column(name = "cat_id")
private String categoryId;
@Id
@Column(name = "c0_id")
private String conceptLevel0Id;
@Id
@Column(name = "c1_id")
private String conceptLevel1Id;
@Override
public String getId() {
return id;
}
@Override
public void setId(final String id) {
this.id = id;
}
public String getContextId() {
return contextId;
}
public void setContextId(final String contextId) {
this.contextId = contextId;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(final String categoryId) {
this.categoryId = categoryId;
}
public String getConceptLevel0Id() {
return conceptLevel0Id;
}
public void setConceptLevel0Id(final String conceptLevel0Id) {
this.conceptLevel0Id = conceptLevel0Id;
}
public String getConceptLevel1Id() {
return conceptLevel1Id;
}
public void setConceptLevel1Id(final String conceptLevel1Id) {
this.conceptLevel1Id = conceptLevel1Id;
}
}

View File

@ -1,79 +0,0 @@
package eu.dnetlib.is.context.model;
import java.io.Serializable;
import java.util.Objects;
public class ConceptLevel2PK implements Serializable {
private static final long serialVersionUID = -5543486531261862945L;
private String id;
private String contextId;
private String categoryId;
private String conceptLevel0Id;
private String conceptLevel1Id;
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public String getContextId() {
return contextId;
}
public void setContextId(final String contextId) {
this.contextId = contextId;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(final String categoryId) {
this.categoryId = categoryId;
}
public String getConceptLevel0Id() {
return conceptLevel0Id;
}
public void setConceptLevel0Id(final String conceptLevel0Id) {
this.conceptLevel0Id = conceptLevel0Id;
}
public String getConceptLevel1Id() {
return conceptLevel1Id;
}
public void setConceptLevel1Id(final String conceptLevel1Id) {
this.conceptLevel1Id = conceptLevel1Id;
}
@Override
public int hashCode() {
return Objects.hash(categoryId, conceptLevel0Id, conceptLevel1Id, contextId, id);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) { return true; }
if (!(obj instanceof ConceptLevel2PK)) { return false; }
final ConceptLevel2PK other = (ConceptLevel2PK) obj;
return Objects.equals(categoryId, other.categoryId) && Objects.equals(conceptLevel0Id, other.conceptLevel0Id)
&& Objects.equals(conceptLevel1Id, other.conceptLevel1Id) && Objects.equals(contextId, other.contextId) && Objects.equals(id, other.id);
}
@Override
public String toString() {
return String
.format("ConceptLevel2PK [id=%s, contextId=%s, categoryId=%s, conceptLevel0Id=%s, conceptLevel1Id=%s]", id, contextId, categoryId, conceptLevel0Id, conceptLevel1Id);
}
}

View File

@ -3,6 +3,7 @@ package eu.dnetlib.is.context.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.hibernate.annotations.Type;
@ -21,6 +22,10 @@ public abstract class CtxInfo implements Serializable {
private static final long serialVersionUID = 4912082158208138795L;
@Id
@Column(name = "id")
private String id;
@Column(name = "label")
private String label;
@ -28,9 +33,13 @@ public abstract class CtxInfo implements Serializable {
@Column(name = "params")
private Parameter[] parameters;
abstract public String getId();
public String getId() {
return id;
}
abstract public void setId(final String id);
public void setId(final String id) {
this.id = id;
}
public String getLabel() {
return label;

View File

@ -11,6 +11,9 @@ public abstract class CtxInfoWithClaim extends CtxInfo {
@Column(name = "claim")
private boolean claim;
@Column(name = "parent")
private String parent;
public boolean isClaim() {
return claim;
}
@ -19,4 +22,12 @@ public abstract class CtxInfoWithClaim extends CtxInfo {
this.claim = claim;
}
public String isParent() {
return parent;
}
public void setParent(final String parent) {
this.parent = parent;
}
}

View File

@ -3,9 +3,8 @@ package eu.dnetlib.is.context.model.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import eu.dnetlib.is.context.model.Category;
import eu.dnetlib.is.context.model.CategoryPK;
public interface CategoryRepository extends JpaRepository<Category, CategoryPK> {
public interface CategoryRepository extends JpaRepository<Category, String> {
Iterable<Category> findByContextIdOrderById(String contextId);
Iterable<Category> findByParentOrderById(String parent);
}

View File

@ -2,9 +2,11 @@ package eu.dnetlib.is.context.model.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import eu.dnetlib.is.context.model.Category;
import eu.dnetlib.is.context.model.ConceptLevel0;
import eu.dnetlib.is.context.model.ConceptLevel0PK;
public interface ConceptLevel0Repository extends JpaRepository<ConceptLevel0, ConceptLevel0PK> {
public interface ConceptLevel0Repository extends JpaRepository<ConceptLevel0, String> {
Iterable<Category> findByParentOrderById(String parent);
}

View File

@ -2,9 +2,11 @@ package eu.dnetlib.is.context.model.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import eu.dnetlib.is.context.model.Category;
import eu.dnetlib.is.context.model.ConceptLevel1;
import eu.dnetlib.is.context.model.ConceptLevel1PK;
public interface ConceptLevel1Repository extends JpaRepository<ConceptLevel1, ConceptLevel1PK> {
public interface ConceptLevel1Repository extends JpaRepository<ConceptLevel1, String> {
Iterable<Category> findByParentOrderById(String parent);
}

View File

@ -2,9 +2,11 @@ package eu.dnetlib.is.context.model.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import eu.dnetlib.is.context.model.Category;
import eu.dnetlib.is.context.model.ConceptLevel2;
import eu.dnetlib.is.context.model.ConceptLevel2PK;
public interface ConceptLevel2Repository extends JpaRepository<ConceptLevel2, ConceptLevel2PK> {
public interface ConceptLevel2Repository extends JpaRepository<ConceptLevel2, String> {
Iterable<Category> findByParentOrderById(String parent);
}

View File

@ -27,52 +27,39 @@ CREATE TABLE contexts (
);
CREATE TABLE context_categories (
ctx_id text NOT NULL REFERENCES contexts(id) ON UPDATE CASCADE ON DELETE CASCADE,
id text NOT NULL,
id text NOT NULL PRIMARY KEY,
parent text NOT NULL REFERENCES contexts(id) ON UPDATE CASCADE ON DELETE CASCADE,
label text NOT NULL,
claim boolean NOT NULL,
params jsonb,
PRIMARY KEY (ctx_id, id)
params jsonb
);
CREATE TABLE context_cat_concepts_lvl_0 (
ctx_id text NOT NULL REFERENCES contexts(id) ON UPDATE CASCADE ON DELETE CASCADE,
cat_id text NOT NULL,
id text NOT NULL,
id text NOT NULL PRIMARY KEY,
parent text NOT NULL REFERENCES context_categories(id) ON UPDATE CASCADE ON DELETE CASCADE,
label text NOT NULL,
claim boolean NOT NULL,
params jsonb,
PRIMARY KEY (ctx_id, cat_id, id),
FOREIGN KEY (ctx_id, cat_id) REFERENCES context_categories(ctx_id, id) ON UPDATE CASCADE ON DELETE CASCADE
params jsonb
);
CREATE TABLE context_cat_concepts_lvl_1 (
ctx_id text NOT NULL REFERENCES contexts(id) ON UPDATE CASCADE ON DELETE CASCADE,
cat_id text NOT NULL,
c0_id text NOT NULL,
id text NOT NULL,
id text NOT NULL PRIMARY KEY,
parent text NOT NULL REFERENCES context_cat_concepts_lvl_0(id) ON UPDATE CASCADE ON DELETE CASCADE,
label text NOT NULL,
claim boolean NOT NULL,
params jsonb,
PRIMARY KEY (ctx_id, cat_id, c0_id, id),
FOREIGN KEY (ctx_id, cat_id, c0_id) REFERENCES context_cat_concepts_lvl_0(ctx_id, cat_id, id) ON UPDATE CASCADE ON DELETE CASCADE
params jsonb
);
CREATE TABLE context_cat_concepts_lvl_2 (
ctx_id text NOT NULL REFERENCES contexts(id) ON UPDATE CASCADE ON DELETE CASCADE,
cat_id text NOT NULL,
c0_id text NOT NULL,
c1_id text NOT NULL,
id text NOT NULL,
id text NOT NULL PRIMARY KEY,
parent text NOT NULL REFERENCES context_cat_concepts_lvl_1(id) ON UPDATE CASCADE ON DELETE CASCADE,
label text NOT NULL,
claim boolean NOT NULL,
params jsonb,
PRIMARY KEY (ctx_id, cat_id, c0_id, c1_id, id),
FOREIGN KEY (ctx_id, cat_id, c0_id, c1_id) REFERENCES context_cat_concepts_lvl_1(ctx_id, cat_id, c0_id, id) ON UPDATE CASCADE ON DELETE CASCADE
params jsonb
);
CREATE INDEX ON context_categories (ctx_id);
CREATE INDEX ON context_cat_concepts_lvl_0 (ctx_id, cat_id);
CREATE INDEX ON context_cat_concepts_lvl_1 (ctx_id, cat_id, c0_id);
CREATE INDEX ON context_cat_concepts_lvl_2 (ctx_id, cat_id, c0_id, c1_id);
CREATE INDEX ON context_categories (parent);
CREATE INDEX ON context_cat_concepts_lvl_0 (parent);
CREATE INDEX ON context_cat_concepts_lvl_1 (parent);
CREATE INDEX ON context_cat_concepts_lvl_2 (parent);