Search in sources :

Example 16 with TagDefinition

use of de.catma.tag.TagDefinition in project catma by forTEXT.

the class TagConflict method createCombinedTagDefinition.

private TagDefinition createCombinedTagDefinition() {
    TagDefinition tagDefinition = new TagDefinition(masterTagDefinition);
    for (PropertyDefinition devPropertyDef : devTagDefinition.getUserDefinedPropertyDefinitions()) {
        PropertyDefinition combinedPropertyDef = tagDefinition.getPropertyDefinitionByUuid(devPropertyDef.getUuid());
        if (combinedPropertyDef == null) {
            combinedPropertyDef = new PropertyDefinition(devPropertyDef);
            tagDefinition.addUserDefinedPropertyDefinition(combinedPropertyDef);
        } else {
            for (String value : devPropertyDef.getPossibleValueList()) {
                if (!combinedPropertyDef.getPossibleValueList().contains(value)) {
                    combinedPropertyDef.addValue(value);
                }
            }
        }
    }
    return tagDefinition;
}
Also used : TagDefinition(de.catma.tag.TagDefinition) PropertyDefinition(de.catma.tag.PropertyDefinition)

Example 17 with TagDefinition

use of de.catma.tag.TagDefinition in project catma by forTEXT.

the class GraphWorktreeProject method initTagManagerListeners.

private void initTagManagerListeners() {
    tagsetDefinitionChangedListener = new PropertyChangeListener() {

        public void propertyChange(final PropertyChangeEvent evt) {
            if (!tagManagerListenersEnabled) {
                return;
            }
            try {
                if (evt.getOldValue() == null) {
                    // insert
                    final TagsetDefinition tagsetDefinition = (TagsetDefinition) evt.getNewValue();
                    addTagsetDefinition(tagsetDefinition);
                } else if (evt.getNewValue() == null) {
                    // delete
                    final TagsetDefinition tagsetDefinition = (TagsetDefinition) evt.getOldValue();
                    removeTagsetDefinition(tagsetDefinition);
                } else {
                    // update
                    final TagsetDefinition tagsetDefinition = (TagsetDefinition) evt.getNewValue();
                    updateTagsetDefinition(tagsetDefinition);
                }
            } catch (Exception e) {
                propertyChangeSupport.firePropertyChange(RepositoryChangeEvent.exceptionOccurred.name(), null, e);
            }
        }
    };
    tagManager.addPropertyChangeListener(TagManagerEvent.tagsetDefinitionChanged, tagsetDefinitionChangedListener);
    tagDefinitionChangedListener = new PropertyChangeListener() {

        public void propertyChange(final PropertyChangeEvent evt) {
            if (!tagManagerListenersEnabled) {
                return;
            }
            try {
                if (evt.getOldValue() == null) {
                    @SuppressWarnings("unchecked") final Pair<TagsetDefinition, TagDefinition> args = (Pair<TagsetDefinition, TagDefinition>) evt.getNewValue();
                    TagsetDefinition tagsetDefinition = args.getFirst();
                    TagDefinition tagDefinition = args.getSecond();
                    addTagDefinition(tagDefinition, tagsetDefinition);
                } else if (evt.getNewValue() == null) {
                    @SuppressWarnings("unchecked") final Pair<TagsetDefinition, TagDefinition> args = (Pair<TagsetDefinition, TagDefinition>) evt.getOldValue();
                    TagsetDefinition tagsetDefinition = args.getFirst();
                    TagDefinition tagDefinition = args.getSecond();
                    removeTagDefinition(tagDefinition, tagsetDefinition);
                } else {
                    TagDefinition tag = (TagDefinition) evt.getNewValue();
                    TagsetDefinition tagset = (TagsetDefinition) evt.getOldValue();
                    updateTagDefinition(tag, tagset);
                }
            } catch (Exception e) {
                propertyChangeSupport.firePropertyChange(RepositoryChangeEvent.exceptionOccurred.name(), null, e);
            }
        }
    };
    tagManager.addPropertyChangeListener(TagManagerEvent.tagDefinitionChanged, tagDefinitionChangedListener);
    userDefinedPropertyChangedListener = new PropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent evt) {
            if (!tagManagerListenersEnabled) {
                return;
            }
            Object oldValue = evt.getOldValue();
            Object newValue = evt.getNewValue();
            try {
                if (oldValue == null) {
                    // insert
                    @SuppressWarnings("unchecked") Pair<PropertyDefinition, TagDefinition> newPair = (Pair<PropertyDefinition, TagDefinition>) newValue;
                    PropertyDefinition propertyDefinition = newPair.getFirst();
                    TagDefinition tagDefinition = newPair.getSecond();
                    addPropertyDefinition(propertyDefinition, tagDefinition);
                } else if (newValue == null) {
                    // delete
                    @SuppressWarnings("unchecked") Pair<PropertyDefinition, Pair<TagDefinition, TagsetDefinition>> oldPair = (Pair<PropertyDefinition, Pair<TagDefinition, TagsetDefinition>>) oldValue;
                    PropertyDefinition propertyDefinition = oldPair.getFirst();
                    TagDefinition tagDefinition = oldPair.getSecond().getFirst();
                    TagsetDefinition tagsetDefinition = oldPair.getSecond().getSecond();
                    removePropertyDefinition(propertyDefinition, tagDefinition, tagsetDefinition);
                } else {
                    // update
                    PropertyDefinition propertyDefinition = (PropertyDefinition) evt.getNewValue();
                    TagDefinition tagDefinition = (TagDefinition) evt.getOldValue();
                    updatePropertyDefinition(propertyDefinition, tagDefinition);
                }
            } catch (Exception e) {
                propertyChangeSupport.firePropertyChange(RepositoryChangeEvent.exceptionOccurred.name(), null, e);
            }
        }
    };
    tagManager.addPropertyChangeListener(TagManagerEvent.userPropertyDefinitionChanged, userDefinedPropertyChangedListener);
}
Also used : TagsetDefinition(de.catma.tag.TagsetDefinition) TagDefinition(de.catma.tag.TagDefinition) PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) PropertyDefinition(de.catma.tag.PropertyDefinition) IOException(java.io.IOException) Pair(de.catma.util.Pair)

