use of org.openforis.collect.model.NodeChangeSet in project collect by openforis.
the class RecordGenerator method addSecondLevelEntities.
private void addSecondLevelEntities(CollectRecord record, RecordKey recordKey) {
CollectSurvey survey = (CollectSurvey) record.getSurvey();
List<AttributeDefinition> nonMeasurementKeyDefs = getNonMeasurementKeyDefs(survey);
List<String> keyValues = recordKey.getValues(nonMeasurementKeyDefs);
List<SamplingDesignItem> secondLevelSamplingPointItems = samplingDesignManager.loadChildItems(survey.getId(), keyValues);
List<CodeAttributeDefinition> samplingPointDataCodeAttributeDefs = findSamplingPointCodeAttributes(survey);
if (!secondLevelSamplingPointItems.isEmpty() && samplingPointDataCodeAttributeDefs.size() > 1) {
int levelIndex = 1;
for (SamplingDesignItem samplingDesignItem : secondLevelSamplingPointItems) {
CodeAttributeDefinition levelKeyDef = samplingPointDataCodeAttributeDefs.get(levelIndex);
EntityDefinition levelEntityDef = levelKeyDef.getParentEntityDefinition();
Entity parentLevelEntity = record.getRootEntity();
NodeChangeSet addEntityChangeSet = recordUpdater.addEntity(parentLevelEntity, levelEntityDef);
Entity entity = getAddedEntity(addEntityChangeSet);
CodeAttribute keyAttr = entity.getChild(levelKeyDef);
recordUpdater.updateAttribute(keyAttr, new Code(samplingDesignItem.getLevelCode(levelIndex + 1)));
}
}
}
use of org.openforis.collect.model.NodeChangeSet in project collect by openforis.
the class RecordManager method addQualifierValues.
private void addQualifierValues(CollectRecord record, User user) {
if (userGroupManager == null) {
return;
}
NodeChangeMap changeSet = new NodeChangeMap();
CollectSurvey survey = (CollectSurvey) record.getSurvey();
UserGroup surveyUserGrup = survey.getUserGroup();
UserInGroup userInGroup = userGroupManager.findUserInGroupOrDescendants(surveyUserGrup, user);
if (userInGroup == null) {
throw new IllegalArgumentException(String.format("User %s is not allowed to create records for survey %s", user.getUsername(), survey.getName()));
}
UserGroup group = userGroupManager.loadById(userInGroup.getGroupId());
Map<String, String> qualifiersByName = group.getQualifiersByName();
for (Entry<String, String> qualifier : qualifiersByName.entrySet()) {
String attributePath = record.getRootEntity().getName() + "/" + qualifier.getKey();
Attribute<?, Value> attribute = record.findNodeByPath(attributePath);
Value qualifierValue = attribute.getDefinition().createValue(qualifier.getValue());
NodeChangeSet changes = updater.updateAttribute(attribute, qualifierValue);
changeSet.addMergeChanges(changes);
}
}
use of org.openforis.collect.model.NodeChangeSet in project collect by openforis.
the class CSVDataImportProcess method setValuesInAttributes.
private void setValuesInAttributes(Entity ancestorEntity, Map<FieldValueKey, String> fieldValues, Map<FieldValueKey, String> colNameByField, long row) {
Set<Entry<FieldValueKey, String>> entrySet = fieldValues.entrySet();
// delete all multiple attributes
for (Entry<FieldValueKey, String> entry : entrySet) {
FieldValueKey fieldValueKey = entry.getKey();
EntityDefinition ancestorDefn = ancestorEntity.getDefinition();
Schema schema = ancestorDefn.getSchema();
AttributeDefinition attrDefn = (AttributeDefinition) schema.getDefinitionById(fieldValueKey.getAttributeDefinitionId());
Entity parentEntity = getOrCreateParentEntity(ancestorEntity, attrDefn);
if (attrDefn.isMultiple()) {
List<Node<?>> attributes = parentEntity.getChildren(attrDefn);
int tot = attributes.size();
for (int i = 0; i < tot; i++) {
Node<?> node = attributes.get(0);
NodeChangeSet changes = recordUpdater.deleteNode(node);
if (nodeChangeBatchProcessor != null) {
nodeChangeBatchProcessor.add(changes, adminUser.getUsername());
}
}
}
}
// set values
for (Entry<FieldValueKey, String> entry : entrySet) {
FieldValueKey fieldValueKey = entry.getKey();
String strValue = entry.getValue();
EntityDefinition ancestorDefn = ancestorEntity.getDefinition();
Schema schema = ancestorDefn.getSchema();
AttributeDefinition attrDefn = (AttributeDefinition) schema.getDefinitionById(fieldValueKey.getAttributeDefinitionId());
String fieldName = fieldValueKey.getFieldName();
Entity parentEntity = getOrCreateParentEntity(ancestorEntity, attrDefn);
String colName = colNameByField.get(fieldValueKey);
int attrPos = fieldValueKey.getAttributePosition();
setValueInField(parentEntity, attrDefn, attrPos - 1, fieldName, strValue, colName, row);
}
}
use of org.openforis.collect.model.NodeChangeSet in project collect by openforis.
the class CSVDataImportProcess method processLine.
private void processLine(DataLine line) throws RecordPersistenceException {
if (!validateRecordKey(line)) {
return;
}
if (settings.isInsertNewRecords()) {
// create new record
EntityDefinition rootEntityDefn = survey.getSchema().getRootEntityDefinition(parentEntityDefinitionId);
CollectRecord record = recordManager.instantiateRecord(survey, rootEntityDefn.getName(), adminUser, settings.getNewRecordVersionName(), Step.ENTRY);
NodeChangeSet changes = recordManager.initializeRecord(record);
if (nodeChangeBatchProcessor != null) {
nodeChangeBatchProcessor.add(changes, adminUser.getUsername());
}
setRecordKeys(line, record);
setValuesInRecord(line, record, Step.ENTRY);
insertRecord(record);
} else {
CollectRecordSummary recordSummary = loadRecordSummaryIfAny(line);
if (recordSummary != null) {
Step originalRecordStep = recordSummary.getStep();
if (step == null) {
// set values in each step data
for (Step currentStep : Step.values()) {
if (currentStep.beforeEqual(originalRecordStep)) {
CollectRecord record = loadRecord(recordSummary.getId(), currentStep);
setValuesInRecord(line, record, currentStep);
// always save record when updating multiple record steps in the same process
updateRecord(record, originalRecordStep, currentStep);
}
}
} else {
if (step.beforeEqual(originalRecordStep)) {
CollectRecord record;
boolean recordChanged = lastModifiedRecordSummary == null || !recordSummary.getId().equals(lastModifiedRecordSummary.getId());
if (recordChanged) {
// record changed
if (lastModifiedRecordSummary != null) {
saveLastModifiedRecord();
}
record = loadRecord(recordSummary.getId(), this.step);
} else {
record = lastModifiedRecord;
}
setValuesInRecord(line, record, step);
lastModifiedRecordSummary = recordSummary;
lastModifiedRecord = record;
} else {
status.addParsingError(new ParsingError(ErrorType.INVALID_VALUE, line.getLineNumber(), (String) null, RECORD_NOT_IN_SELECTED_STEP_MESSAGE_KEY));
}
}
}
}
status.addProcessedRow(line.getLineNumber());
}
use of org.openforis.collect.model.NodeChangeSet in project collect by openforis.
the class CSVDataImportProcess method deleteAllParentEntities.
private void deleteAllParentEntities(CollectRecord record) {
String parentEntitiesPath = getParentEntityDefinition().getPath();
List<Entity> entitiesToBeDeleted = record.findNodesByPath(parentEntitiesPath);
for (Entity entity : entitiesToBeDeleted) {
NodeChangeSet changes = recordUpdater.deleteNode(entity);
if (nodeChangeBatchProcessor != null) {
nodeChangeBatchProcessor.add(changes, adminUser.getUsername());
}
}
}
Aggregations