Skip to content

Commit

Permalink
Added functionality to see proposed items in API and frontend #138
Browse files Browse the repository at this point in the history
Changed the functionality so draft items are visible when in table reg_status (draft) ispublic = true
  • Loading branch information
unaibrrgn committed Jun 23, 2022
1 parent 4dc6409 commit 858d72e
Show file tree
Hide file tree
Showing 12 changed files with 1,778 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static String getURI(RegItem regItem) throws Exception {
throw new RuntimeException("Invalid type");
}
}

private static List<RegItem> getCollectionChain(RegItem regItem, EntityManager entityManager) throws Exception {
RegRelationpredicateManager regRelationpredicateManager = new RegRelationpredicateManager(entityManager);
RegRelationpredicate hasCollection = regRelationpredicateManager.get(BaseConstants.KEY_PREDICATE_COLLECTION);
Expand All @@ -276,7 +276,7 @@ protected static RegItem getRelatedItemBySubject(RegItem regItem, RegRelationpre
// .findAny()
// .orElse(null);
}

private static List<RegItem> getRelatedItemsBySubject(RegItem regItem, RegRelationpredicate predicate, EntityManager entityManager) throws Exception {
RegRelationManager regRelationManager = new RegRelationManager(entityManager);
if (regRelationManager != null && regItem != null && predicate != null
Expand All @@ -288,5 +288,4 @@ private static List<RegItem> getRelatedItemsBySubject(RegItem regItem, RegRelati
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@

import eu.europa.ec.re3gistry2.crudimplementation.RegFieldManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegItemManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegItemproposedManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegLanguagecodeManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegLocalizationManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegLocalizationproposedManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegRelationManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegRelationpredicateManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegRelationproposedManager;
import eu.europa.ec.re3gistry2.model.RegField;
import eu.europa.ec.re3gistry2.model.RegItem;
import eu.europa.ec.re3gistry2.model.RegItemclass;
import eu.europa.ec.re3gistry2.model.RegItemproposed;
import eu.europa.ec.re3gistry2.model.RegLanguagecode;
import eu.europa.ec.re3gistry2.model.RegLocalization;
import eu.europa.ec.re3gistry2.model.RegLocalizationproposed;
import eu.europa.ec.re3gistry2.model.RegRelation;
import eu.europa.ec.re3gistry2.model.RegRelationproposed;
import eu.europa.ec.re3gistry2.model.RegRelationpredicate;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;

public class ItemproposedHelper {
Expand Down Expand Up @@ -96,6 +97,65 @@ public static String getProposedURI(RegItemproposed regItemproposed, RegItem reg
}
return uri;
}

public static String getProposedURI(RegItemproposed regItem) throws Exception {
// Getting the DB manager
EntityManager entityManager = PersistenceFactory.getEntityManagerFactory().createEntityManager();

RegRelationpredicateManager relationPredicateManager = new RegRelationpredicateManager(entityManager);
RegRelationpredicate hasRegistry = relationPredicateManager.get(BaseConstants.KEY_PREDICATE_REGISTRY);
RegRelationpredicate hasRegister = relationPredicateManager.get(BaseConstants.KEY_PREDICATE_REGISTER);

if (regItem == null) {
return null;
}
RegItemclass itemclass = regItem.getRegItemclass();
switch (itemclass.getRegItemclasstype().getLocalid()) {
case BaseConstants.KEY_ITEMCLASS_TYPE_REGISTRY:
return itemclass.getBaseuri() + "/" + regItem.getLocalid();
case BaseConstants.KEY_ITEMCLASS_TYPE_REGISTER:
String baseuri = itemclass.getBaseuri();
if (baseuri != null) {
return baseuri + "/" + regItem.getLocalid();
}
String registryURI = getProposedURI(getRelatedItemProposedBySubject(regItem, hasRegistry, entityManager));
return registryURI + "/" + regItem.getLocalid();
case BaseConstants.KEY_ITEMCLASS_TYPE_ITEM:
String itemURI = null;
if (regItem.getExternal()) {
itemURI = regItem.getLocalid();
} else {
String registerURI = getProposedURI(getRelatedItemProposedBySubject(regItem, hasRegister, entityManager));
List<RegItemproposed> collectionChain = getCollectionChain(regItem, entityManager);
if (collectionChain.isEmpty()) {
return registerURI + "/" + regItem.getLocalid();
}
String collectionsPath = collectionChain.stream()
.map(collection -> collection.getLocalid())
.collect(Collectors.joining("/"));
itemURI = registerURI + "/" + collectionsPath + "/" + regItem.getLocalid();
}

return itemURI;
default:
throw new RuntimeException("Invalid type");
}
}

private static List<RegItemproposed> getCollectionChain(RegItemproposed regItem, EntityManager entityManager) throws Exception {
RegRelationpredicateManager regRelationpredicateManager = new RegRelationpredicateManager(entityManager);
RegRelationpredicate hasCollection = regRelationpredicateManager.get(BaseConstants.KEY_PREDICATE_COLLECTION);
RegItemproposed collection = getRelatedItemProposedBySubject(regItem, hasCollection, entityManager);
if (collection == null) {
return Collections.emptyList();
}
LinkedList<RegItemproposed> collectionChain = new LinkedList<>();
while (collection != null) {
collectionChain.addFirst(collection);
collection = getRelatedItemProposedBySubject(collection, hasCollection, entityManager);
}
return collectionChain;
}

public static HashMap<String, String> getRegistryLocalizationByRegItemproposed(RegItemproposed regItemproposed, EntityManager entityManager, RegLanguagecode regLanguagecode) {
RegLanguagecodeManager regLanguagecodeManager = new RegLanguagecodeManager(entityManager);
Expand Down Expand Up @@ -213,5 +273,25 @@ public static String getBreadcrumbCollectionHTMLForRegItemproposed(String HTML,

}
}

protected static RegItemproposed getRelatedItemProposedBySubject(RegItemproposed regItem, RegRelationpredicate predicate, EntityManager entityManager) throws Exception {
List<RegItemproposed> list = getRelatedItemsProposedBySubject(regItem, predicate, entityManager);
if (list != null && !list.isEmpty()) {
return list.stream().findAny().orElse(null);
}
return null;
}

private static List<RegItemproposed> getRelatedItemsProposedBySubject(RegItemproposed regItem, RegRelationpredicate predicate, EntityManager entityManager) throws Exception {
RegRelationproposedManager regRelationManager = new RegRelationproposedManager(entityManager);
if (regRelationManager != null && regItem != null && predicate != null
&& regRelationManager.getAll(regItem, predicate) != null) {
return regRelationManager.getAll(regItem, predicate).stream()
.map(rel -> rel.getRegItemproposedObject())
.collect(Collectors.toList());
} else {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public RegItem get(String uuid) throws Exception {
return (RegItem) q.getSingleResult();

}

/**
* Returns all the RegItems
*
Expand Down Expand Up @@ -249,7 +249,7 @@ public List<RegItem> getAll(RegItemclass regItemcalss) throws Exception {
q.setParameter(SQLConstants.SQL_PARAMETERS_REGITEMCLASS, regItemcalss);
return (List<RegItem>) q.getResultList();
}

/**
* Returns all the RegItems by RegAction
*
Expand Down Expand Up @@ -514,7 +514,7 @@ public List<RegItem> getAllSubjectsByRegItemObjectAndPredicateAndSubjectNotPredi
return null;
}
}

/**
* Returns all RegItems (subject) by RegItem (object) and RegPredicate where
* subject RegItems must not have second RegPredicate Useful for example for
Expand Down Expand Up @@ -547,7 +547,7 @@ public List<String> getAllItemByRegItemObjectAndPredicateAndSubjectNotPredicate(
}
return (List<String>) q.getResultList();
}

@Override
public List<RegItem> getAllValid(RegItemclass regItemcalss) throws Exception {
//Preparing query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public List<RegItemhistory> getByRegItemReference(RegItem regItemReference) thro

return (List<RegItemhistory>) q.getResultList();
}

/**
* Find the RegItemhistory specified by parameter. Returns RegItemhistory if
* the operation succeed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import eu.europa.ec.re3gistry2.model.RegItemclass;
import eu.europa.ec.re3gistry2.model.RegItemclasstype;
import eu.europa.ec.re3gistry2.model.RegRelationpredicate;
import eu.europa.ec.re3gistry2.model.RegStatus;
import eu.europa.ec.re3gistry2.model.RegUser;
import java.text.MessageFormat;

Expand Down Expand Up @@ -474,4 +475,45 @@ public List<RegItemproposed> getAll(RegAction regAction) throws Exception {
q.setParameter("regAction", regAction);
return (List<RegItemproposed>) q.getResultList();
}

public List<String> getAllItemByRegItemProposedObjectAndPredicateAndSubjectNotPredicate(RegItem regItem, RegStatus regStatus, RegRelationpredicate regRelationPredicate, RegRelationpredicate subjectNotHavingPredicate) throws Exception {
//Preparing query
Query q = null;
// q = this.em.createQuery(SQLConstants.SQL_GET_REG_ITEM_BY_SUBJECT_PREDICATE_AND_FILTER_PREDICATE);
// q.setParameter(SQLConstants.SQL_PARAMETERS_REGITEM, regItem);
// q.setParameter(SQLConstants.SQL_PARAMETERS_REGSTATUS, regStatus);
// q.setParameter(SQLConstants.SQL_PARAMETERS_PREDICATE, regRelationPredicate);
// q.setParameter(SQLConstants.SQL_PARAMETERS_NOT_PREDICATE, subjectNotHavingPredicate);
try {
String query = "select r0.reg_item_subject from (select * from reg_relation r JOIN reg_item ri on ri.uuid = r.reg_item_subject WHERE ri.reg_status = ':regStatus' AND r.reg_item_object = ':regitem' and r.reg_relationpredicate = ':predicate') as r0 where r0.reg_item_subject not in (select r1.reg_item_subject from reg_relation r1 where r1.reg_relationpredicate = ':notpredicate')";
query = query.replace(":" + SQLConstants.SQL_PARAMETERS_REGSTATUS, regStatus.getUuid()).replace(":" + SQLConstants.SQL_PARAMETERS_REGITEM, regItem.getUuid()).replace(":" + SQLConstants.SQL_PARAMETERS_PREDICATE, regRelationPredicate.getUuid()).replace(":" + SQLConstants.SQL_PARAMETERS_NOT_PREDICATE, subjectNotHavingPredicate.getUuid());
q = this.em.createNativeQuery(query);
} catch (Exception ex) {
System.err.println("RegItemproposedManager.getAllItemByRegItemProposedObjectAndPredicateAndSubjectNotPredicate(), regItem: " + regItem.getLocalid());
return null;
}
return (List<String>) q.getResultList();
}

public List<RegItemproposed> getAllSubjectsByRegItemProposedObjectAndPredicateAndSubjectNotPredicate(RegItem regItem, RegRelationpredicate regRelationPredicate, RegRelationpredicate subjectNotHavingPredicate) throws Exception {
//Preparing query
Query q = this.em.createQuery(SQLConstants.SQL_GET_REG_ITEM_BY_OBJECT_PREDICATE_AND_SUBJECT_FILTER);
q.setParameter(SQLConstants.SQL_PARAMETERS_REGITEMPROPOSED, regItem);
q.setParameter(SQLConstants.SQL_PARAMETERS_PREDICATE, regRelationPredicate);
q.setParameter(SQLConstants.SQL_PARAMETERS_NOT_PREDICATE, subjectNotHavingPredicate);
try {
return (List<RegItemproposed>) q.getResultList();
} catch (Exception e) {
return null;
}
}

public List<RegItemproposed> getAllItemProposed(RegItemclass regItemcalss) throws Exception {
//Preparing query
Query q = this.em.createQuery(SQLConstants.SQL_GET_REGITEM_BY_REGITEMCLASS);
q.setParameter(SQLConstants.SQL_PARAMETERS_REGITEMCLASS, regItemcalss);
return (List<RegItemproposed>) q.getResultList();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public List<RegLocalization> getAll(RegItem regItem, RegLanguagecode regLanguage
q.setParameter(SQLConstants.SQL_PARAMETERS_REGLANGUAGECODE, regLanguagecode);
return (List<RegLocalization>) q.getResultList();
}

/**
* Returns all the RegLocalization for the specified RegItems
* in the specific language
Expand Down Expand Up @@ -313,7 +313,7 @@ public List<RegLocalization> getAll(RegField regField, RegItem regItem, RegLangu
q.setParameter(SQLConstants.SQL_PARAMETERS_REGLANGUAGECODE, regLanguagecode);
return (List<RegLocalization>) q.getResultList();
}

/**
* Returns all the RegLocalization for the specified RegField, RegItem and
* RegAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public List<RegRelation> getAllByRegItemObjectAndPredicate(RegItem regItem, RegR
return null;
}
}

/**
* Returns all the RegRelation by RegItem (object) and RegPredicate
*
Expand Down Expand Up @@ -275,7 +275,7 @@ public List<RegRelation> getAllByRegItemSubjectAndPredicate(RegItem regItem, Reg
return null;
}
}

/**
* Returns all the RegRelation by RegItems (subject) and RegPredicate
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ public List<RegRelationproposed> getAll(RegItemproposed regItemproposed, RegRela
return null;
}
}

public List<RegRelationproposed> getAllByRegItemsSubjectAndPredicate(List<RegItemproposed> regItems, RegRelationpredicate regRelationPredicate) throws Exception {
// //Preparing query
// Query q = this.em.createQuery(SQLConstants.SQL_GET_RELATIONPROPOSED_SUBJECTS_PREDICATE);
// q.setParameter(SQLConstants.SQL_PARAMETERS_REGITEM_LIST, regItems);
// q.setParameter(SQLConstants.SQL_PARAMETERS_PREDICATE, regRelationPredicate);
// try {
// return (List<RegRelationproposed>) q.getResultList();
// } catch (Exception e) {
// return null;
// }
return null;
}

/**
* Returns all the RegRelationproposed by the RegRelation reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private SQLConstants() {
public static final String SQL_GET_REGITEMPROPOSED_BY_REGUSER = "SELECT r FROM RegItemproposed r WHERE r.regUser = :regUser";
public static final String SQL_GET_REGITEMPROPOSED_BY_REGUSER_COUNT = "SELECT count(r) FROM RegItemproposed r WHERE r.regUser = :regUser";
public static final String SQL_GET_REGITEMPROPOSED_BY_REGACTION = "SELECT r FROM RegItemproposed r WHERE r.regAction = :regAction";

public static final String SQL_GET_REGITEMPROPOSED_NEW_BY_REGITEMCLASSES_COLLECTION = "SELECT p.regItemproposedSubject FROM RegRelationproposed p JOIN p.regItemproposedSubject r WHERE r.regItemclass IN :regItemclasses AND r.regItemReference IS NULL and p.regItemObject = :regItemObject and p.regRelationpredicate=:regRelationpredicete ORDER BY r.localid";
public static final String SQL_GET_REGITEMPROPOSED_NEW_BY_REGITEMCLASSES_COLLECTION_COUNT = "SELECT count(p.regItemproposedSubject) FROM RegRelationproposed p JOIN p.regItemproposedSubject r WHERE r.regItemclass IN :regItemclasses AND r.regItemReference IS NULL and p.regItemObject = :regItemObject and p.regRelationpredicate=:regRelationpredicete";

Expand Down
Loading

0 comments on commit 858d72e

Please sign in to comment.