use of de.catma.tag.TagInstance in project catma by forTEXT.
the class TaggerView method tagInstanceAdded.
public void tagInstanceAdded(ClientTagInstance clientTagInstance) {
AnnotationCollection collection = annotationPanel.getSelectedEditableCollection();
if (collection == null) {
// shouldn't happen, but just in case
Notification.show("Info", "Please make sure you have an editable Collection available " + "and select this Collection as 'currently being edited'! " + "Your Annotation hasn't been saved!", Type.ERROR_MESSAGE);
} else {
TagLibrary tagLibrary = collection.getTagLibrary();
TagDefinition tagDef = tagLibrary.getTagDefinition(clientTagInstance.getTagDefinitionID());
TagInstance ti = new TagInstance(clientTagInstance.getInstanceID(), tagDef.getUuid(), project.getUser().getIdentifier(), ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), tagDef.getUserDefinedPropertyDefinitions(), tagDef.getTagsetDefinitionUuid());
List<TagReference> tagReferences = new ArrayList<TagReference>();
try {
String userMarkupCollectionUuid = collection.getId();
for (TextRange tr : clientTagInstance.getRanges()) {
Range r = new Range(tr.getStartPos(), tr.getEndPos());
TagReference ref = new TagReference(ti, sourceDocument.getUuid(), r, userMarkupCollectionUuid);
tagReferences.add(ref);
}
final Annotation annotation = new Annotation(ti, tagReferences, collection, tagLibrary.getTagPath(tagDef));
if (!tagDef.getUserDefinedPropertyDefinitions().isEmpty()) {
EditAnnotationPropertiesDialog editAnnotationPropertiesDialog = new EditAnnotationPropertiesDialog(project, annotation, new SaveCancelListener<List<Property>>() {
@Override
public void savePressed(List<Property> notOfInterest) {
userMarkupCollectionManager.addTagReferences(tagReferences, collection);
}
});
editAnnotationPropertiesDialog.show();
} else {
userMarkupCollectionManager.addTagReferences(tagReferences, collection);
}
} catch (URISyntaxException e) {
errorHandler.showAndLogError("Error adding Annotations!", e);
}
}
}
use of de.catma.tag.TagInstance in project catma by forTEXT.
the class JsonLdWebAnnotationTest method toTagReferenceList.
@Test
public void toTagReferenceList() throws Exception {
try (JGitRepoManager jGitRepoManager = new JGitRepoManager(this.catmaProperties.getProperty(RepositoryPropertyKey.GitBasedRepositoryBasePath.name()), this.catmaUser)) {
this.directoriesToDeleteOnTearDown.add(jGitRepoManager.getRepositoryBasePath());
// TODO: test with a hierarchy of tag definitions
HashMap<String, Object> getJsonLdWebAnnotationResult = JsonLdWebAnnotationTest.getJsonLdWebAnnotation(jGitRepoManager, this.gitLabServerManager, this.catmaUser);
JsonLdWebAnnotation jsonLdWebAnnotation = (JsonLdWebAnnotation) getJsonLdWebAnnotationResult.get("jsonLdWebAnnotation");
this.projectsToDeleteOnTearDown.add((String) getJsonLdWebAnnotationResult.get("projectUuid"));
assertNotNull(jsonLdWebAnnotation);
List<TagReference> tagReferences = jsonLdWebAnnotation.toTagReferenceList((String) getJsonLdWebAnnotationResult.get("projectUuid"), (String) getJsonLdWebAnnotationResult.get("userMarkupCollectionUuid"), jGitRepoManager, this.gitLabServerManager);
assertEquals(2, tagReferences.size());
for (TagReference tagReference : tagReferences) {
TagDefinition tagDefinition = tagReference.getTagDefinition();
TagInstance tagInstance = tagReference.getTagInstance();
assertEquals(getJsonLdWebAnnotationResult.get("tagsetDefinitionUuid"), tagDefinition.getTagsetDefinitionUuid());
assertEquals(getJsonLdWebAnnotationResult.get("tagDefinitionUuid"), tagDefinition.getUuid());
assertEquals("TAG_DEF", tagDefinition.getName());
assertEquals("", tagDefinition.getParentUuid());
PropertyDefinition[] systemPropertyDefinitions = tagDefinition.getSystemPropertyDefinitions().toArray(new PropertyDefinition[0]);
assertEquals(1, systemPropertyDefinitions.length);
assertEquals(getJsonLdWebAnnotationResult.get("systemPropertyDefinitionUuid"), systemPropertyDefinitions[0].getName());
assertEquals("catma_displaycolor", systemPropertyDefinitions[0].getName());
List<String> possibleSystemPropertyValues = systemPropertyDefinitions[0].getPossibleValueList();
assertEquals(2, possibleSystemPropertyValues.size());
assertArrayEquals(new String[] { "SYSPROP_VAL_1", "SYSPROP_VAL_2" }, possibleSystemPropertyValues.toArray(new String[0]));
PropertyDefinition[] userPropertyDefinitions = tagDefinition.getUserDefinedPropertyDefinitions().toArray(new PropertyDefinition[0]);
assertEquals(1, userPropertyDefinitions.length);
assertEquals(getJsonLdWebAnnotationResult.get("userPropertyDefinitionUuid"), userPropertyDefinitions[0].getName());
assertEquals("UPROP_DEF", userPropertyDefinitions[0].getName());
List<String> possibleUserPropertyValues = userPropertyDefinitions[0].getPossibleValueList();
assertEquals(2, possibleUserPropertyValues.size());
assertArrayEquals(new String[] { "UPROP_VAL_1", "UPROP_VAL_2" }, possibleUserPropertyValues.toArray(new String[0]));
assertEquals(getJsonLdWebAnnotationResult.get("tagInstanceUuid"), tagInstance.getUuid());
Property[] systemProperties = tagInstance.getSystemProperties().toArray(new Property[0]);
assertEquals(1, systemProperties.length);
assertEquals(systemPropertyDefinitions[0], systemProperties[0].getPropertyDefinition());
List<String> systemPropertyValues = systemProperties[0].getPropertyValueList();
assertEquals(1, systemPropertyValues.size());
assertEquals("SYSPROP_VAL_1", systemPropertyValues.get(0));
Property[] userProperties = tagInstance.getUserDefinedProperties().toArray(new Property[0]);
assertEquals(1, userProperties.length);
assertEquals(userPropertyDefinitions[0], userProperties[0].getPropertyDefinition());
List<String> userPropertyValues = userProperties[0].getPropertyValueList();
assertEquals(1, userPropertyValues.size());
assertEquals("UPROP_VAL_2", userPropertyValues.get(0));
}
assertEquals(new URI(String.format("http://catma.de/gitlab/%s/%s/%s", getJsonLdWebAnnotationResult.get("projectRootRepositoryName"), GitProjectHandler.SOURCE_DOCUMENT_SUBMODULES_DIRECTORY_NAME, getJsonLdWebAnnotationResult.get("sourceDocumentUuid"))), tagReferences.get(0).getTarget());
assertEquals(new Range(12, 18), tagReferences.get(0).getRange());
assertEquals(new URI(String.format("http://catma.de/gitlab/%s/%s/%s", getJsonLdWebAnnotationResult.get("projectRootRepositoryName"), GitProjectHandler.SOURCE_DOCUMENT_SUBMODULES_DIRECTORY_NAME, getJsonLdWebAnnotationResult.get("sourceDocumentUuid"))), tagReferences.get(1).getTarget());
assertEquals(new Range(41, 47), tagReferences.get(1).getRange());
}
}
use of de.catma.tag.TagInstance in project catma by forTEXT.
the class JsonLdWebAnnotationTest method getJsonLdWebAnnotation.
/**
* @return a HashMap<String, Object> with these keys:
* 'jsonLdWebAnnotation' - for the JsonLdWebAnnotation object
* 'projectUuid'
* --- following additional keys which are to be used when formatting EXPECTED_SERIALIZED_ANNOTATION ---:
* projectRootRepositoryName, tagsetDefinitionUuid, tagDefinitionUuid, userPropertyDefinitionUuid,
* systemPropertyDefinitionUuid, userMarkupCollectionUuid, tagInstanceUuid, sourceDocumentUuid
*/
public static HashMap<String, Object> getJsonLdWebAnnotation(JGitRepoManager jGitRepoManager, IRemoteGitServerManager gitLabServerManager, de.catma.user.User catmaUser) throws Exception {
try (JGitRepoManager localJGitRepoManager = jGitRepoManager) {
// caller should do the following:
// this.directoriesToDeleteOnTearDown.add(localJGitRepoManager.getRepositoryBasePath());
// create project
GitProjectManager gitProjectManager = new GitProjectManager(RepositoryPropertyKey.GitBasedRepositoryBasePath.getValue(), UserIdentification.userToMap(catmaUser.getIdentifier()));
String projectId = gitProjectManager.create("Test CATMA Project", "This is a test CATMA project");
// caller should do the following:
// this.projectsToDeleteOnTearDown.add(projectId);
GitProjectHandler gitProjectHandler = new GitProjectHandler(null, projectId, jGitRepoManager, gitLabServerManager);
// add new tagset to project
String tagsetId = gitProjectHandler.createTagset(null, "Test Tagset", null);
// add new source document to project
File originalSourceDocument = new File("testdocs/rose_for_emily.pdf");
File convertedSourceDocument = new File("testdocs/rose_for_emily.txt");
FileInputStream originalSourceDocumentStream = new FileInputStream(originalSourceDocument);
FileInputStream convertedSourceDocumentStream = new FileInputStream(convertedSourceDocument);
IndexInfoSet indexInfoSet = new IndexInfoSet();
indexInfoSet.setLocale(Locale.ENGLISH);
ContentInfoSet contentInfoSet = new ContentInfoSet("William Faulkner", "", "", "A Rose for Emily");
TechInfoSet techInfoSet = new TechInfoSet(FileType.TEXT, StandardCharsets.UTF_8, FileOSType.DOS, 705211438L);
SourceDocumentInfo sourceDocumentInfo = new SourceDocumentInfo(indexInfoSet, contentInfoSet, techInfoSet);
String sourceDocumentId = gitProjectHandler.createSourceDocument(null, originalSourceDocumentStream, originalSourceDocument.getName(), convertedSourceDocumentStream, convertedSourceDocument.getName(), null, null, sourceDocumentInfo);
// add new markup collection to project
String markupCollectionId = gitProjectHandler.createMarkupCollection(null, "Test Markup Collection", null, sourceDocumentId, "fakeSourceDocumentVersion");
// commit the changes to the project root repo (addition of tagset, source document and markup collection
// submodules)
String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
localJGitRepoManager.open(projectId, projectRootRepositoryName);
localJGitRepoManager.commit(String.format("Adding new tagset %s, source document %s and markup collection %s", tagsetId, sourceDocumentId, markupCollectionId), "Test Committer", "testcommitter@catma.de");
// can't call open on an attached instance
localJGitRepoManager.detach();
// construct TagDefinition object
IDGenerator idGenerator = new IDGenerator();
List<String> systemPropertyPossibleValues = Arrays.asList("SYSPROP_VAL_1", "SYSPROP_VAL_2");
PropertyDefinition systemPropertyDefinition = new PropertyDefinition(PropertyDefinition.SystemPropertyName.catma_displaycolor.toString(), systemPropertyPossibleValues);
List<String> userPropertyPossibleValues = Arrays.asList("UPROP_VAL_1", "UPROP_VAL_2");
PropertyDefinition userPropertyDefinition = new PropertyDefinition("UPROP_DEF", userPropertyPossibleValues);
String tagDefinitionUuid = idGenerator.generate();
TagDefinition tagDefinition = new TagDefinition(null, tagDefinitionUuid, "TAG_DEF", new Version(), null, null, tagsetId);
tagDefinition.addSystemPropertyDefinition(systemPropertyDefinition);
tagDefinition.addUserDefinedPropertyDefinition(userPropertyDefinition);
// call createTagDefinition
// NB: in this case we know that the tagset submodule is on the master branch tip, ie: not in a detached
// head state, so it's safe to make changes to the submodule and commit them
// TODO: createTagDefinition should probably do some validation and fail fast if the tagset submodule is in
// a detached head state - in that case the submodule would need to be updated first
// see the "Updating a submodule in-place in the container" scenario at
// https://medium.com/@porteneuve/mastering-git-submodules-34c65e940407
GitTagsetHandler gitTagsetHandler = new GitTagsetHandler(localJGitRepoManager, gitLabServerManager);
String returnedTagDefinitionId = gitTagsetHandler.createOrUpdateTagDefinition(projectId, tagsetId, tagDefinition);
assertNotNull(returnedTagDefinitionId);
assert returnedTagDefinitionId.startsWith("CATMA_");
// the JGitRepoManager instance should always be in a detached state after GitTagsetHandler calls return
assertFalse(localJGitRepoManager.isAttached());
assertEquals(tagDefinitionUuid, returnedTagDefinitionId);
// commit and push submodule changes (creation of tag definition)
// TODO: add methods to JGitRepoManager to do this
localJGitRepoManager.open(projectId, projectRootRepositoryName);
Repository projectRootRepository = localJGitRepoManager.getGitApi().getRepository();
String tagsetSubmodulePath = String.format("%s/%s", GitProjectHandler.TAGSET_SUBMODULES_DIRECTORY_NAME, tagsetId);
Repository tagsetSubmoduleRepository = SubmoduleWalk.getSubmoduleRepository(projectRootRepository, tagsetSubmodulePath);
Git submoduleGit = new Git(tagsetSubmoduleRepository);
submoduleGit.add().addFilepattern(tagDefinitionUuid).call();
submoduleGit.commit().setMessage(String.format("Adding tag definition %s", tagDefinitionUuid)).setCommitter("Test Committer", "testcommitter@catma.de").call();
submoduleGit.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(gitLabServerManager.getUsername(), gitLabServerManager.getPassword())).call();
tagsetSubmoduleRepository.close();
submoduleGit.close();
// commit and push project root repo changes (update of tagset submodule)
localJGitRepoManager.getGitApi().add().addFilepattern(tagsetSubmodulePath).call();
localJGitRepoManager.commit(String.format("Updating tagset %s", tagsetId), "Test Committer", "testcommitter@catma.de");
// construct TagInstance object
Property systemProperty = new Property(systemPropertyDefinition, Collections.singleton("SYSPROP_VAL_1"));
Property userProperty = new Property(userPropertyDefinition, Collections.singleton("UPROP_VAL_2"));
String tagInstanceUuid = idGenerator.generate();
TagInstance tagInstance = new TagInstance(tagInstanceUuid, tagDefinition);
tagInstance.addSystemProperty(systemProperty);
tagInstance.addUserDefinedProperty(userProperty);
// construct JsonLdWebAnnotation object
String sourceDocumentUri = String.format("http://catma.de/gitlab/%s/%s/%s", projectRootRepositoryName, GitProjectHandler.SOURCE_DOCUMENT_SUBMODULES_DIRECTORY_NAME, sourceDocumentId);
Range range1 = new Range(12, 18);
Range range2 = new Range(41, 47);
List<TagReference> tagReferences = new ArrayList<>(Arrays.asList(new TagReference(tagInstance, sourceDocumentUri, range1, markupCollectionId), new TagReference(tagInstance, sourceDocumentUri, range2, markupCollectionId)));
JsonLdWebAnnotation jsonLdWebAnnotation = new JsonLdWebAnnotation("http://catma.de/gitlab", projectId, tagReferences);
HashMap<String, Object> returnValue = new HashMap<>();
returnValue.put("jsonLdWebAnnotation", jsonLdWebAnnotation);
returnValue.put("projectRootRepositoryName", projectRootRepositoryName);
returnValue.put("projectUuid", projectId);
returnValue.put("tagsetDefinitionUuid", tagsetId);
returnValue.put("tagDefinitionUuid", tagDefinitionUuid);
returnValue.put("userMarkupCollectionUuid", markupCollectionId);
returnValue.put("tagInstanceUuid", tagInstanceUuid);
returnValue.put("sourceDocumentUuid", sourceDocumentId);
return returnValue;
}
}
use of de.catma.tag.TagInstance in project catma by forTEXT.
the class KwicPanel method annotateSelection.
@SuppressWarnings("unchecked")
private void annotateSelection(Set<QueryResultRow> selectedRows, WizardContext result) throws Exception {
List<Property> properties = (List<Property>) result.get(AnnotationWizardContextKey.PROPERTIES);
Map<String, AnnotationCollectionReference> collectionRefsByDocId = (Map<String, AnnotationCollectionReference>) result.get(AnnotationWizardContextKey.COLLECTIONREFS_BY_DOCID);
TagDefinition tag = (TagDefinition) result.get(AnnotationWizardContextKey.TAG);
AnnotationCollectionManager collectionManager = new AnnotationCollectionManager(project);
for (AnnotationCollectionReference ref : collectionRefsByDocId.values()) {
collectionManager.add(project.getUserMarkupCollection(ref));
}
for (QueryResultRow row : selectedRows) {
AnnotationCollectionReference collectionRef = collectionRefsByDocId.get(row.getSourceDocumentId());
TagInstance tagInstance = new TagInstance(idGenerator.generate(), tag.getUuid(), project.getUser().getIdentifier(), ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), tag.getUserDefinedPropertyDefinitions(), tag.getTagsetDefinitionUuid());
List<TagReference> tagReferences = new ArrayList<TagReference>();
for (Property protoProp : properties) {
tagInstance.getUserDefinedPropetyByUuid(protoProp.getPropertyDefinitionId()).setPropertyValueList(protoProp.getPropertyValueList());
}
Set<Range> ranges = row.getRanges();
for (Range range : ranges) {
TagReference tagReference = new TagReference(tagInstance, row.getSourceDocumentId(), range, collectionRef.getId());
tagReferences.add(tagReference);
}
collectionManager.addTagReferences(tagReferences, collectionRef.getId());
}
}
use of de.catma.tag.TagInstance in project catma by forTEXT.
the class TagInstanceInfoHTMLSerializer method toHTML.
public String toHTML(Annotation tagInstanceInfo) {
// $NON-NLS-1$
Element table = new Element("table");
// $NON-NLS-1$ //$NON-NLS-2$
table.addAttribute(new Attribute("class", "taginstanceinfo"));
// $NON-NLS-1$
Element tagPathField = new Element("td");
// $NON-NLS-1$ //$NON-NLS-2$
tagPathField.addAttribute(new Attribute("colspan", "2"));
// $NON-NLS-1$ //$NON-NLS-2$
tagPathField.addAttribute(new Attribute("class", "taginstanceinfo-caption"));
tagPathField.appendChild(tagInstanceInfo.getTagPath());
// $NON-NLS-1$
addRow(table, tagPathField, "");
// $NON-NLS-1$
addRow(table, "Collection", tagInstanceInfo.getUserMarkupCollection().getName());
TagInstance tagInstance = tagInstanceInfo.getTagInstance();
String tagId = tagInstance.getTagDefinitionId();
String author = tagInstance.getAuthor();
TagDefinition tag = project.getTagManager().getTagLibrary().getTagDefinition(tagId);
// $NON-NLS-1$
addRow(table, "Author", author);
if (!tagInstance.getUserDefinedProperties().isEmpty()) {
Element propertyField = new Element("td");
// $NON-NLS-1$ //$NON-NLS-2$
propertyField.addAttribute(new Attribute("class", "taginstanceinfo-caption"));
propertyField.appendChild("Properties");
// $NON-NLS-1$
addRow(table, propertyField, "");
for (Property property : tagInstance.getUserDefinedProperties()) {
List<String> values = property.getPropertyValueList();
PropertyDefinition propertyDefinition = tag.getPropertyDefinitionByUuid(property.getPropertyDefinitionId());
if (propertyDefinition != null) {
// may be deleted already
addRow(table, tag.getPropertyDefinitionByUuid(property.getPropertyDefinitionId()).getName(), // $NON-NLS-1$
values.isEmpty() ? "" : values.size() > 1 ? values.toString() : values.get(0));
}
}
}
return table.toXML();
}
Aggregations