use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class ProjectView method handleSaveDocumentWizardContext.
private void handleSaveDocumentWizardContext(final WizardContext result) {
setEnabled(false);
setProgressBarVisible(true);
final UI ui = UI.getCurrent();
BackgroundServiceProvider backgroundServiceProvider = (BackgroundServiceProvider) UI.getCurrent();
BackgroundService backgroundService = backgroundServiceProvider.accuireBackgroundService();
backgroundService.submit(new DefaultProgressCallable<Void>() {
@SuppressWarnings("unchecked")
@Override
public Void call() throws Exception {
IDGenerator idGenerator = new IDGenerator();
boolean useApostropheAsSeparator = (Boolean) result.get(DocumentWizard.WizardContextKey.APOSTROPHE_AS_SEPARATOR);
String collectionNamePattern = (String) result.get(DocumentWizard.WizardContextKey.COLLECTION_NAME_PATTERN);
Collection<TagsetImport> tagsetImports = (Collection<TagsetImport>) result.get(DocumentWizard.WizardContextKey.TAGSET_IMPORT_LIST);
Collection<UploadFile> uploadFiles = (Collection<UploadFile>) result.get(DocumentWizard.WizardContextKey.UPLOAD_FILE_LIST);
if (tagsetImports == null) {
tagsetImports = Collections.emptyList();
}
// Ignoring Tagsets
tagsetImports.stream().filter(ti -> ti.getImportState().equals(TagsetImportState.WILL_BE_IGNORED)).forEach(tagsetImport -> {
for (TagDefinition tag : tagsetImport.getExtractedTagset()) {
for (UploadFile uploadFile : uploadFiles) {
if (uploadFile.getIntrinsicMarkupCollection() != null) {
AnnotationCollection intrinsicMarkupCollection = uploadFile.getIntrinsicMarkupCollection();
intrinsicMarkupCollection.removeTagReferences(intrinsicMarkupCollection.getTagReferences(tag));
}
}
}
});
getProgressListener().setProgress("Creating imported Tagsets");
// Creating Tagsets
tagsetImports.stream().filter(ti -> ti.getImportState().equals(TagsetImportState.WILL_BE_CREATED)).forEach(tagsetImport -> {
getProgressListener().setProgress("Creating Tagset %1$s", tagsetImport.getTargetTagset().getName());
ui.accessSynchronously(() -> {
if (project.getTagManager().getTagLibrary().getTagsetDefinition(tagsetImport.getTargetTagset().getUuid()) != null) {
// already imported, so it will be a merge
tagsetImport.setImportState(TagsetImportState.WILL_BE_MERGED);
} else {
TagsetDefinition extractedTagset = tagsetImport.getExtractedTagset();
try {
project.importTagsets(Collections.singletonList(new TagsetDefinitionImportStatus(extractedTagset, project.inProjectHistory(extractedTagset.getUuid()), project.getTagManager().getTagLibrary().getTagsetDefinition(extractedTagset.getUuid()) != null)));
} catch (Exception e) {
Logger.getLogger(ProjectView.class.getName()).log(Level.SEVERE, String.format("Error importing tagset %1$s with ID %2$s", extractedTagset.getName(), extractedTagset.getUuid()), e);
String errorMsg = e.getMessage();
if ((errorMsg == null) || (errorMsg.trim().isEmpty())) {
errorMsg = "";
}
Notification.show("Error", String.format("Error importing tagset %1$s! " + "This tagset will be skipped!\n The underlying error message was:\n%2$s", extractedTagset.getName(), errorMsg), Type.ERROR_MESSAGE);
}
}
ui.push();
});
});
// Merging Tagsets
tagsetImports.stream().filter(ti -> ti.getImportState().equals(TagsetImportState.WILL_BE_MERGED)).forEach(tagsetImport -> {
getProgressListener().setProgress("Merging Tagset %1$s", tagsetImport.getTargetTagset().getName());
ui.accessSynchronously(() -> {
TagsetDefinition targetTagset = project.getTagManager().getTagLibrary().getTagsetDefinition(tagsetImport.getTargetTagset().getUuid());
for (TagDefinition tag : tagsetImport.getExtractedTagset()) {
Optional<TagDefinition> optionalTag = targetTagset.getTagDefinitionsByName(tag.getName()).findFirst();
if (optionalTag.isPresent()) {
TagDefinition existingTag = optionalTag.get();
tag.getUserDefinedPropertyDefinitions().forEach(pd -> {
if (existingTag.getPropertyDefinition(pd.getName()) == null) {
project.getTagManager().addUserDefinedPropertyDefinition(existingTag, new PropertyDefinition(pd));
}
});
for (UploadFile uploadFile : uploadFiles) {
if (uploadFile.getIntrinsicMarkupCollection() != null) {
AnnotationCollection intrinsicMarkupCollection = uploadFile.getIntrinsicMarkupCollection();
List<TagReference> tagReferences = intrinsicMarkupCollection.getTagReferences(tag);
intrinsicMarkupCollection.removeTagReferences(tagReferences);
Multimap<TagInstance, TagReference> referencesByInstance = ArrayListMultimap.create();
tagReferences.forEach(tr -> referencesByInstance.put(tr.getTagInstance(), tr));
for (TagInstance incomingTagInstance : referencesByInstance.keySet()) {
TagInstance newTagInstance = new TagInstance(idGenerator.generate(), existingTag.getUuid(), incomingTagInstance.getAuthor(), incomingTagInstance.getTimestamp(), existingTag.getUserDefinedPropertyDefinitions(), targetTagset.getUuid());
for (Property oldProp : incomingTagInstance.getUserDefinedProperties()) {
String oldPropDefId = oldProp.getPropertyDefinitionId();
PropertyDefinition oldPropDef = tag.getPropertyDefinitionByUuid(oldPropDefId);
PropertyDefinition existingPropDef = existingTag.getPropertyDefinition(oldPropDef.getName());
newTagInstance.addUserDefinedProperty(new Property(existingPropDef.getUuid(), oldProp.getPropertyValueList()));
}
ArrayList<TagReference> newReferences = new ArrayList<>();
referencesByInstance.get(incomingTagInstance).forEach(tr -> {
try {
newReferences.add(new TagReference(newTagInstance, tr.getTarget().toString(), tr.getRange(), tr.getUserMarkupCollectionUuid()));
} catch (URISyntaxException e) {
e.printStackTrace();
}
});
intrinsicMarkupCollection.addTagReferences(newReferences);
}
}
}
} else {
tag.setTagsetDefinitionUuid(targetTagset.getUuid());
project.getTagManager().addTagDefinition(targetTagset, tag);
}
}
ui.push();
});
});
// Importing docs and collections
for (UploadFile uploadFile : uploadFiles) {
getProgressListener().setProgress("Importing Document %1$s", uploadFile.getTitle());
ui.accessSynchronously(() -> {
addUploadFile(uploadFile, useApostropheAsSeparator, collectionNamePattern);
ui.push();
});
}
return null;
}
}, new ExecutionListener<Void>() {
@Override
public void done(Void result) {
setProgressBarVisible(false);
setEnabled(true);
}
@Override
public void error(Throwable t) {
setProgressBarVisible(false);
setEnabled(true);
errorHandler.showAndLogError("Error adding Documents", t);
}
}, progressListener);
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class TeiUserMarkupCollectionDeserializer method deserialize.
private void deserialize(String collectionId) {
Nodes segmentNodes = teiDocument.getNodes(TeiElementName.seg, AttributeValue.seg_ana_catma_tag_ref.getStartsWith());
for (int i = 0; i < segmentNodes.size(); i++) {
TeiElement curSegment = (TeiElement) segmentNodes.get(i);
AnaValueHandler anaValueHandler = new AnaValueHandler();
List<String> tagInstanceIDs = anaValueHandler.makeTagInstanceIDListFrom(curSegment.getAttributeValue(Attribute.ana));
Elements pointerElements = curSegment.getChildElements(TeiElementName.ptr);
for (String tagInstanceID : tagInstanceIDs) {
TagInstance tagInstance = createTagInstance(tagInstanceID);
for (int j = 0; j < pointerElements.size(); j++) {
TeiElement curPointer = (TeiElement) pointerElements.get(j);
PtrValueHandler ptrValueHandler = new PtrValueHandler();
TargetValues targetValues = ptrValueHandler.getTargetValuesFrom(curPointer.getAttributeValue(Attribute.ptr_target));
try {
TagReference tagReference = new TagReference(tagInstance, targetValues.getURI(), targetValues.getRange(), collectionId);
tagReferences.add(tagReference);
} catch (URISyntaxException ue) {
logger.log(Level.SEVERE, "error during deserialization", ue);
}
}
}
}
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class GraphWorktreeProject method removePropertyDefinition.
private void removePropertyDefinition(PropertyDefinition propertyDefinition, TagDefinition tagDefinition, TagsetDefinition tagsetDefinition) throws Exception {
// remove AnnotationProperties
Multimap<String, TagReference> annotationIdsByCollectionId = graphProjectHandler.getTagReferencesByCollectionId(this.rootRevisionHash, propertyDefinition, tagDefinition);
for (String collectionId : annotationIdsByCollectionId.keySet()) {
// TODO: check permissions if commit is allowed, if that is not the case skip git update
gitProjectHandler.addCollectionToStagedAndCommit(collectionId, String.format("Autocommitting changes before performing an update of Annotations " + "as part of a Property Definition deletion operation", propertyDefinition.getName()), false);
Collection<TagReference> tagReferences = annotationIdsByCollectionId.get(collectionId);
Set<TagInstance> tagInstances = tagReferences.stream().map(tagReference -> tagReference.getTagInstance()).collect(Collectors.toSet());
tagInstances.forEach(tagInstance -> tagInstance.removeUserDefinedProperty(propertyDefinition.getUuid()));
for (TagInstance tagInstance : tagInstances) {
gitProjectHandler.addOrUpdate(collectionId, tagReferences.stream().filter(tagRef -> tagRef.getTagInstanceId().equals(tagInstance.getUuid())).collect(Collectors.toList()), tagManager.getTagLibrary());
}
String collectionRevisionHash = gitProjectHandler.addCollectionToStagedAndCommit(collectionId, String.format("Annotation Properties removed, caused by the removal of Tag Property %1$s ", propertyDefinition.getName()), false);
graphProjectHandler.removeProperties(this.rootRevisionHash, collectionId, collectionRevisionHash, propertyDefinition.getUuid());
}
String tagsetRevision = gitProjectHandler.removePropertyDefinition(propertyDefinition, tagDefinition, tagsetDefinition);
tagsetDefinition.setRevisionHash(tagsetRevision);
String oldRootRevisionHash = this.rootRevisionHash;
this.rootRevisionHash = gitProjectHandler.commitProject(String.format("Removed Property Definition %1$s with ID %2$s " + "from Tag %3$s with ID %4$s in Tagset %5$s with ID %6$s", propertyDefinition.getName(), propertyDefinition.getUuid(), tagDefinition.getName(), tagDefinition.getUuid(), tagsetDefinition.getName(), tagsetDefinition.getUuid()));
graphProjectHandler.removePropertyDefinition(rootRevisionHash, propertyDefinition, tagDefinition, tagsetDefinition, oldRootRevisionHash);
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class TaggerView method initListeners.
private void initListeners() {
this.tagReferencesChangedListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() != null) {
@SuppressWarnings("unchecked") Pair<AnnotationCollection, List<TagReference>> changeValue = (Pair<AnnotationCollection, List<TagReference>>) evt.getNewValue();
List<TagReference> tagReferences = changeValue.getSecond();
List<TagReference> relevantTagReferences = new ArrayList<TagReference>();
for (TagReference tr : tagReferences) {
if (isRelevantTagReference(tr, userMarkupCollectionManager.getUserMarkupCollections())) {
relevantTagReferences.add(tr);
}
}
tagger.setVisible(relevantTagReferences, true);
Set<String> tagInstanceUuids = new HashSet<String>();
for (TagReference tr : relevantTagReferences) {
tagInstanceUuids.add(tr.getTagInstance().getUuid());
}
tagInstanceUuids.forEach(annotationId -> tagger.updateAnnotation(annotationId));
} else if (evt.getOldValue() != null) {
@SuppressWarnings("unchecked") Pair<String, Collection<String>> changeValue = (Pair<String, Collection<String>>) evt.getOldValue();
String collectionId = changeValue.getFirst();
Collection<String> annotationIds = changeValue.getSecond();
if (userMarkupCollectionManager.contains(collectionId)) {
userMarkupCollectionManager.removeTagInstance(annotationIds, false);
}
tagger.removeTagInstances(annotationIds);
annotationPanel.removeAnnotations(annotationIds);
}
}
};
project.addPropertyChangeListener(RepositoryChangeEvent.tagReferencesChanged, tagReferencesChangedListener);
annotationPropertiesChangedListener = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
TagInstance tagInstance = (TagInstance) evt.getOldValue();
tagger.updateAnnotation(tagInstance.getUuid());
}
};
project.addPropertyChangeListener(RepositoryChangeEvent.propertyValueChanged, annotationPropertiesChangedListener);
tagChangedListener = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Object newValue = evt.getNewValue();
Object oldValue = evt.getOldValue();
if (oldValue == null) {
// created
// noop
} else if (newValue == null) {
// removed
@SuppressWarnings("unchecked") Pair<TagsetDefinition, TagDefinition> deleted = (Pair<TagsetDefinition, TagDefinition>) oldValue;
for (AnnotationCollectionReference ref : userMarkupCollectionManager.getCollections(deleted.getSecond())) {
setAnnotationCollectionSelected(ref, false);
setAnnotationCollectionSelected(ref, true);
}
} else {
// update
TagDefinition tag = (TagDefinition) newValue;
for (AnnotationCollection collection : userMarkupCollectionManager.getUserMarkupCollections()) {
List<TagReference> relevantTagReferences = collection.getTagReferences(tag);
tagger.setVisible(relevantTagReferences, false);
tagger.setVisible(relevantTagReferences, true);
}
}
}
};
project.getTagManager().addPropertyChangeListener(TagManagerEvent.tagDefinitionChanged, tagChangedListener);
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class TaggerView method setAnnotationCollectionSelected.
private void setAnnotationCollectionSelected(AnnotationCollectionReference collectionReference, boolean selected) {
try {
AnnotationCollection collection = project.getUserMarkupCollection(collectionReference);
if (selected) {
userMarkupCollectionManager.add(collection);
annotationPanel.addCollection(collection);
List<TagReference> visibleRefs = annotationPanel.getVisibleTagReferences(collection.getTagReferences());
if (!visibleRefs.isEmpty()) {
tagger.setVisible(visibleRefs, true);
}
} else {
userMarkupCollectionManager.remove(collectionReference.getId());
annotationPanel.removeCollection(collectionReference.getId());
tagger.setVisible(collection.getTagReferences(), false);
}
} catch (Exception e) {
errorHandler.showAndLogError("Error handling Annotation Collection!", e);
}
}
Aggregations