Search in sources :

Example 6 with NodeChangeSet

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)));
        }
    }
}
Also used : Entity(org.openforis.idm.model.Entity) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) Code(org.openforis.idm.model.Code) SamplingDesignItem(org.openforis.collect.model.SamplingDesignItem) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CodeAttributeDefinition(org.openforis.idm.metamodel.CodeAttributeDefinition) CodeAttribute(org.openforis.idm.model.CodeAttribute) CollectSurvey(org.openforis.collect.model.CollectSurvey)

Example 7 with NodeChangeSet

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);
    }
}
Also used : NodeChangeSet(org.openforis.collect.model.NodeChangeSet) UserInGroup(org.openforis.collect.model.UserInGroup) Value(org.openforis.idm.model.Value) NodeChangeMap(org.openforis.collect.model.NodeChangeMap) CollectSurvey(org.openforis.collect.model.CollectSurvey) UserGroup(org.openforis.collect.model.UserGroup)

Example 8 with NodeChangeSet

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);
    }
}
Also used : Entity(org.openforis.idm.model.Entity) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) FieldValueKey(org.openforis.collect.io.data.DataLine.FieldValueKey) Schema(org.openforis.idm.metamodel.Schema) Node(org.openforis.idm.model.Node) NumericAttributeDefinition(org.openforis.idm.metamodel.NumericAttributeDefinition) CoordinateAttributeDefinition(org.openforis.idm.metamodel.CoordinateAttributeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) NumberAttributeDefinition(org.openforis.idm.metamodel.NumberAttributeDefinition) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entry(java.util.Map.Entry)

Example 9 with NodeChangeSet

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());
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) CollectRecord(org.openforis.collect.model.CollectRecord) NodeChangeSet(org.openforis.collect.model.NodeChangeSet) ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) CollectRecordSummary(org.openforis.collect.model.CollectRecordSummary) Step(org.openforis.collect.model.CollectRecord.Step)

Example 10 with NodeChangeSet

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());
        }
    }
}
Also used : Entity(org.openforis.idm.model.Entity) NodeChangeSet(org.openforis.collect.model.NodeChangeSet)

Aggregations

NodeChangeSet (org.openforis.collect.model.NodeChangeSet)14 CollectRecord (org.openforis.collect.model.CollectRecord)7 EventProducer (org.openforis.collect.event.EventProducer)6 RecordEvent (org.openforis.collect.event.RecordEvent)6 CollectSurvey (org.openforis.collect.model.CollectSurvey)6 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)4 Entity (org.openforis.idm.model.Entity)4 Value (org.openforis.idm.model.Value)4 ParsingError (org.openforis.collect.io.metadata.parsing.ParsingError)3 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)3 NodeChangeMap (org.openforis.collect.model.NodeChangeMap)2 User (org.openforis.collect.model.User)2 MultipleEditException (org.openforis.collect.persistence.MultipleEditException)2 NumericAttributeDefinition (org.openforis.idm.metamodel.NumericAttributeDefinition)2 Survey (org.openforis.idm.metamodel.Survey)2 Secured (org.springframework.security.access.annotation.Secured)2 Entry (java.util.Map.Entry)1 FieldValueKey (org.openforis.collect.io.data.DataLine.FieldValueKey)1 RecordIndexException (org.openforis.collect.manager.RecordIndexException)1 RecordPromoteException (org.openforis.collect.manager.RecordPromoteException)1