Skip to content

Commit

Permalink
MCR-2647 fix static metamodel and some jql queries
Browse files Browse the repository at this point in the history
deprecate MCRCategoryID.getID()
  • Loading branch information
yagee-de committed Oct 13, 2023
1 parent ec70e5a commit 44ace0d
Show file tree
Hide file tree
Showing 41 changed files with 85 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Date;

import jakarta.persistence.Basic;
import org.mycore.backend.jpa.MCRObjectIDConverter;
import org.mycore.datamodel.metadata.MCRObjectID;

Expand Down Expand Up @@ -127,6 +128,7 @@ public MCRAccessKey(final String secret, final String type) {
length = MCRObjectID.MAX_LENGTH,
nullable = false)
@Convert(converter = MCRObjectIDConverter.class)
@Basic
public MCRObjectID getObjectId() {
return objectId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public Optional<MCRStringFact> computeFact(MCRFactsHolder facts) {
Optional<MCRObject> optMcrObj = idc.get().getObject();
if (optMcrObj.isPresent()) {
MCRCategoryID state = optMcrObj.get().getService().getState();
if (getTerm().equals(state.getID())) {
if (getTerm().equals(state.getId())) {
MCRStringFact fact = new MCRStringFact(getFactName(), getTerm());
fact.setValue(state.getID());
fact.setValue(state.getId());
facts.add(fact);
return Optional.of(fact);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ public void updateRule(MCRAccessRule rule) {
@Override
public void deleteRule(String ruleid) {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
em.createQuery("delete MCRACCESSRULE where RID = '" + ruleid + "'").executeUpdate();
em.createQuery("delete MCRACCESSRULE where rid = :rid")
.setParameter("rid", ruleid)
.executeUpdate();
ruleCache.invalidate(ruleid);
}

Expand Down Expand Up @@ -185,19 +187,17 @@ public int getNextFreeRuleID(String prefix) {
int ret = 1;
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
List<String> l = em
.createQuery("select max(rid) from MCRACCESSRULE where rid like '" + prefix + "%'", String.class)
.createQuery("select max(rid) from MCRACCESSRULE where rid like :like", String.class)
.setParameter("like", prefix + "%")
.getResultList();
if (l.size() > 0) {
String max = l.get(0);
if (max == null) {
ret = 1;
} else {
int lastNumber = Integer.parseInt(max.substring(prefix.length()));
ret = lastNumber + 1;
}
} else {
if (l.isEmpty()) {
return 1;
}
String max = l.get(0);
if (max != null) {
int lastNumber = Integer.parseInt(max.substring(prefix.length()));
ret = lastNumber + 1;
}
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private MCRCategoryID() {
*/
public MCRCategoryID(String rootID, String id) {
this();
setID(id);
setId(id);
setRootID(rootID);
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public static MCRCategoryID fromString(String categoryId) {

@Transient
public boolean isRootID() {
return id == null || id.equals("");
return id == null || id.isEmpty();
}

/*
Expand Down Expand Up @@ -144,10 +144,10 @@ public boolean equals(Object obj) {
}
final MCRCategoryID other = (MCRCategoryID) obj;
if (id == null) {
if (other.id != null && other.id.length() > 0) {
if (other.id != null && !other.id.isEmpty()) {
return false;
}
} else if (!id.equals(other.id) && (id.length() > 0 || other.id != null && other.id.length() >= 0)) {
} else if (!id.equals(other.id) && (!id.isEmpty() || other.id != null && other.id.length() >= 0)) {
return false;
}
if (rootID == null) {
Expand All @@ -157,19 +157,28 @@ public boolean equals(Object obj) {
}
}

/**
* @deprecated Use {@link #getId()} instead.
*/
@Deprecated
@Transient
public String getID(){
return getId();
}

/**
* @return the ID
*/
public String getID() {
public String getId() {
return id == null ? "" : id;
}

/**
* @param id
* the ID to set
*/
private void setID(String id) {
if (id != null && id.length() > 0) {
private void setId(String id) {
if (id != null && !id.isEmpty()) {
if (!VALID_ID.matcher(id).matches()) {
throw new MCRException("category ID '" + id + "' is invalid and does not match: " + VALID_ID);
}
Expand Down Expand Up @@ -229,7 +238,7 @@ private void setRootID(String rootID) {
@Override
@JsonValue
public String toString() {
if (id == null || id.length() == 0) {
if (id == null || id.isEmpty()) {
return rootID;
}
return rootID + ':' + id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public boolean isInCategory(MCRCategLinkReference reference, MCRCategoryID id) {
setCacheable(q);
setReadOnly(q);
q.setParameter("rootID", id.getRootID());
q.setParameter("categID", id.getID());
q.setParameter("categID", id.getId());
q.setParameter("objectID", reference.getObjectID());
q.setParameter("type", reference.getType());
return !q.getResultList().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public List<MCRCategory> getParents(MCRCategoryID id) {
Query parentQuery = entityManager
.createNamedQuery(NAMED_QUERY_NAMESPACE + "parentQuery")
.setParameter("classID", id.getRootID())
.setParameter("categID", id.getID())
.setParameter("categID", id.getId())
.setParameter("left", leftRight.leftValue)
.setParameter("right", leftRight.rightValue);
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -616,7 +616,7 @@ public static MCRCategoryImpl getByNaturalID(EntityManager entityManager, MCRCat
TypedQuery<MCRCategoryImpl> naturalIDQuery = entityManager
.createNamedQuery(NAMED_QUERY_NAMESPACE + "byNaturalId", MCRCategoryImpl.class)
.setParameter("classID", id.getRootID())
.setParameter("categID", id.getID());
.setParameter("categID", id.getId());
return getSingleResult(naturalIDQuery);
}

Expand Down Expand Up @@ -650,7 +650,7 @@ private static int getNumberOfChildren(EntityManager entityManager, MCRCategoryI
return getSingleResult(entityManager
.createNamedQuery(NAMED_QUERY_NAMESPACE + "childCount")
.setParameter("classID", id.getRootID())
.setParameter("categID", id.getID()));
.setParameter("categID", id.getId()));
}

private static void updateLeftRightValue(EntityManager entityManager, String classID, int left,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private int getPositionInParentByID() {
.getChildren()
.stream()
.map(MCRCategory::getId)
.map(MCRCategoryID::getID)
.map(MCRCategoryID::getId)
.collect(Collectors.joining(", ")));
}

Expand Down Expand Up @@ -465,14 +465,14 @@ public void setRootID(String rootID) {
if (getId() == null) {
setId(MCRCategoryID.rootID(rootID));
} else if (!getId().getRootID().equals(rootID)) {
setId(new MCRCategoryID(rootID, getId().getID()));
setId(new MCRCategoryID(rootID, getId().getId()));
}
}

public void setCategID(String categID) {
if (getId() == null) {
setId(new MCRCategoryID(null, categID));
} else if (!getId().getID().equals(categID)) {
} else if (!getId().getId().equals(categID)) {
setId(new MCRCategoryID(getId().getRootID(), categID));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void setID(String value) {
public static MCRClassCategory getInstance(MCRCategory from) {
MCRClassCategory categ = new MCRClassCategory();
MCRCategoryID categoryID = from.getId();
categ.setID(categoryID.isRootID() ? categoryID.getRootID() : categoryID.getID());
categ.setID(categoryID.isRootID() ? categoryID.getRootID() : categoryID.getId());
categ.setUrl(MCRClassURL.getInstance(from.getURI()));
categ.getCategory()
.addAll(getInstance(from.getChildren()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static Document getDocument(MCRCategory cl, Map<MCRCategoryID, Number> countMap)

static Element getElement(MCRCategory category, Map<MCRCategoryID, Number> countMap) {
Element ce = new Element("category");
ce.setAttribute("ID", category.getId().getID());
ce.setAttribute("ID", category.getId().getId());
Number number = countMap == null ? null : countMap.get(category.getId());
if (number != null) {
ce.setAttribute("counter", Integer.toString(number.intValue()));
Expand Down Expand Up @@ -246,7 +246,7 @@ void addChildren(Element parent, MCRCategory category) {
}

Element ce = new Element("item");
ce.setAttribute("value", completeId ? category.getId().toString() : category.getId().getID());
ce.setAttribute("value", completeId ? category.getId().toString() : category.getId().getId());
parent.addContent(ce);

for (MCRLabel label : category.getLabels()) {
Expand All @@ -273,7 +273,7 @@ void addLabel(Element item, MCRLabel label, MCRCategory cat) {
} catch (RuntimeException e) {
throw new RuntimeException("Error while inserting '" + labtext + "' into: " + labelFormat, e);
}
text = ID_PATTERN.matcher(text).replaceAll(cat.getId().getID());
text = ID_PATTERN.matcher(text).replaceAll(cat.getId().getId());
text = DESCR_PATTERN.matcher(text)
.replaceAll(label.getDescription().replace("\\", "\\\\").replace("$", "\\$"));
int num = countMap == null ? -1 : countMap.get(cat.getId()).intValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static String retrieveURI(MCRCategory categ) {
return MCRFrontendUtil.getBaseURL() + "open-data/classification/" + categ.getId().getRootID();
}
return MCRFrontendUtil.getBaseURL() + "open-data/classification/" + categ.getId().getRootID() + "/"
+ categ.getId().getID();
+ categ.getId().getId();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private void buildLanguagesFromClassification() {
* Builds a new language object from the given category
*/
private void buildLanguage(MCRCategory category) {
String xmlCode = category.getId().getID();
String xmlCode = category.getId().getId();
String termCode = category.getLabel("x-term").map(MCRLabel::getText).orElse(null);
String biblCode = category.getLabel("x-bibl").map(MCRLabel::getText).orElse(termCode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class MCRDerivateDefaultClassEventHandler extends MCREventHandlerBase {

private static MCRMetaClassification asMetaClassification(MCRCategoryID categoryId) {
return new MCRMetaClassification("classification", 0, null,
categoryId.getRootID(), categoryId.getID());
categoryId.getRootID(), categoryId.getId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void setClassifications(List<MCRCategoryID> list) {
list.stream().map(clazz -> {
final Element classElement = new Element(CLASSIFICATION_ELEMENT_NAME);
classElement.setAttribute(CLASSID_ATTRIBUTE_NAME, clazz.getRootID());
classElement.setAttribute(CATEGID_ATTRIBUTE_NAME, clazz.getID());
classElement.setAttribute(CATEGID_ATTRIBUTE_NAME, clazz.getId());
return classElement;
}).forEach(getContentList()::add);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public final String getClassId() {
* @return the categId
*/
public final String getCategId() {
return category.getID();
return category.getId();
}

/**
Expand Down Expand Up @@ -186,7 +186,7 @@ public Element createXML() throws MCRException {
public JsonObject createJSON() {
JsonObject obj = super.createJSON();
obj.addProperty("classid", category.getRootID());
obj.addProperty("categid", category.getID());
obj.addProperty("categid", category.getId());
return obj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,8 @@ public final JsonObject createJSON() {
// state
Optional.ofNullable(getState()).ifPresent(stateId -> {
JsonObject state = new JsonObject();
if (stateId.getID() != null) {
state.addProperty("id", stateId.getID());
if (stateId.getId() != null) {
state.addProperty("id", stateId.getId());
}
state.addProperty("rootId", stateId.getRootID());
});
Expand Down Expand Up @@ -1262,7 +1262,7 @@ public final void addClassification(String type, MCRCategoryID value) {
*/
public final void replaceClassification(int index, MCRCategoryID value) throws IndexOutOfBoundsException {
updateClassification(index,
classificationValue -> classificationValue.setValue(value.getRootID(), value.getID()));
classificationValue -> classificationValue.setValue(value.getRootID(), value.getId()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ static boolean objectIsHidden(MCRObject obj) {
LogManager.getLogger().debug("{} is visible as it does not use a service state.", obj.getId());
return false;
}
boolean hidden = !"published".equals(state.getID());
boolean hidden = !"published".equals(state.getId());
LogManager.getLogger().debug("{} is hidden due to service state '{}': {}", obj.getId(), state, hidden);
return hidden;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ private static void listCategory(MCRCategory categ) {
int level = categ.getLevel();
String space = " ".repeat(level * 2);
if (categ.isCategory()) {
LOGGER.info("{} ID : {}", space, categ.getId().getID());
LOGGER.info("{} ID : {}", space, categ.getId().getId());
}
for (MCRLabel label : categ.getLabels()) {
LOGGER.info("{} Label : ({}) {}", space, label.getLang(), label.getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ public static void setClassificationOfDerivate(String derivateIDStr, String cate

final String nonExistingCategoriesCommaList = derivateTypes.stream()
.filter(Predicate.not(categoryDAO::exist))
.map(MCRCategoryID::getID)
.map(MCRCategoryID::getId)
.collect(Collectors.joining(", "));
if (!nonExistingCategoriesCommaList.isEmpty()) {
throw new MCRPersistenceException("Categories do not exist: " + nonExistingCategoriesCommaList);
Expand All @@ -860,7 +860,7 @@ public static void setClassificationOfDerivate(String derivateIDStr, String cate
.addAll(
derivateTypes.stream()
.map(categoryID -> new MCRMetaClassification("classification", 0, null, categoryID.getRootID(),
categoryID.getID()))
categoryID.getId()))
.collect(Collectors.toList()));
MCRMetadataManager.update(derivate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public static List<MCRMetaClassification> getClassifications(String classificati

return categoryIDS.stream()
.map(category -> new MCRMetaClassification("classification", 0, null, category.getRootID(),
category.getID()))
category.getId()))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testMCRCategoryIDStringString() {
MCRCategoryID categID;
categID = new MCRCategoryID(validRootID, validCategID);
assertEquals("RootIDs do not match", validRootID, categID.getRootID());
assertEquals("CategIDs do not match", validCategID, categID.getID());
assertEquals("CategIDs do not match", validCategID, categID.getId());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private Element getClassificationBrowserData(HttpServletRequest req, Settings se
Optional<MCRQueryAdapter> queryAdapter = configureQueryAdapter(req, settings, xml);

Function<MCRCategoryID, String> toIdSearchValue = settings.addClassId() ? MCRCategoryID::toString
: MCRCategoryID::getID;
: MCRCategoryID::getId;
List<Element> data = new ArrayList<>();
for (MCRCategory child : category.getChildren()) {
queryAdapter.ifPresent(qa -> qa.setCategory(toIdSearchValue.apply(child.getId())));
Expand All @@ -116,7 +116,7 @@ private Element getClassificationBrowserData(HttpServletRequest req, Settings se
if ((settings.removeEmptyLeaves()) && (numResults < 1)) {
continue;
}
if(settings.excludeCategories().contains(child.getId().getID())) {
if(settings.excludeCategories().contains(child.getId().getId())) {
continue;
}

Expand All @@ -139,7 +139,7 @@ private Element getCategoryElement(MCRCategory category, long numResults,
categoryE.setAttribute("numResults", String.valueOf(numResults));
}

categoryE.setAttribute("id", category.getId().getID());
categoryE.setAttribute("id", category.getId().getId());
categoryE.setAttribute("children", Boolean.toString(category.hasChildren()));

if (settings.addUri() && (category.getURI() != null)) {
Expand Down
Loading

0 comments on commit 44ace0d

Please sign in to comment.