use of de.catma.tag.PropertyDefinition in project catma by forTEXT.
the class TeiUserMarkupCollectionDeserializer method addProperties.
private void addProperties(TagDefinition tagDefinition, AddPropertyHandler addPropertyHandler, Nodes propertyElements) {
for (int i = 0; i < propertyElements.size(); i++) {
try {
TeiElement curPropertyElement = (TeiElement) propertyElements.get(i);
PropertyDefinition propertyDefinition = tagDefinition.getPropertyDefinition(curPropertyElement.getAttributeValue(Attribute.f_name));
if (curPropertyElement.getChildElements().size() == 0) {
addPropertyHandler.addProperty(new Property(propertyDefinition.getUuid(), Collections.emptyList()));
} else {
TeiElement valueElement = (TeiElement) curPropertyElement.getChildElements().get(0);
if (valueElement.is(TeiElementName.numeric)) {
addPropertyHandler.addProperty(new Property(propertyDefinition.getUuid(), new NumericPropertyValueFactory(curPropertyElement).getValueAsList()));
} else if (valueElement.is(TeiElementName.string)) {
StringPropertyValueFactory stringPropFact = new StringPropertyValueFactory(curPropertyElement);
if (!stringPropFact.getValue().trim().isEmpty()) {
addPropertyHandler.addProperty(new Property(propertyDefinition.getUuid(), stringPropFact.getValueAsList()));
}
} else if (valueElement.is(TeiElementName.vRange)) {
TeiElement vColl = (TeiElement) valueElement.getChildElements().get(0);
if (vColl.hasChildElements()) {
List<String> valueList = new ArrayList<String>();
for (int j = 0; j < vColl.getChildElements().size(); j++) {
valueList.add(new StringPropertyValueFactory(vColl, j).getValue());
}
addPropertyHandler.addProperty(new Property(propertyDefinition.getUuid(), valueList));
}
} else {
throw new UnknownElementException(valueElement.getLocalName() + " is not supported!");
}
}
} catch (UnknownElementException ue) {
logger.log(Level.SEVERE, "error adding properties", ue);
}
}
}
use of de.catma.tag.PropertyDefinition 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.PropertyDefinition 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.PropertyDefinition in project catma by forTEXT.
the class TagsView method handleAddPropertyRequest.
private void handleAddPropertyRequest() {
final List<TagDefinition> targetTags = new ArrayList<>();
if (tagsetGrid.getSelectedItems().size() == 1) {
TagsetTreeItem selectedItem = tagsetGrid.getSelectedItems().iterator().next();
while (!(selectedItem instanceof TagDataItem) && (selectedItem != null)) {
selectedItem = tagsetData.getParent(selectedItem);
}
if (selectedItem != null) {
targetTags.add(((TagDataItem) selectedItem).getTag());
}
} else {
targetTags.addAll(tagsetGrid.getSelectedItems().stream().filter(tagsetTreeItem -> tagsetTreeItem instanceof TagDataItem).map(tagsetTreeItem -> ((TagDataItem) tagsetTreeItem).getTag()).collect(Collectors.toList()));
}
if (targetTags.isEmpty()) {
Notification.show("Info", "Please select one ore more Tags first!", Type.TRAY_NOTIFICATION);
} else {
for (TagDefinition targetTag : targetTags) {
if (!project.hasPermission(project.getRoleForTagset(targetTag.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!", targetTag.getName()), Type.HUMANIZED_MESSAGE);
return;
}
}
Multimap<String, PropertyDefinition> propertiesByName = ArrayListMultimap.create();
for (TagDefinition tag : targetTags) {
for (PropertyDefinition propertyDef : tag.getUserDefinedPropertyDefinitions()) {
if (!propertiesByName.containsKey(propertyDef.getName()) || propertiesByName.get(propertyDef.getName()).iterator().next().getPossibleValueList().equals(propertyDef.getPossibleValueList())) {
propertiesByName.put(propertyDef.getName(), propertyDef);
}
}
}
List<PropertyDefinition> commonProperties = propertiesByName.asMap().entrySet().stream().filter(entry -> entry.getValue().size() == targetTags.size()).map(entry -> new PropertyDefinition(entry.getValue().iterator().next())).collect(Collectors.toList());
// just a single tag's properties or is it a bulk(>1) edit?
final boolean bulkEdit = targetTags.size() > 1;
AddEditPropertyDialog addPropertyDialog = new AddEditPropertyDialog(bulkEdit, commonProperties, new SaveCancelListener<List<PropertyDefinition>>() {
@Override
public void savePressed(List<PropertyDefinition> result) {
if (bulkEdit) {
handleBulkEditProperties(result, commonProperties, targetTags);
} else {
handleSingleEditProperties(result, targetTags.iterator().next());
}
}
});
addPropertyDialog.show();
}
}
use of de.catma.tag.PropertyDefinition in project catma by forTEXT.
the class TagsView method handleBulkEditProperties.
private void handleBulkEditProperties(List<PropertyDefinition> editedProperties, List<PropertyDefinition> commonProperties, List<TagDefinition> targetTags) {
final Set<String> availableCommonPropertyNames = editedProperties.stream().map(propertyDef -> propertyDef.getName()).collect(Collectors.toSet());
final Set<String> deletedCommonProperyNames = commonProperties.stream().map(propertyDef -> propertyDef.getName()).filter(name -> !availableCommonPropertyNames.contains(name)).collect(Collectors.toSet());
for (TagDefinition tag : targetTags) {
TagsetDefinition tagset = project.getTagManager().getTagLibrary().getTagsetDefinition(tag);
for (PropertyDefinition existingPropertyDef : new ArrayList<>(tag.getUserDefinedPropertyDefinitions())) {
// handle deleted PropertyDefs
if (deletedCommonProperyNames.contains(existingPropertyDef.getName())) {
project.getTagManager().removeUserDefinedPropertyDefinition(existingPropertyDef, tag, tagset);
} else // handle updated PropertyDefs
if (availableCommonPropertyNames.contains(existingPropertyDef.getName())) {
editedProperties.stream().filter(possiblyChangedPd -> possiblyChangedPd.getName().equals(existingPropertyDef.getName())).findFirst().ifPresent(possiblyChangedPd -> existingPropertyDef.setPossibleValueList(possiblyChangedPd.getPossibleValueList()));
project.getTagManager().updateUserDefinedPropertyDefinition(tag, existingPropertyDef);
}
}
// handle created PropertyDefs
for (PropertyDefinition pd : editedProperties) {
if (tag.getPropertyDefinition(pd.getName()) == null) {
PropertyDefinition createdPropertyDefinition = new PropertyDefinition(pd);
pd.setUuid(idGenerator.generate());
project.getTagManager().addUserDefinedPropertyDefinition(tag, createdPropertyDefinition);
}
}
}
}
Aggregations