use of org.openforis.idm.metamodel.Schema in project collect by openforis.
the class SurveyBuilder method survey.
public static Survey survey(NodeDefinitionBuilder... builders) {
String rootEntityName = "root";
Survey survey = new TestSurveyContext().createSurvey();
EntityDefinitionBuilder entityBuilder = new EntityDefinitionBuilder(rootEntityName, builders);
EntityDefinition rootEntityDef = (EntityDefinition) entityBuilder.buildInternal(survey);
Schema schema = survey.getSchema();
if (schema.getRootEntityDefinition(rootEntityName) != null) {
schema.removeRootEntityDefinition(rootEntityName);
}
schema.addRootEntityDefinition(rootEntityDef);
survey.refreshSurveyDependencies();
return survey;
}
use of org.openforis.idm.metamodel.Schema in project collect by openforis.
the class ProtostuffSerializationTest method testSkipRemovedAttribute.
@Test
public void testSkipRemovedAttribute() throws Exception {
// Set up
Survey survey = getTestSurvey();
// assignFakeNodeDefinitionIds(survey.getSchema());
Record record1 = createTestRecord(survey);
Entity cluster1 = record1.getRootEntity();
// Write
ModelSerializer ser = new ModelSerializer(10000);
byte[] data = ser.toByteArray(cluster1);
// remove attribute from record before comparing it with the new one
cluster1.remove("crew_no", 0);
// remove node definition from schema
Schema schema = survey.getSchema();
EntityDefinition clusterDefn = schema.getRootEntityDefinition("cluster");
NodeDefinition crewNumDefn = clusterDefn.getChildDefinition("crew_no");
clusterDefn.removeChildDefinition(crewNumDefn);
Record record2 = new Record(survey, "2.0", "cluster");
ser.mergeFrom(data, record2.getRootEntity());
// Compare
Assert.assertTrue(record1.getRootEntity().deepEquals(record2.getRootEntity()));
}
use of org.openforis.idm.metamodel.Schema in project collect by openforis.
the class ProtostuffSerializationTest method testSkipRemovedEntity.
@Test
public void testSkipRemovedEntity() throws Exception {
// Set up
Survey survey = getTestSurvey();
// assignFakeNodeDefinitionIds(survey.getSchema());
Record record1 = createTestRecord(survey);
Entity cluster1 = record1.getRootEntity();
// Write
ModelSerializer ser = new ModelSerializer(10000);
byte[] data = ser.toByteArray(cluster1);
// remove data
cluster1.remove("map_sheet", 1);
cluster1.remove("map_sheet", 0);
Schema schema = survey.getSchema();
EntityDefinition clusterDefn = schema.getRootEntityDefinition("cluster");
NodeDefinition mapSheetDefn = clusterDefn.getChildDefinition("map_sheet");
clusterDefn.removeChildDefinition(mapSheetDefn);
Record record2 = new Record(survey, "2.0", "cluster");
ser.mergeFrom(data, record2.getRootEntity());
// Compare
Assert.assertTrue(record1.getRootEntity().deepEquals(record2.getRootEntity()));
}
use of org.openforis.idm.metamodel.Schema in project collect by openforis.
the class CodeListsVM method getReferences.
protected List<NodeDefinition> getReferences(CodeList item) {
List<NodeDefinition> references = new ArrayList<NodeDefinition>();
Schema schema = survey.getSchema();
List<EntityDefinition> rootEntities = schema.getRootEntityDefinitions();
Stack<NodeDefinition> stack = new Stack<NodeDefinition>();
stack.addAll(rootEntities);
while (!stack.isEmpty()) {
NodeDefinition defn = stack.pop();
if (defn instanceof EntityDefinition) {
stack.addAll(((EntityDefinition) defn).getChildDefinitions());
} else if (defn instanceof CodeAttributeDefinition) {
CodeList list = ((CodeAttributeDefinition) defn).getList();
if (list.equals(item)) {
references.add(defn);
}
}
;
}
return references;
}
use of org.openforis.idm.metamodel.Schema in project collect by openforis.
the class RecordController method loadRecordSummaries.
@RequestMapping(value = "survey/{surveyId}/data/records/summary", method = GET)
@ResponseBody
public Map<String, Object> loadRecordSummaries(@PathVariable("surveyId") int surveyId, @Valid RecordSummarySearchParameters params) {
CollectSurvey survey = surveyManager.getOrLoadSurveyById(surveyId);
User user = loadUser(params.getUserId(), params.getUsername());
Map<String, Object> result = new HashMap<String, Object>();
Schema schema = survey.getSchema();
EntityDefinition rootEntityDefinition = params.getRootEntityName() == null ? schema.getFirstRootEntityDefinition() : schema.getRootEntityDefinition(params.getRootEntityName());
RecordFilter filter = createRecordFilter(survey, user, userGroupManager, rootEntityDefinition.getId(), false);
filter.setKeyValues(params.getKeyValues());
filter.setCaseSensitiveKeyValues(params.isCaseSensitiveKeyValues());
if (CollectionUtils.isEmpty(filter.getQualifiers())) {
// filter by qualifiers only if not already done by user group qualifiers
filter.setQualifiers(params.getQualifierValues());
}
filter.setSummaryValues(params.getSummaryValues());
if (filter.getOwnerIds() == null && params.getOwnerIds() != null && params.getOwnerIds().length > 0) {
filter.setOwnerIds(Arrays.asList(params.getOwnerIds()));
}
filter.setOffset(params.getOffset());
filter.setMaxNumberOfRecords(params.getMaxNumberOfRows());
// load summaries
List<CollectRecordSummary> summaries = params.isFullSummary() ? recordManager.loadFullSummaries(filter, params.getSortFields()) : recordManager.loadSummaries(filter, params.getSortFields());
result.put("records", toProxies(summaries));
// count total records
int count = recordManager.countRecords(filter);
result.put("count", count);
if (params.isIncludeOwners()) {
Set<User> owners = recordManager.loadDistinctOwners(createRecordFilter(survey, user, userGroupManager, rootEntityDefinition.getId(), false));
Set<BasicUserProxy> ownerProxies = Proxies.fromSet(owners, BasicUserProxy.class);
result.put("owners", ownerProxies);
}
return result;
}
Aggregations