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

Removed some dependencies to name #391

Open
wants to merge 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.generationcp.middleware.domain.dms.DatasetDTO;
import org.generationcp.middleware.domain.dms.DatasetTypeDTO;
import org.generationcp.middleware.domain.dms.Study;
import org.generationcp.middleware.domain.oms.CvId;
import org.generationcp.middleware.domain.oms.Term;
import org.generationcp.middleware.manager.api.OntologyDataManager;
import org.generationcp.middleware.manager.api.StudyDataManager;
import org.generationcp.middleware.service.api.dataset.DatasetService;
import org.generationcp.middleware.service.api.dataset.DatasetTypeService;
Expand Down Expand Up @@ -39,25 +42,23 @@ public class DatasetGeneratorInputValidator {
@Autowired
private DatasetService studyDatasetService;

@Autowired
private OntologyDataManager ontologyDataManager;

@Autowired
private Environment environment;

private Integer maxAllowedSubobservationUnits;

private Integer maxAllowedDatasetsPerParent;

private final VariableType observationUnitVariableType;
private VariableType observationUnitVariableType;

private static final String DATASET_NAME_REGEX = "^[a-zA-Z0-9\\s(\\\\/:*?\\\"\"<>|.)]*$";

private static final Pattern DATASET_NAME_PATTERN = Pattern.compile(DatasetGeneratorInputValidator.DATASET_NAME_REGEX);

DatasetGeneratorInputValidator() {
this.observationUnitVariableType =
new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());
}

@PostConstruct
Expand Down Expand Up @@ -120,6 +121,15 @@ public Integer apply(final StudyInstance i) {

final Study study = this.studyDataManager.getStudy(studyId);
try {
final Term observationUnitTerm = this.ontologyDataManager.getAllTermsByCvId(CvId.VARIABLE_TYPE).stream()
.filter(f -> org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().equals(f.getId())).findFirst()
.get();
this.observationUnitVariableType =
new VariableType(
String.valueOf(observationUnitTerm.getId()),
observationUnitTerm.getName(),
observationUnitTerm.getDefinition());

final VariableDetails variableDetails =
this.variableService
.getVariableById(crop, study.getProgramUUID(), String.valueOf(datasetInputGenerator.getSequenceVariableId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ void validateIfDatasetVariableAlreadyExists(
// If variable was found, check it is a supported variable type for dataset
} else if (this.isInvalidVariableTypeForDatasetType(datasetType, variableType)) {
this.errors
.reject("dataset.variable.cannot.be.deleted", new Object[] {String.valueOf(variableId), variableType.getName()},
.reject("dataset.variable.cannot.be.deleted",
new Object[] {String.valueOf(variableId), ontologyDataManager.getTermById(variableType.getId()).getName()},
"");
throw new NotSupportedException(this.errors.getAllErrors().get(0));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@

package org.ibp.api.java.impl.middleware.ontology;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import com.google.common.base.Function;
import org.generationcp.middleware.domain.oms.CvId;
import org.generationcp.middleware.domain.oms.Term;
import org.generationcp.middleware.exceptions.MiddlewareException;
Expand All @@ -22,7 +16,12 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Function;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
Expand Down Expand Up @@ -88,15 +87,11 @@ public int compare(String str1, String str2) {
@Override
public List<VariableType> getAllVariableTypes() {

final List<Term> variableTypeTerms = this.termDataManager.getTermByCvId(CvId.VARIABLE_TYPE.getId());

List<VariableType> variableTypes =
Util.convertAll(Arrays.asList(org.generationcp.middleware.domain.ontology.VariableType.values()),
new Function<org.generationcp.middleware.domain.ontology.VariableType, VariableType>() {

@Override
public VariableType apply(org.generationcp.middleware.domain.ontology.VariableType variableType) {
return new VariableType(String.valueOf(variableType.getId()), variableType.getName(), variableType.getDescription());
}
});
Util.convertAll(variableTypeTerms,
term -> new VariableType(String.valueOf(term.getId()), term.getName(), term.getDefinition()));

Collections.sort(variableTypes, new Comparator<VariableType>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ private boolean areAllPreviousVariableTypesPresent(Set<org.generationcp.middlewa
boolean found = false;

for (VariableType type : currentTypeList) {
if (type.getName().equals(variableType.getName())) {
if (type.getId().equals(variableType.getId())) {
found = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package org.ibp.api.java.impl.middleware.dataset.validator;

import com.google.common.collect.Lists;
import org.generationcp.middleware.domain.dms.DatasetDTO;
import org.generationcp.middleware.domain.dms.DatasetTypeDTO;
import org.generationcp.middleware.domain.dms.Study;
import org.generationcp.middleware.domain.oms.CvId;
import org.generationcp.middleware.domain.oms.Term;
import org.generationcp.middleware.enumeration.DatasetTypeEnum;
import org.generationcp.middleware.manager.OntologyDataManagerImpl;
import org.generationcp.middleware.manager.api.OntologyDataManager;
import org.generationcp.middleware.manager.api.StudyDataManager;
import org.generationcp.middleware.service.api.dataset.DatasetService;
import org.generationcp.middleware.service.api.dataset.DatasetTypeService;
Expand All @@ -20,6 +25,7 @@
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
Expand Down Expand Up @@ -59,6 +65,11 @@ public class DatasetGeneratorInputValidatorTest {
@Mock
final Environment environment = new StandardEnvironment();

@Mock
final OntologyDataManager ontologyDataManager = new OntologyDataManagerImpl();

private VariableType variableType;

@Before
public void setup() {

Expand All @@ -77,6 +88,12 @@ public void setup() {
meansDatasetType.setObservationType(false);
when(this.datasetTypeService.getDatasetTypeById(DatasetTypeEnum.MEANS_DATA.getId())).thenReturn(meansDatasetType);

final Term observationUnitTerm =
new Term(org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId(), "OBS", "Desc");
variableType =
new VariableType(String.valueOf(org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId()), "OBS",
"Desc");
Mockito.when(this.ontologyDataManager.getAllTermsByCvId(CvId.VARIABLE_TYPE)).thenReturn(Lists.newArrayList(observationUnitTerm));
}

@Test
Expand Down Expand Up @@ -161,10 +178,6 @@ public void testValidateBasicData() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -215,10 +228,6 @@ public void testValidateBasicDataDatasetTypeInexistent() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -262,10 +271,6 @@ public void testValidateBasicDataDatasetTypeIsObservation() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -310,10 +315,6 @@ public void testValidateBasicDataMaxAllowed() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -358,10 +359,6 @@ public void testValidateBasicDataLongName() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -411,10 +408,6 @@ public void testValidateBasicDataEmptyName() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -463,10 +456,6 @@ public void testValidateBasicDataInvalidVariable() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -516,10 +505,6 @@ public void testValidateBasicDataMaxAllowedSubObservations() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -572,10 +557,6 @@ public void testValidateBasicDataInvalidInstance() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -623,10 +604,6 @@ public void testValidateDataSpecialCharacters() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down Expand Up @@ -671,10 +648,6 @@ public void testValidateDataSpecialCharactersError() {
final Integer parentId = random.nextInt();
final DatasetGeneratorInput datasetInputGenerator = new DatasetGeneratorInput();
final VariableDetails variableDetails = TestDataProvider.getTestVariableDetails();
final VariableType variableType = new VariableType(
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getId().toString(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getName(),
org.generationcp.middleware.domain.ontology.VariableType.OBSERVATION_UNIT.getDescription());

final int studyId = random.nextInt();
final String program = "MAIZE-Program";
Expand Down