use of de.catma.tag.TagDefinition in project catma by forTEXT.
the class XmlMarkupCollectionSerializationHandler method scanElements.
private void scanElements(StringBuilder contentBuilder, Element element, Stack<String> elementStack, TagManager tagManager, TagLibrary tagLibrary, Map<String, String> namespacePrefixToTagsetIdMap, AnnotationCollection userMarkupCollection, String docId, int docLength) throws Exception {
int start = contentBuilder.length();
StringBuilder pathBuilder = new StringBuilder();
for (int j = 0; j < elementStack.size(); j++) {
pathBuilder.append("/" + elementStack.get(j));
}
String parentPath = pathBuilder.toString();
elementStack.push(element.getLocalName());
String path = parentPath + "/" + elementStack.peek();
String tagName = element.getLocalName();
String prefix = element.getNamespacePrefix();
String tagsetId = namespacePrefixToTagsetIdMap.get(prefix);
if (tagsetId == null) {
tagsetId = KnownTagsetDefinitionName.DEFAULT_INTRINSIC_XML.asTagsetId();
}
TagsetDefinition tagset = tagLibrary.getTagsetDefinition(tagsetId);
String tagId = idGenerator.generate();
TagDefinition tagDefinition = tagset.getTagDefinitionsByName(tagName).findFirst().orElse(null);
String pathPropertyDefId = null;
if (tagDefinition == null) {
tagDefinition = new TagDefinition(tagId, elementStack.peek(), // no parent, hierarchy is collected in annotation property
null, tagsetId);
tagDefinition.addSystemPropertyDefinition(new PropertyDefinition(idGenerator.generate(PropertyDefinition.SystemPropertyName.catma_displaycolor.name()), PropertyDefinition.SystemPropertyName.catma_displaycolor.name(), Collections.singletonList(ColorConverter.toRGBIntAsString(ColorConverter.randomHex()))));
tagDefinition.addSystemPropertyDefinition(new PropertyDefinition(idGenerator.generate(PropertyDefinition.SystemPropertyName.catma_markupauthor.name()), PropertyDefinition.SystemPropertyName.catma_markupauthor.name(), Collections.singletonList(author)));
pathPropertyDefId = idGenerator.generate();
PropertyDefinition pathDef = new PropertyDefinition(pathPropertyDefId, "path", Collections.emptyList());
tagDefinition.addUserDefinedPropertyDefinition(pathDef);
tagManager.addTagDefinition(tagset, tagDefinition);
} else {
pathPropertyDefId = tagDefinition.getPropertyDefinition("path").getUuid();
}
for (int idx = 0; idx < element.getChildCount(); idx++) {
Node curChild = element.getChild(idx);
if (curChild instanceof Text) {
xmlContentHandler.addTextContent(contentBuilder, element, curChild.getValue());
} else if (curChild instanceof Element) {
// descent
scanElements(contentBuilder, (Element) curChild, elementStack, tagManager, tagLibrary, namespacePrefixToTagsetIdMap, userMarkupCollection, docId, docLength);
}
}
if (element.getChildCount() != 0) {
xmlContentHandler.addBreak(contentBuilder, element);
}
int end = contentBuilder.length();
Range range = new Range(start, end);
if (range.isSinglePoint()) {
int newStart = range.getStartPoint();
if (newStart > 0) {
newStart = newStart - 1;
}
int newEnd = range.getEndPoint();
if (newEnd < docLength - 1) {
newEnd = newEnd + 1;
}
range = new Range(newStart, newEnd);
}
TagInstance tagInstance = new TagInstance(idGenerator.generate(), tagDefinition.getUuid(), author, ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), tagDefinition.getUserDefinedPropertyDefinitions(), tagDefinition.getTagsetDefinitionUuid());
for (int i = 0; i < element.getAttributeCount(); i++) {
PropertyDefinition propertyDefinition = tagDefinition.getPropertyDefinition(element.getAttribute(i).getQualifiedName());
if (propertyDefinition == null) {
propertyDefinition = new PropertyDefinition(idGenerator.generate(), element.getAttribute(i).getQualifiedName(), Collections.singleton(element.getAttribute(i).getValue()));
tagManager.addUserDefinedPropertyDefinition(tagDefinition, propertyDefinition);
} else if (!propertyDefinition.getPossibleValueList().contains(element.getAttribute(i).getValue())) {
List<String> newValueList = new ArrayList<>();
newValueList.addAll(propertyDefinition.getPossibleValueList());
newValueList.add(element.getAttribute(i).getValue());
propertyDefinition.setPossibleValueList(newValueList);
}
Property property = new Property(propertyDefinition.getUuid(), Collections.singleton(element.getAttribute(i).getValue()));
tagInstance.addUserDefinedProperty(property);
}
Property pathProperty = new Property(pathPropertyDefId, Collections.singletonList(path));
tagInstance.addUserDefinedProperty(pathProperty);
TagReference tagReference = new TagReference(tagInstance, docId, range, userMarkupCollection.getId());
userMarkupCollection.addTagReference(tagReference);
elementStack.pop();
}
use of de.catma.tag.TagDefinition in project catma by forTEXT.
the class CollectionQueryResultRowItem method addChildRowItems.
@Override
public void addChildRowItems(TreeData<QueryResultRowItem> treeData, LoadingCache<String, KwicProvider> kwicProviderCache) {
try {
for (QueryResultRow row : getRows()) {
if (row instanceof TagQueryResultRow) {
TagQueryResultRow tRow = (TagQueryResultRow) row;
KwicProvider kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
TagDefinition tagDefinition = project.getTagManager().getTagLibrary().getTagDefinition(tRow.getTagDefinitionId());
KwicQueryResultRowItem item = new KwicQueryResultRowItem(tRow, AnnotatedTextProvider.buildAnnotatedText(new ArrayList<>(tRow.getRanges()), kwicProvider, tagDefinition), AnnotatedTextProvider.buildAnnotatedKeywordInContext(new ArrayList<>(tRow.getRanges()), kwicProvider, tagDefinition, tRow.getTagDefinitionPath()), true);
if (!treeData.contains(item)) {
treeData.addItem(this, item);
}
}
}
} catch (Exception e) {
((ErrorHandler) UI.getCurrent()).showAndLogError("error displaying annotated kwic query results", e);
}
}
use of de.catma.tag.TagDefinition in project catma by forTEXT.
the class TagSelectionPanel method handleAddSubtagRequest.
private void handleAddSubtagRequest() {
final List<TagDefinition> parentTags = tagsetGrid.getSelectedItems().stream().filter(tagsetTreeItem -> tagsetTreeItem instanceof TagDataItem).map(tagsetTreeItem -> ((TagDataItem) tagsetTreeItem).getTag()).collect(Collectors.toList());
for (TagDefinition parentTag : parentTags) {
if (!project.hasPermission(project.getRoleForTagset(parentTag.getTagsetDefinitionUuid()), RBACPermission.TAGSET_WRITE)) {
Notification.show("Info", String.format("You do not have the permission to make changes to the Tagset of Tag %1$s, " + "Please contact the Project maintainer!", parentTag.getName()), Type.HUMANIZED_MESSAGE);
return;
}
}
if (!parentTags.isEmpty()) {
AddSubtagDialog addTagDialog = new AddSubtagDialog(new SaveCancelListener<TagDefinition>() {
public void savePressed(TagDefinition result) {
for (TagDefinition parent : parentTags) {
TagsetDefinition tagset = project.getTagManager().getTagLibrary().getTagsetDefinition(parent);
TagDefinition tag = new TagDefinition(result);
tag.setUuid(idGenerator.generate());
tag.setParentUuid(parent.getUuid());
tag.setTagsetDefinitionUuid(tagset.getUuid());
project.getTagManager().addTagDefinition(tagset, tag);
}
}
});
addTagDialog.show();
} else {
Notification.show("Info", "Please select at least one parent Tag!", Type.HUMANIZED_MESSAGE);
}
}
use of de.catma.tag.TagDefinition in project catma by forTEXT.
the class TagsView method initListeners.
private void initListeners() {
tagChangedListener = new PropertyChangeListener() {
@SuppressWarnings("unchecked")
@Override
public void propertyChange(PropertyChangeEvent evt) {
Object newValue = evt.getNewValue();
Object oldValue = evt.getOldValue();
if (oldValue == null) {
// created
Pair<TagsetDefinition, TagDefinition> value = (Pair<TagsetDefinition, TagDefinition>) newValue;
TagsetDefinition tagset = value.getFirst();
TagDefinition tag = value.getSecond();
if (tag.getParentUuid().isEmpty()) {
tagsetData.getRootItems().stream().map(tagsetTreeItem -> (TagsetDataItem) tagsetTreeItem).filter(tagsetDataItem -> tagsetDataItem.getTagset().getUuid().equals(tagset.getUuid())).findFirst().ifPresent(tagsetDataItem -> {
TagDataItem tagDataItem = new TagDataItem(tag, tagsetDataItem.isEditable());
if (!tagsetData.contains(tagDataItem)) {
tagsetData.addItem(tagsetDataItem, tagDataItem);
tagsetDataProvider.refreshAll();
tagsetGrid.expand(tagsetDataItem);
}
});
} else {
tagsetData.getRootItems().stream().map(tagsetTreeItem -> (TagsetDataItem) tagsetTreeItem).filter(tagsetDataItem -> tagsetDataItem.getTagset().getUuid().equals(tagset.getUuid())).findFirst().ifPresent(tagsetDataItem -> {
TagDataItem tagDataItem = new TagDataItem(tag, tagsetDataItem.isEditable());
if (!tagsetData.contains(tagDataItem)) {
TagDefinition parentTag = project.getTagManager().getTagLibrary().getTagDefinition(tag.getParentUuid());
TagsetTreeItem parentTagItem = new TagDataItem(parentTag, tagsetDataItem.isEditable());
tagsetData.addItem(parentTagItem, tagDataItem);
tagsetDataProvider.refreshAll();
tagsetGrid.expand(parentTagItem);
}
});
}
} else if (newValue == null) {
// removed
Pair<TagsetDefinition, TagDefinition> deleted = (Pair<TagsetDefinition, TagDefinition>) oldValue;
TagDefinition deletedTag = deleted.getSecond();
tagsetData.removeItem(new TagDataItem(deletedTag, true));
tagsetDataProvider.refreshAll();
tagsetGrid.deselectAll();
} else {
// update
TagDefinition tag = (TagDefinition) newValue;
TagsetDefinition tagset = (TagsetDefinition) oldValue;
tagsetData.getRootItems().stream().map(tagsetTreeItem -> (TagsetDataItem) tagsetTreeItem).filter(tagsetDataItem -> tagsetDataItem.getTagset().getUuid().equals(tagset.getUuid())).findFirst().ifPresent(tagsetDataItem -> {
TagDataItem oldItem = new TagDataItem(tag, tagsetDataItem.isEditable());
String parentTagId = tag.getParentUuid();
TagsetTreeItem parent = tagsetDataItem;
if (!parentTagId.isEmpty()) {
parent = new TagDataItem(tagset.getTagDefinition(parentTagId), tagsetDataItem.isEditable());
}
tagsetData.removeItem(oldItem);
TagDataItem tagDataItem = new TagDataItem(tag, tagsetDataItem.isEditable());
tagDataItem.setPropertiesExpanded(true);
tagsetData.addItem(parent, tagDataItem);
addTagSubTree(tagset, tag, tagDataItem);
// TODO: sort
tagsetDataProvider.refreshAll();
showExpandedProperties(tagDataItem);
});
}
}
};
project.getTagManager().addPropertyChangeListener(TagManagerEvent.tagDefinitionChanged, tagChangedListener);
propertyDefinitionChangedListener = new PropertyChangeListener() {
@SuppressWarnings("unchecked")
@Override
public void propertyChange(PropertyChangeEvent evt) {
Object newValue = evt.getNewValue();
Object oldValue = evt.getOldValue();
TagDefinition tag = null;
if (oldValue == null) {
// created
Pair<PropertyDefinition, TagDefinition> newData = (Pair<PropertyDefinition, TagDefinition>) newValue;
tag = newData.getSecond();
} else if (newValue == null) {
// removed
Pair<PropertyDefinition, Pair<TagDefinition, TagsetDefinition>> oldData = (Pair<PropertyDefinition, Pair<TagDefinition, TagsetDefinition>>) oldValue;
tag = oldData.getSecond().getFirst();
} else {
// update
tag = (TagDefinition) oldValue;
}
TagsetTreeItem parentItem = null;
if (tag.getParentUuid().isEmpty()) {
parentItem = new TagsetDataItem(project.getTagManager().getTagLibrary().getTagsetDefinition(tag.getTagsetDefinitionUuid()));
} else {
parentItem = new TagDataItem(project.getTagManager().getTagLibrary().getTagDefinition(tag.getParentUuid()));
}
final String tagId = tag.getUuid();
tagsetData.getChildren(parentItem).stream().map(tagsetTreeItem -> (TagDataItem) tagsetTreeItem).filter(tagDataItem -> tagDataItem.getTag().getUuid().equals(tagId)).findFirst().ifPresent(tagDataItem -> {
tagDataItem.setPropertiesExpanded(false);
hideExpandedProperties(tagDataItem);
tagDataItem.setPropertiesExpanded(true);
showExpandedProperties(tagDataItem);
});
tagsetDataProvider.refreshAll();
tagsetGrid.deselectAll();
}
};
project.getTagManager().addPropertyChangeListener(TagManagerEvent.userPropertyDefinitionChanged, propertyDefinitionChangedListener);
}
use of de.catma.tag.TagDefinition in project catma by forTEXT.
the class TagsView method deleteTags.
void deleteTags(List<TagDefinition> targetTags) {
String msg = String.format("Are you sure you want to delete the following Tags: %1$s?", targetTags.stream().map(TagDefinition::getName).collect(Collectors.joining(",")));
ConfirmDialog.show(UI.getCurrent(), "Warning", msg, "Delete", "Cancel", dlg -> {
if (dlg.isConfirmed()) {
for (TagDefinition tag : targetTags) {
TagsetDefinition tagset = project.getTagManager().getTagLibrary().getTagsetDefinition(tag);
project.getTagManager().removeTagDefinition(tagset, tag);
}
}
});
}
Aggregations