Example 18 with TagDefinition

use of de.catma.tag.TagDefinition in project catma by forTEXT.

the class GraphWorktreeProject method importTagsets.

@Override
public void importTagsets(List<TagsetDefinitionImportStatus> tagsetDefinitionImportStatusList) throws IOException {
    for (TagsetDefinitionImportStatus tagsetDefinitionImportStatus : tagsetDefinitionImportStatusList) {
        if (tagsetDefinitionImportStatus.isDoImport()) {
            TagsetDefinition tagset = tagsetDefinitionImportStatus.getTagset();
            // new Tagset
            if (!tagsetDefinitionImportStatus.isInProjectHistory()) {
                try {
                    tagManagerListenersEnabled = false;
                    try {
                        addTagsetDefinition(tagset);
                        tagManager.addTagsetDefinition(tagset);
                    } catch (Exception e) {
                        throw new IOException(String.format("Import of Tagset %1$s failed! The import has been aborted.", tagset.getName()), e);
                    }
                } finally {
                    tagManagerListenersEnabled = true;
                }
                for (TagDefinition tag : tagset.getRootTagDefinitions()) {
                    tagManager.addTagDefinition(tagset, tag);
                    importTagHierarchy(tag, tagset, tagset);
                }
            } else // removed, but exists in version history
            if (!tagsetDefinitionImportStatus.isCurrent()) {
                String oldRootRevisionHash = this.rootRevisionHash;
                Pair<TagsetDefinition, String> result = gitProjectHandler.cloneAndAddTagset(tagset.getUuid(), tagset.getName(), String.format("Re-Added Tagset %1$s with ID %2$s", tagset.getName(), tagset.getUuid()));
                TagsetDefinition oldTagset = result.getFirst();
                this.rootRevisionHash = result.getSecond();
                // remove old Tags
                for (TagDefinition tagDefinition : oldTagset.getRootTagDefinitions()) {
                    gitProjectHandler.removeTag(tagDefinition);
                    oldTagset.remove(tagDefinition);
                }
                try {
                    // add empty Tagset
                    graphProjectHandler.addTagset(this.rootRevisionHash, oldTagset, oldRootRevisionHash);
                    try {
                        tagManagerListenersEnabled = false;
                        tagManager.addTagsetDefinition(tagset);
                    } finally {
                        tagManagerListenersEnabled = true;
                    }
                    // add imported Tags
                    for (TagDefinition tag : tagset.getRootTagDefinitions()) {
                        tagManager.addTagDefinition(oldTagset, tag);
                        importTagHierarchy(tag, tagset, oldTagset);
                    }
                    // update meta data
                    oldTagset.setName(tagset.getName());
                    updateTagsetDefinition(oldTagset);
                } catch (Exception e) {
                    throw new IOException(String.format("Import of Tagset %1$s failed! The import has been aborted.", tagset.getName()), e);
                }
            } else // exists already in project
            {
                try {
                    TagsetDefinition existingTagset = getTagManager().getTagLibrary().getTagsetDefinition(tagset.getUuid());
                    for (TagDefinition incomingTag : tagset) {
                        if (existingTagset.hasTagDefinition(incomingTag.getUuid())) {
                            if (tagsetDefinitionImportStatus.passesUpdateFilter(incomingTag.getUuid())) {
                                TagDefinition existingTag = existingTagset.getTagDefinition(incomingTag.getUuid());
                                for (PropertyDefinition incomingPropertyDef : incomingTag.getUserDefinedPropertyDefinitions()) {
                                    PropertyDefinition existingPropertyDef = existingTag.getPropertyDefinitionByUuid(incomingPropertyDef.getUuid());
                                    if (existingPropertyDef != null) {
                                        for (String value : incomingPropertyDef.getPossibleValueList()) {
                                            if (!existingPropertyDef.getPossibleValueList().contains(value)) {
                                                existingPropertyDef.addValue(value);
                                            }
                                        }
                                        existingPropertyDef.setName(incomingPropertyDef.getName());
                                        updatePropertyDefinition(existingPropertyDef, existingTag);
                                    } else {
                                        existingTag.addUserDefinedPropertyDefinition(incomingPropertyDef);
                                    }
                                    existingTag.setName(incomingTag.getName());
                                    existingTag.setColor(incomingTag.getColor());
                                    updateTagDefinition(existingTag, existingTagset);
                                }
                            }
                        } else {
                            getTagManager().addTagDefinition(existingTagset, incomingTag);
                        }
                    }
                    existingTagset.setName(tagset.getName());
                    updateTagsetDefinition(existingTagset);
                } catch (Exception e) {
                    throw new IOException(String.format("Import of Tagset %1$s failed! The import has been aborted.", tagset.getName()), e);
                }
            }
        }
    }
}
Also used : TagsetDefinition(de.catma.tag.TagsetDefinition) TagDefinition(de.catma.tag.TagDefinition) IOException(java.io.IOException) TagsetDefinitionImportStatus(de.catma.serialization.TagsetDefinitionImportStatus) PropertyDefinition(de.catma.tag.PropertyDefinition) IOException(java.io.IOException) Pair(de.catma.util.Pair)

