Search in sources :

Example 1 with CSVParseException

use of com.archimatetool.csv.CSVParseException in project archi by archimatetool.

the class CSVImportProvider method doImport.

@Override
public void doImport(IArchimateModel model) throws IOException {
    File elementsFile = askOpenFile();
    if (elementsFile == null) {
        return;
    }
    // Check file is valid
    if (!CSVImporter.isElementsFileName(elementsFile)) {
        throw new IOException(Messages.CSVImportProvider_0);
    }
    // Import
    try {
        CSVImporter importer = new CSVImporter(model);
        importer.doImport(elementsFile);
    } catch (CSVParseException ex) {
        throw new IOException(ex.getMessage());
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) CSVParseException(com.archimatetool.csv.CSVParseException)

Example 2 with CSVParseException

use of com.archimatetool.csv.CSVParseException in project archi by archimatetool.

the class CSVImporter method createPropertyFromRecord.

/**
 * Create a Property from a given CSVRecord
 */
private void createPropertyFromRecord(CSVRecord csvRecord) throws CSVParseException {
    // ID
    String id = csvRecord.get(0);
    if (!StringUtils.isSet(id)) {
        throw new CSVParseException(Messages.CSVImporter_6);
    } else {
        checkIDForInvalidCharacters(id);
    }
    // Find referenced concept in newly created list
    IProperties propertiesObject = newConcepts.get(id);
    // Not found, check if it's referencing an existing element in the model
    if (propertiesObject == null) {
        EObject eObject = ArchimateModelUtils.getObjectByID(fModel, id);
        if (eObject instanceof IProperties) {
            propertiesObject = (IProperties) eObject;
        }
    }
    // Not found, check if it's referencing the model
    if (propertiesObject == null && id.equals(modelID)) {
        propertiesObject = fModel;
    }
    // Not found at all
    if (propertiesObject == null) {
        throw new CSVParseException(Messages.CSVImporter_7 + id);
    }
    String key = normalise(csvRecord.get(1));
    String value = normalise(csvRecord.get(2));
    // Special properties for relationship attributes
    if (INFLUENCE_STRENGTH.equals(key) && propertiesObject instanceof IInfluenceRelationship) {
        storeUpdatedConceptFeature((IArchimateConcept) propertiesObject, IArchimatePackage.Literals.INFLUENCE_RELATIONSHIP__STRENGTH, value);
        return;
    } else if (ACCESS_TYPE.equals(key) && propertiesObject instanceof IAccessRelationship) {
        int newvalue = ACCESS_TYPES.indexOf(value);
        storeUpdatedConceptFeature((IArchimateConcept) propertiesObject, IArchimatePackage.Literals.ACCESS_RELATIONSHIP__ACCESS_TYPE, newvalue);
        return;
    }
    // Is there already a property with this key?
    IProperty property = getProperty(propertiesObject, key);
    if (property != null) {
        updatedProperties.put(property, value);
    } else // No, create new one
    {
        property = IArchimateFactory.eINSTANCE.createProperty();
        property.setKey(key);
        property.setValue(value);
        newProperties.put(property, propertiesObject);
    }
}
Also used : IInfluenceRelationship(com.archimatetool.model.IInfluenceRelationship) IProperty(com.archimatetool.model.IProperty) EObject(org.eclipse.emf.ecore.EObject) IArchimateConcept(com.archimatetool.model.IArchimateConcept) IProperties(com.archimatetool.model.IProperties) IAccessRelationship(com.archimatetool.model.IAccessRelationship) CSVParseException(com.archimatetool.csv.CSVParseException)

Example 3 with CSVParseException

use of com.archimatetool.csv.CSVParseException in project archi by archimatetool.

the class CSVImporter method createRelationFromRecord.

/**
 * Create an Archimate relationship from a given CSVRecord
 */
private void createRelationFromRecord(CSVRecord csvRecord) throws CSVParseException {
    // ID
    String id = csvRecord.get(0);
    if (!StringUtils.isSet(id)) {
        id = generateID();
    } else {
        checkIDForInvalidCharacters(id);
    }
    // Type
    String type = csvRecord.get(1);
    EClass eClass = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(type);
    if (!isArchimateRelationshipEClass(eClass)) {
        throw new CSVParseException(Messages.CSVImporter_4 + id);
    }
    String name = normalise(csvRecord.get(2));
    String documentation = csvRecord.get(3);
    // Is the relation already in the model?
    IArchimateRelationship relation = (IArchimateRelationship) findArchimateConceptInModel(id, eClass);
    // Yes it is, so updated values
    if (relation != null) {
        storeUpdatedConceptFeature(relation, IArchimatePackage.Literals.NAMEABLE__NAME, name);
        storeUpdatedConceptFeature(relation, IArchimatePackage.Literals.DOCUMENTABLE__DOCUMENTATION, documentation);
    } else // No, create a new one
    {
        relation = (IArchimateRelationship) IArchimateFactory.eINSTANCE.create(eClass);
        relation.setId(id);
        relation.setName(name);
        relation.setDocumentation(documentation);
        // Get source and target ids and temporarily store their ids
        String sourceID = csvRecord.get(4);
        // $NON-NLS-1$
        relation.setAdapter("sourceID", sourceID);
        String targetID = csvRecord.get(5);
        // $NON-NLS-1$
        relation.setAdapter("targetID", targetID);
        newConcepts.put(id, relation);
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) IArchimateRelationship(com.archimatetool.model.IArchimateRelationship) CSVParseException(com.archimatetool.csv.CSVParseException)

Example 4 with CSVParseException

use of com.archimatetool.csv.CSVParseException in project archi by archimatetool.

the class CSVImporter method createElementFromRecord.

/**
 * Create an Archimate Element from a given CSVRecord
 */
private void createElementFromRecord(CSVRecord csvRecord) throws CSVParseException {
    // ID
    String id = csvRecord.get(0);
    if (!StringUtils.isSet(id)) {
        id = generateID();
    } else {
        checkIDForInvalidCharacters(id);
    }
    // Class type
    String type = csvRecord.get(1);
    EClass eClass = (EClass) IArchimatePackage.eINSTANCE.getEClassifier(type);
    // Can only be Archimate element type
    if (!isArchimateElementEClass(eClass)) {
        throw new CSVParseException(Messages.CSVImporter_3);
    }
    String name = normalise(csvRecord.get(2));
    String documentation = csvRecord.get(3);
    // Is the element already in the model?
    IArchimateElement element = (IArchimateElement) findArchimateConceptInModel(id, eClass);
    // Yes it is, so update values
    if (element != null) {
        storeUpdatedConceptFeature(element, IArchimatePackage.Literals.NAMEABLE__NAME, name);
        storeUpdatedConceptFeature(element, IArchimatePackage.Literals.DOCUMENTABLE__DOCUMENTATION, documentation);
    } else // No, create a new element
    {
        element = (IArchimateElement) IArchimateFactory.eINSTANCE.create(eClass);
        element.setId(id);
        element.setName(name);
        element.setDocumentation(documentation);
        newConcepts.put(id, element);
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) IArchimateElement(com.archimatetool.model.IArchimateElement) CSVParseException(com.archimatetool.csv.CSVParseException)

Example 5 with CSVParseException

use of com.archimatetool.csv.CSVParseException in project archi by archimatetool.

the class CSVImporter method importRelations.

// -------------------------------- Import Relations --------------------------------
/**
 * Import Relations from CSV file
 * @param file The file to import
 * @throws IOException
 * @throws CSVParseException
 */
void importRelations(File file) throws IOException, CSVParseException {
    for (CSVRecord csvRecord : getRecords(file)) {
        if (!isRelationsRecordCorrectSize(csvRecord)) {
            throw new CSVParseException(Messages.CSVImporter_2);
        }
        // Header
        if (isHeaderRecord(csvRecord, RELATIONSHIPS_HEADER)) {
            continue;
        } else // Relation
        {
            createRelationFromRecord(csvRecord);
        }
    }
    // Now connect the relations
    for (Entry<String, IArchimateConcept> entry : newConcepts.entrySet()) {
        if (entry.getValue() instanceof IArchimateRelationship) {
            IArchimateRelationship relation = (IArchimateRelationship) entry.getValue();
            // Get the ids from the temporary stores
            // $NON-NLS-1$
            IArchimateConcept source = findReferencedConcept((String) relation.getAdapter("sourceID"));
            // $NON-NLS-1$
            IArchimateConcept target = findReferencedConcept((String) relation.getAdapter("targetID"));
            // Is it a valid relationship?
            if (!ArchimateModelUtils.isValidRelationship(source.eClass(), target.eClass(), relation.eClass())) {
                throw new CSVParseException(Messages.CSVImporter_5 + relation.getId());
            }
            // Connect
            relation.connect(source, target);
        }
    }
}
Also used : IArchimateConcept(com.archimatetool.model.IArchimateConcept) CSVRecord(org.apache.commons.csv.CSVRecord) IArchimateRelationship(com.archimatetool.model.IArchimateRelationship) CSVParseException(com.archimatetool.csv.CSVParseException)

Aggregations

CSVParseException (com.archimatetool.csv.CSVParseException)5 IArchimateConcept (com.archimatetool.model.IArchimateConcept)2 IArchimateRelationship (com.archimatetool.model.IArchimateRelationship)2 EClass (org.eclipse.emf.ecore.EClass)2 IAccessRelationship (com.archimatetool.model.IAccessRelationship)1 IArchimateElement (com.archimatetool.model.IArchimateElement)1 IInfluenceRelationship (com.archimatetool.model.IInfluenceRelationship)1 IProperties (com.archimatetool.model.IProperties)1 IProperty (com.archimatetool.model.IProperty)1 File (java.io.File)1 IOException (java.io.IOException)1 CSVRecord (org.apache.commons.csv.CSVRecord)1 EObject (org.eclipse.emf.ecore.EObject)1