Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ibp 3981 variable type get by name deprecation #869

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/main/java/org/generationcp/middleware/dao/oms/CVTermDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* DAO class for {@link CVTerm}.
Expand Down Expand Up @@ -183,8 +182,13 @@ private VariableType getDefaultVariableType(final Integer cvTermId) {
criteria.addOrder(Order.asc("cvTermPropertyId"));
final List<CVTermProperty> variableTypes = criteria.list();
if (variableTypes != null) {
final List<Term> allVariableTypes = this.getTermByCvId(CvId.VARIABLE_TYPE.getId());
final Map<String, Integer> allVariableTypesByName = new HashMap<>();
for (final Term term: allVariableTypes) {
allVariableTypesByName.put(term.getName(), term.getId());
}
for (final CVTermProperty cvTermProperty : variableTypes) {
return VariableType.getByName(cvTermProperty.getValue());
return VariableType.getById(allVariableTypesByName.get(cvTermProperty.getValue()));
}
}
return null;
Expand Down Expand Up @@ -1225,6 +1229,8 @@ public Integer getStandadardVariableIdByPropertyScaleMethodRole(final Integer pr
public boolean hasPossibleTreatmentPairs(final int cvTermId, final int propertyId,
final List<Integer> hiddenFields) {
try {
final String variableTypeName = this.getTermByCvId(CvId.VARIABLE_TYPE.getId()).stream().filter(f->VariableType.TREATMENT_FACTOR.getId().equals(f.getId())).findAny() // If 'findAny' then return found
.get().getName();
final StringBuilder sqlString = new StringBuilder().append("SELECT count(c.cvterm_id) ")
.append(" FROM cvterm c ").append(" INNER JOIN cvterm_relationship pr ON pr.type_id = ")
.append(TermId.HAS_PROPERTY.getId()).append(" AND pr.subject_id = c.cvterm_id ")
Expand All @@ -1235,7 +1241,7 @@ public boolean hasPossibleTreatmentPairs(final int cvTermId, final int propertyI
.append(" AND mr.subject_id = c.cvterm_id ")
.append(" INNER JOIN cvtermprop cvprop ON cvprop.type_id = ").append(TermId.VARIABLE_TYPE.getId())
.append(" AND cvprop.cvterm_id = c.cvterm_id AND cvprop.value = '")
.append(VariableType.TREATMENT_FACTOR.getName()).append("' WHERE c.cvterm_id <> ").append(cvTermId)
.append(variableTypeName).append("' WHERE c.cvterm_id <> ").append(cvTermId)
.append(" AND c.cvterm_id NOT IN (:hiddenFields) ");

final SQLQuery query = this.getSession().createSQLQuery(sqlString.toString());
Expand All @@ -1255,6 +1261,9 @@ public List<StandardVariable> getAllPossibleTreatmentPairs(final int cvTermId, f
final List<StandardVariable> list = new ArrayList<>();

try {
final String variableTypeName = this.getTermByCvId(CvId.VARIABLE_TYPE.getId()).stream().filter(f->VariableType.TREATMENT_FACTOR.getId().equals(f.getId())).findAny() // If 'findAny' then return found
.get().getName();

final StringBuilder sqlString = new StringBuilder()
.append(
"SELECT c.cvterm_id, c.name, c.definition, pr.object_id AS propertyId, sr.object_id AS scaleId, mr.object_id AS methodId ")
Expand All @@ -1267,7 +1276,7 @@ public List<StandardVariable> getAllPossibleTreatmentPairs(final int cvTermId, f
.append(" AND mr.subject_id = c.cvterm_id ")
.append(" INNER JOIN cvtermprop cvprop ON cvprop.type_id = ").append(TermId.VARIABLE_TYPE.getId())
.append(" AND cvprop.cvterm_id = c.cvterm_id AND cvprop.value = '")
.append(VariableType.TREATMENT_FACTOR.getName()).append("' WHERE c.cvterm_id <> ").append(cvTermId)
.append(variableTypeName).append("' WHERE c.cvterm_id <> ").append(cvTermId)
.append(" AND c.cvterm_id NOT IN (:hiddenFields) ");

final SQLQuery query = this.getSession().createSQLQuery(sqlString.toString()).addScalar("cvterm_id")
Expand Down Expand Up @@ -1507,17 +1516,14 @@ private SQLQuery createCountVariableQuery(final Integer datasetId, final List<In
.getId());
stringBuilder.append(" LEFT JOIN projectprop pp ON pp.variable_id = variable.cvterm_id");
stringBuilder.append(" LEFT JOIN project dataset ON dataset.project_id = pp.project_id");
stringBuilder.append(" WHERE variableType.value in (:variableTypeNames) ");
stringBuilder.append(" WHERE variableType.value in (select te.name from cvterm te where te.cv_id = "+ CvId.VARIABLE_TYPE.getId() +") ");

if (datasetId != null) {
stringBuilder.append(" AND dataset.project_id = :datasetId ");
stringBuilder.append(" AND pp.type_id in(:variableTypes)");
}

final SQLQuery sqlQuery = this.getSession().createSQLQuery(stringBuilder.toString());
final List<String> variableTypeNames =
variableTypes.stream().map(i -> VariableType.getById(i).getName()).collect(Collectors.toList());
sqlQuery.setParameterList(VARIABLE_TYPE_NAMES, variableTypeNames);

if (datasetId != null) {
sqlQuery.setParameter("datasetId", datasetId);
Expand Down Expand Up @@ -1600,7 +1606,7 @@ private SQLQuery createVariableQuery(final Integer datasetId, final List<Integer
// To get Min and Max override values per program
stringBuilder.append(
" LEFT JOIN variable_overrides vo ON variable.cvterm_id = vo.cvterm_id AND dataset.program_uuid = vo.program_uuid");
stringBuilder.append(" WHERE variableType.value in (:variableTypeNames) ");
stringBuilder.append(" WHERE variableType.value in (select te.name from cvterm te where te.cv_id = "+ CvId.VARIABLE_TYPE.getId()+ ")");

if (datasetId != null) {
stringBuilder.append(" AND dataset.project_id = :datasetId ");
Expand All @@ -1610,9 +1616,6 @@ private SQLQuery createVariableQuery(final Integer datasetId, final List<Integer
stringBuilder.append(" GROUP BY variable.cvterm_id, traitClass.propertyTermId, scale.cvterm_id ");

final SQLQuery sqlQuery = this.getSession().createSQLQuery(stringBuilder.toString());
final List<String> variableTypeNames =
variableTypes.stream().map(i -> VariableType.getById(i).getName()).collect(Collectors.toList());
sqlQuery.setParameterList(VARIABLE_TYPE_NAMES, variableTypeNames);

if (datasetId != null) {
sqlQuery.setParameter("datasetId", datasetId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
package org.generationcp.middleware.dao.oms;

import org.generationcp.middleware.dao.GenericDAO;
import org.generationcp.middleware.domain.oms.CvId;
import org.generationcp.middleware.domain.oms.Term;
import org.generationcp.middleware.domain.oms.TermId;
import org.generationcp.middleware.domain.ontology.VariableType;
import org.generationcp.middleware.exceptions.MiddlewareQueryException;
import org.generationcp.middleware.pojos.oms.CVTermProperty;
import org.generationcp.middleware.pojos.oms.VariableOverrides;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -124,17 +128,62 @@ public Map<String, Map<Integer, VariableType>> getVariableOverridesByVariableIds

}

public VariableType getDefaultVariableType(final Integer cvTermId) {
private VariableType getDefaultVariableType(final Integer cvTermId) {
final Criteria criteria = this.getSession().createCriteria(CVTermProperty.class);
criteria.add(Restrictions.eq("cvTermId", cvTermId));
criteria.add(Restrictions.eq("typeId", TermId.VARIABLE_TYPE.getId()));
criteria.addOrder(Order.asc("cvTermPropertyId"));
final List<CVTermProperty> variableTypes = criteria.list();
if (variableTypes != null) {
final List<Term> allVariableTypes = this.getTermByCvId(CvId.VARIABLE_TYPE.getId());
final Map<String, Integer> allVariableTypesByName = new HashMap<>();
for (final Term term: allVariableTypes) {
allVariableTypesByName.put(term.getName(), term.getId());
}
for (final CVTermProperty cvTermProperty : variableTypes) {
return VariableType.getByName(cvTermProperty.getValue());
return VariableType.getById(allVariableTypesByName.get(cvTermProperty.getValue()));
}
}
return null;
}

private List<Term> getTermByCvId(final int cvId) {

final List<Term> terms = new ArrayList<>();

try {

final StringBuilder sqlString = new StringBuilder()
.append("SELECT DISTINCT cvt.cvterm_id, cvt.cv_id, cvt.name, cvt.definition ")
.append("FROM cvterm cvt ").append("WHERE cvt.cv_id = :cvId");

final SQLQuery query = this.getSession().createSQLQuery(sqlString.toString());
query.setParameter("cvId", cvId);

final List<Object[]> results = query.list();

if (!results.isEmpty()) {

for (final Object[] row : results) {

final Integer cvtermId = (Integer) row[0];
final Integer cvtermCvId = (Integer) row[1];
final String cvtermName = (String) row[2];
final String cvtermDefinition = (String) row[3];

final Term term = new Term();
term.setId(cvtermId);
term.setName(cvtermName);
term.setDefinition(cvtermDefinition);
term.setVocabularyId(cvtermCvId);
terms.add(term);
}
}

} catch (final HibernateException e) {
this.logAndThrowException("Error at getTermByCvId=" + cvId + " query on CVTermDao: " + e.getMessage(), e);
}

return terms;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

package org.generationcp.middleware.domain.oms;

import java.io.Serializable;

import org.generationcp.middleware.pojos.oms.CVTerm;
import org.generationcp.middleware.util.Debug;

import java.io.Serializable;

/**
* Contains the details of a Term - id, vocabularyId, name, definition, nameSynonyms, obsolete.
*/
Expand All @@ -36,6 +36,10 @@ public class Term implements Serializable {
public Term() {
}

public Term(final int id) {
this.id = id;
}

public Term(int id, String name, String definition) {
this.id = id;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,12 @@ public enum VariableType {
}

private static final Map<Integer, VariableType> byId = new HashMap<>();
private static final Map<String, VariableType> byName = new HashMap<>();

static {
for (final VariableType e : VariableType.values()) {
if (VariableType.byId.put(e.getId(), e) != null) {
throw new IllegalArgumentException("duplicate id: " + e.getId());
}

if (VariableType.byName.put(e.getName(), e) != null) {
throw new IllegalArgumentException("duplicate name: " + e.getName());
}
}
}

Expand All @@ -94,10 +89,6 @@ public static VariableType getById(final Integer id) {
return VariableType.byId.get(id);
}

public static VariableType getByName(final String name) {
return VariableType.byName.get(name);
}

public static Set<Integer> getVariableTypesIdsByPhenotype(final PhenotypicType phenotype) {
final Set<Integer> variableTypes = new HashSet<>();
for (final VariableType variableType : VariableType.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Implements {@link OntologyVariableDataManagerImpl}
Expand Down Expand Up @@ -238,14 +239,8 @@ public Integer apply(final DataType x) {
// check of variable type list is not empty then get variables by variable types and add filter clause of it
if (!variableFilter.getVariableTypes().isEmpty()) {

final List<String> variableTypeNames =
Util.convertAll(variableFilter.getVariableTypes(), new Function<VariableType, String>() {

@Override
public String apply(final VariableType x) {
return x.getName();
}
});
final List<String> variableTypeNames = this.daoFactory.getCvTermDao().getTermByCvId(CvId.VARIABLE_TYPE.getId()).stream().map(Term::getName).collect(
Collectors.toList());

final SQLQuery vSQLQuery = this.getActiveSession()
.createSQLQuery("select cvterm_id from cvtermprop where type_id = 1800 and value in (:variableTypeNames)");
Expand Down Expand Up @@ -396,6 +391,9 @@ public String apply(final VariableType x) {
}
}


final Map<String, Integer> variableTypesByName = this.getVariableTypesByName();

// Fetch Property CropOntologyId, Scale min-max, Variable Type, Creation and Last Modified date of all terms
final SQLQuery pQuery = this.getActiveSession()
.createSQLQuery("select t.cvterm_id tid, t.cv_id cvid, tp.type_id typeid, tp.value value from cvtermprop tp "
Expand Down Expand Up @@ -423,7 +421,7 @@ public String apply(final VariableType x) {
if (Objects.equals(typeId, TermId.CROP_ONTOLOGY_ID.getId()) && Objects.equals(cvId, CvId.PROPERTIES.getId())) {
pMap.get(cvTermId).setCropOntologyId(value);
} else if (Objects.equals(typeId, TermId.VARIABLE_TYPE.getId()) && Objects.equals(cvId, CvId.VARIABLES.getId())) {
map.get(cvTermId).addVariableType(VariableType.getByName(value));
map.get(cvTermId).addVariableType(VariableType.getById(variableTypesByName.get(value)));
} else if (Objects.equals(typeId, TermId.MIN_VALUE.getId()) && Objects.equals(cvId, CvId.SCALES.getId())) {
sMap.get(cvTermId).setMinValue(value);
} else if (Objects.equals(typeId, TermId.MAX_VALUE.getId()) && Objects.equals(cvId, CvId.SCALES.getId())) {
Expand Down Expand Up @@ -503,10 +501,11 @@ public Variable getVariable(final String programUuid, final Integer id, final bo

// Variable Types, Created, modified from CVTermProperty
final List<CVTermProperty> properties = daoFactory.getCvTermPropertyDao().getByCvTermId(term.getCvTermId());
final Map<String, Integer> variableTypesByName = this.getVariableTypesByName();

for (final CVTermProperty property : properties) {
if (property.getTypeId() == TermId.VARIABLE_TYPE.getId()) {
final VariableType variableType = VariableType.getByName(property.getValue());
final VariableType variableType = VariableType.getById(variableTypesByName.get(property.getValue()));
variable.addVariableType(variableType);
if (variableType.equals(VariableType.TRAIT)) {
variable.setAllowsFormula(true);
Expand Down Expand Up @@ -588,6 +587,12 @@ public void addVariable(final OntologyVariableInfo variableInfo) {

final CVTerm term = daoFactory.getCvTermDao().getByNameAndCvId(variableInfo.getName(), CvId.VARIABLES.getId());

final List<Term> variableTypes = daoFactory.getCvTermDao().getTermByCvId(CvId.VARIABLE_TYPE.getId());
final Map<Integer, String> variableTypesById = new HashMap<>();
for (final Term varTypeTerm: variableTypes) {
variableTypesById.put(varTypeTerm.getId(), varTypeTerm.getName());
}

if (term != null) {
throw new MiddlewareException(OntologyVariableDataManagerImpl.VARIABLE_EXIST_WITH_SAME_NAME);
}
Expand Down Expand Up @@ -619,7 +624,7 @@ public void addVariable(final OntologyVariableInfo variableInfo) {
final CVTermProperty property = new CVTermProperty();
property.setCvTermId(variableInfo.getId());
property.setTypeId(TermId.VARIABLE_TYPE.getId());
property.setValue(type.getName());
property.setValue(variableTypesById.get(type.getId()));
property.setRank(rank++);
daoFactory.getCvTermPropertyDao().save(property);
}
Expand All @@ -646,6 +651,12 @@ public void addVariable(final OntologyVariableInfo variableInfo) {
@Override
public void updateVariable(final OntologyVariableInfo variableInfo) {

final List<Term> variableTypes = daoFactory.getCvTermDao().getTermByCvId(CvId.VARIABLE_TYPE.getId());
final Map<Integer, String> variableTypesById = new HashMap<>();
for (final Term varTypeTerm: variableTypes) {
variableTypesById.put(varTypeTerm.getId(), varTypeTerm.getName());
}

VariableCache.removeFromCache(variableInfo.getId());

final List<FormulaDto> formulas = this.formulaService.getByInputId(Integer.valueOf(variableInfo.getId()));
Expand Down Expand Up @@ -710,10 +721,12 @@ public void updateVariable(final OntologyVariableInfo variableInfo) {
final Map<VariableType, CVTermProperty> existingProperties = new HashMap<>();
final Set<VariableType> existingVariableTypes = new HashSet<>();

final Map<String, Integer> variableTypesByName = this.getVariableTypesByName();

// Variable Types from CVTermProperty
for (final CVTermProperty property : termProperties) {
if (Objects.equals(property.getTypeId(), TermId.VARIABLE_TYPE.getId())) {
final VariableType type = VariableType.getByName(property.getValue());
final VariableType type = VariableType.getById(variableTypesByName.get(property.getValue()));
existingVariableTypes.add(type);
existingProperties.put(type, property);
}
Expand All @@ -730,7 +743,7 @@ public void updateVariable(final OntologyVariableInfo variableInfo) {
final CVTermProperty property = new CVTermProperty();
property.setCvTermId(variableInfo.getId());
property.setTypeId(TermId.VARIABLE_TYPE.getId());
property.setValue(type.getName());
property.setValue(variableTypesById.get(type.getId()));
property.setRank(rank++);
daoFactory.getCvTermPropertyDao().save(property);
}
Expand Down Expand Up @@ -1000,10 +1013,11 @@ public VariableOverrides getVariableOverridesByVariableIdAndProgram(final Intege

@Override
public List<VariableType> getVariableTypes(final Integer variableId) {
final Map<String, Integer> variableTypesByName = this.getVariableTypesByName();
final List<VariableType> variableTypes = new ArrayList<>();
final List<CVTermProperty> properties = daoFactory.getCvTermPropertyDao().getByCvTermAndType(variableId, TermId.VARIABLE_TYPE.getId());
for (final CVTermProperty property : properties) {
variableTypes.add(VariableType.getByName(property.getValue()));
variableTypes.add(VariableType.getById(variableTypesByName.get(property.getValue())));
}
return variableTypes;
}
Expand Down Expand Up @@ -1047,4 +1061,13 @@ public List<VariableDTO> getAllVariables(final List<Integer> variableTypes,
public long countAllVariables(final List<Integer> variableTypes) {
return this.daoFactory.getCvTermDao().countAllVariables(variableTypes);
}

private Map<String, Integer> getVariableTypesByName(){
final List<Term> terms = this.daoFactory.getCvTermDao().getTermByCvId(CvId.VARIABLE_TYPE.getId());
final Map<String, Integer> variableTypesByName = new HashMap<>();
for (final Term term : terms) {
variableTypesByName.put(term.getName(), term.getId());
}
return variableTypesByName;
}
}
Loading