Example 19 with TagDefinition

use of de.catma.tag.TagDefinition in project catma by forTEXT.

the class TPGraphProjectIndexer method searchProperty.

@Override
public QueryResult searchProperty(QueryId queryId, List<String> collectionIdList, String propertyNamePattern, String propertyValuePattern, String tagPathPattern) throws Exception {
    QueryResultRowArray result = new QueryResultRowArray();
    PropertyNameFilter propertyNameFilter = new PropertyNameFilter(propertyNamePattern);
    PropertyValueFilter propertyValueFilter = new PropertyValueFilter(propertyValuePattern);
    // add default wildcard if no explicit root is defined
    if (tagPathPattern != null) {
        if (!tagPathPattern.startsWith("/")) {
            tagPathPattern = "%" + tagPathPattern;
        }
    }
    final String tagPathRegex = tagPathPattern == null ? null : SQLWildcard2RegexConverter.convert(tagPathPattern);
    GraphTraversalSource g = graph.traversal();
    // get all Tags referenced by the participating Collections
    GraphTraversal<Vertex, Vertex> traversal = g.V().hasLabel(nt(ProjectRevision)).outE(rt(hasDocument)).inV().hasLabel(nt(SourceDocument)).outE(rt(hasCollection)).inV().has(nt(MarkupCollection), "collectionId", P.within(collectionIdList)).outE(rt(hasInstance)).inV().hasLabel(nt(TagInstance)).inE(rt(hasInstance)).outV().hasLabel(nt(Tag));
    Set<Vertex> tagVs = traversal.toSet();
    if (!tagVs.isEmpty()) {
        // get all paths for the Tags
        List<Path> tagPaths = g.V(tagVs).optional(__.repeat(__.out(rt(hasParent))).until(__.outE(rt(hasParent)).count().is(0))).path().toList();
        // collect all Tags matching the given pattern and map them by their tagId
        Map<String, String> validTagIdToTagPathMapping = new HashMap<>();
        for (Path path : tagPaths) {
            Vertex tag = path.get(0);
            String tagId = (String) tag.properties("tagId").next().orElse(null);
            StringBuilder builder = new StringBuilder();
            String conc = "/";
            path.forEach(tagVertex -> {
                builder.insert(0, ((Vertex) tagVertex).properties("name").next().orElse(null));
                builder.insert(0, conc);
            });
            String tagPathStr = builder.toString();
            if ((tagPathRegex == null) || Pattern.matches(tagPathRegex, tagPathStr)) {
                validTagIdToTagPathMapping.put(tagId, tagPathStr);
            }
        }
        // get all Annotations for the participating Collections and Tags with their matching Annotaiton Properties
        List<Map<String, Object>> resultMap = g.V().hasLabel(nt(ProjectRevision)).outE(rt(hasDocument)).inV().hasLabel(nt(SourceDocument)).as("doc-uuid").outE(rt(hasCollection)).inV().has(nt(MarkupCollection), "collectionId", P.within(collectionIdList)).as("collection-uuid").outE(rt(hasInstance)).inV().hasLabel(nt(TagInstance)).as("anno").optional(__.outE(rt(hasProperty)).inV().hasLabel(nt(AnnotationProperty)).filter(propertyValueFilter)).as("anno-property").select("anno").inE(rt(hasInstance)).outV().has(nt(Tag), "tagId", P.within(validTagIdToTagPathMapping.keySet())).as("tag").optional(__.outE(rt(hasProperty)).inV().hasLabel(nt(Property)).filter(propertyNameFilter)).as("property").select("doc-uuid", "collection-uuid", "anno", "tag", "anno-property", "property").by("documentId").by("collectionId").by().by().by().by().toList();
        HashSet<String> systemPropertiesAddedTagInstanceIds = new HashSet<>();
        for (Map<String, Object> entry : resultMap) {
            String documentId = (String) entry.get("doc-uuid");
            String collectionId = (String) entry.get("collection-uuid");
            Vertex annoV = (Vertex) entry.get("anno");
            String tagInstanceId = (String) annoV.property("tagInstanceId").value();
            @SuppressWarnings("unchecked") List<Integer> ranges = (List<Integer>) annoV.property("ranges").value();
            List<Range> rangeList = new ArrayList<>();
            for (int i = 0; i < ranges.size() - 1; i += 2) {
                rangeList.add(new Range(ranges.get(i), ranges.get(i + 1)));
            }
            String annoAuthor = (String) annoV.property("author").value();
            String annoTimestamp = (String) annoV.property("timestamp").value();
            Vertex tagV = (Vertex) entry.get("tag");
            String tagId = (String) tagV.property("tagId").value();
            String tagPath = validTagIdToTagPathMapping.get(tagId);
            TagDefinition tag = (TagDefinition) tagV.property("tag").value();
            String color = tag.getColor();
            Vertex propertyV = (Vertex) entry.get("property");
            if (propertyV.equals(tagV)) {
                // no matching Properties for this Tag
                propertyV = null;
            }
            Vertex annoPropertyV = (Vertex) entry.get("anno-property");
            if (annoPropertyV.equals(annoV)) {
                // no matching Annotation Property for this Annotation
                annoPropertyV = null;
            }
            // we try to add them now with respect to user defined name and value filters
            if (!systemPropertiesAddedTagInstanceIds.contains(tagInstanceId)) {
                // try to add rows for matching system properties
                addTagQueryResultRowForSystemProperty(queryId, result, PropertyDefinition.SystemPropertyName.catma_markupauthor, annoAuthor, propertyNameFilter, propertyValueFilter, documentId, collectionId, tagId, tagPath, tagInstanceId, rangeList);
                addTagQueryResultRowForSystemProperty(queryId, result, PropertyDefinition.SystemPropertyName.catma_markuptimestamp, annoTimestamp, propertyNameFilter, propertyValueFilter, documentId, collectionId, tagId, tagPath, tagInstanceId, rangeList);
                addTagQueryResultRowForSystemProperty(queryId, result, PropertyDefinition.SystemPropertyName.catma_displaycolor, color, propertyNameFilter, propertyValueFilter, documentId, collectionId, tagId, tagPath, tagInstanceId, rangeList);
                systemPropertiesAddedTagInstanceIds.add(tagInstanceId);
            }
            // add rows for user defined properties for each matching value
            if ((propertyV != null) && (annoPropertyV != null)) {
                @SuppressWarnings("unchecked") List<String> propertyValues = (List<String>) annoPropertyV.property("values").value();
                String annoPropertyDefinitionId = (String) annoPropertyV.property("uuid").value();
                String propertyName = (String) propertyV.property("name").value();
                String propertyDefinitionId = (String) propertyV.property("uuid").value();
                if (annoPropertyDefinitionId.equals(propertyDefinitionId)) {
                    for (String propValue : propertyValues) {
                        if (propertyValueFilter.testValue(propValue)) {
                            result.add(new TagQueryResultRow(queryId, documentId, rangeList, collectionId, tagId, tagPath, // TODO: Version
                            "", tagInstanceId, annoPropertyDefinitionId, propertyName, propValue));
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) TagDefinition(de.catma.tag.TagDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Range(de.catma.document.Range) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray) Map(java.util.Map) HashMap(java.util.HashMap)

Example 20 with TagDefinition

use of de.catma.tag.TagDefinition in project catma by forTEXT.

the class GraphWriter method addHasParentRelations.

void addHasParentRelations(Vertex tagsetV, TagsetDefinition tagset) {
    if (!tagset.isEmpty()) {
        GraphTraversalSource g = graph.traversal();
        for (TagDefinition tag : tagset) {
            if (!tag.getParentUuid().isEmpty()) {
                Vertex tagV = g.V(tagsetV).outE(rt(hasTag)).inV().has(nt(Tag), "tagId", tag.getUuid()).next();
                GraphTraversal<Vertex, Vertex> traversal = g.V(tagsetV).outE(rt(hasTag)).inV().has(nt(Tag), "tagId", tag.getParentUuid());
                if (traversal.hasNext()) {
                    Vertex parentTagV = traversal.next();
                    tagV.addEdge(rt(hasParent), parentTagV);
                } else {
                    throw new IllegalStateException(String.format("Couldn't find parent for Tag %1$s with parent ID %2$s in Tagset %3$s", tag.toString(), tag.getParentUuid(), tagset.toString()));
                }
            }
        }
    }
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) TagDefinition(de.catma.tag.TagDefinition) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex)

Aggregations

TagDefinition (de.catma.tag.TagDefinition)72 ArrayList (java.util.ArrayList)31 PropertyDefinition (de.catma.tag.PropertyDefinition)28 TagsetDefinition (de.catma.tag.TagsetDefinition)28 List (java.util.List)23 IDGenerator (de.catma.util.IDGenerator)22 Pair (de.catma.util.Pair)22 UI (com.vaadin.ui.UI)19 Project (de.catma.project.Project)19 ErrorHandler (de.catma.ui.module.main.ErrorHandler)19 PropertyChangeEvent (java.beans.PropertyChangeEvent)19 PropertyChangeListener (java.beans.PropertyChangeListener)19 IOException (java.io.IOException)19 Collection (java.util.Collection)19 Collectors (java.util.stream.Collectors)19 Notification (com.vaadin.ui.Notification)17 Type (com.vaadin.ui.Notification.Type)17 Set (java.util.Set)17 TagManagerEvent (de.catma.tag.TagManager.TagManagerEvent)16 SaveCancelListener (de.catma.ui.dialog.SaveCancelListener)16