use of de.catma.util.Pair 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);
}
}
}
}
}
use of de.catma.util.Pair in project catma by forTEXT.
the class TaggerContextMenu 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();
Object parent = tagset;
if (!tag.getParentUuid().isEmpty()) {
parent = tagset.getTagDefinition(tag.getParentUuid());
}
MenuItem parentItem = entryToMenuItemMap.get(parent);
if (parentItem != null) {
addToMenuItem(parentItem, tagset, tag);
}
} else if (newValue == null) {
// removed
Pair<TagsetDefinition, TagDefinition> deleted = (Pair<TagsetDefinition, TagDefinition>) oldValue;
TagDefinition deletedTag = deleted.getSecond();
MenuItem menuItem = entryToMenuItemMap.get(deletedTag);
if (menuItem != null) {
contextMenu.removeItem(menuItem);
}
} else {
// update
TagDefinition tag = (TagDefinition) newValue;
TagsetDefinition tagset = (TagsetDefinition) oldValue;
MenuItem menuItem = entryToMenuItemMap.get(tag);
if (menuItem != null) {
menuItem.setText(createTagMenuItemCaption(tag));
}
}
}
};
tagManager.addPropertyChangeListener(TagManagerEvent.tagDefinitionChanged, tagChangedListener);
}
use of de.catma.util.Pair in project catma by forTEXT.
the class GitProjectHandler method cloneAndAddTagset.
public Pair<TagsetDefinition, String> cloneAndAddTagset(String tagsetId, String name, String commitMessage) throws IOException {
try (ILocalGitRepositoryManager localGitRepoManager = localGitRepositoryManager) {
// open the project root repo
localGitRepoManager.open(projectId, GitProjectManager.getProjectRootRepositoryName(projectId));
// add the submodule
File targetSubmodulePath = Paths.get(localGitRepoManager.getRepositoryWorkTree().toString(), TAGSET_SUBMODULES_DIRECTORY_NAME, tagsetId).toFile();
String tagsetRepoRemoteUrl = String.format("%s/%s/%s.git", CATMAPropertyKey.GitLabServerUrl.getValue(), projectId, tagsetId);
String rootRevisionHash = addAndCommitSubmoduleWithRetry(localGitRepoManager, targetSubmodulePath, tagsetRepoRemoteUrl, commitMessage);
// need to explicitly detach so that we can call checkout below
localGitRepoManager.detach();
// create and checkout submodule dev branch
GitTagsetHandler gitTagsetHandler = new GitTagsetHandler(localGitRepoManager, remoteGitServerManager, credentialsProvider);
gitTagsetHandler.checkout(projectId, tagsetId, ILocalGitRepositoryManager.DEFAULT_LOCAL_DEV_BRANCH, true);
// update cached permissions
if (!rolesPerResource.containsKey(tagsetId)) {
rolesPerResource.put(tagsetId, RBACRole.OWNER);
}
return new Pair<>(gitTagsetHandler.getTagset(projectId, tagsetId), rootRevisionHash);
}
}
use of de.catma.util.Pair in project catma by forTEXT.
the class ClientTagInstanceJSONSerializer method fromInstanceIDLineIDJSONArray.
public Pair<String, String> fromInstanceIDLineIDJSONArray(String instanceIDLineIDJSONArray) throws IOException {
ObjectMapper mapper = new ObjectMapper();
ArrayNode instanceIDArray = mapper.readValue(instanceIDLineIDJSONArray, ArrayNode.class);
JsonNode instanceIDJSON = instanceIDArray.get(0);
String instanceID = instanceIDJSON.get(SerializationField.instanceID.name()).asText();
String lineID = instanceIDJSON.get(SerializationField.lineID.name()).asText();
return new Pair<>(instanceID, lineID);
}
use of de.catma.util.Pair in project catma by forTEXT.
the class CorpusImporter method importCorpus.
/**
* !BACKGROUND THREAD! No direct UI code here!
*
* @param progressListener
* @param corpusFile
* @param documentMetadataList
* @param tempDir
* @param ui
* @param project
* @return
* @throws Exception
*/
public Void importCorpus(final ProgressListener progressListener, final File corpusFile, final List<CorpusImportDocumentMetadata> documentMetadataList, final String tempDir, final UI ui, final Project project) throws Exception {
progressListener.setProgress("Importing Corpus");
GZIPInputStream gzipIs = new GZIPInputStream(new FileInputStream(corpusFile));
try (TarArchiveInputStream taIs = new TarArchiveInputStream(gzipIs)) {
TarArchiveEntry entry = taIs.getNextTarEntry();
while (entry != null) {
final String entryName = entry.getName();
final String[] pathParts = entry.getName().split(Pattern.quote("/"));
final String documentIdPart = pathParts[2];
final String documentId = documentIdPart.substring(documentIdPart.indexOf("__") + 3);
final String idUri = "catma://" + documentId;
if (pathParts[3].equals("annotationcollections")) {
progressListener.setProgress("Importing Collection %1$s", pathParts[4]);
ui.accessSynchronously(() -> {
try {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
IOUtils.copy(taIs, buffer);
SourceDocument document = project.getSourceDocument(documentId);
Pair<AnnotationCollection, List<TagsetDefinitionImportStatus>> loadResult = project.loadAnnotationCollection(new ByteArrayInputStream(buffer.toByteArray()), document);
List<TagsetDefinitionImportStatus> tagsetDefinitionImportStatusList = loadResult.getSecond();
final AnnotationCollection annotationCollection = loadResult.getFirst();
Optional<TagsetDefinition> optIntrinsicTagset = annotationCollection.getTagLibrary().getTagsetDefinitions().stream().filter(tagsetDef -> tagsetDef.getName().equals("Intrinsic Markup")).findFirst();
if (optIntrinsicTagset.isPresent()) {
TagsetDefinition intrinsicTagset = optIntrinsicTagset.get();
List<TagReference> intrinsicAnnotations = annotationCollection.getTagReferences(intrinsicTagset);
if (!intrinsicAnnotations.isEmpty()) {
annotationCollection.removeTagReferences(intrinsicAnnotations);
}
annotationCollection.getTagLibrary().remove(intrinsicTagset);
tagsetDefinitionImportStatusList.stream().filter(status -> status.getTagset().equals(intrinsicTagset)).findFirst().ifPresent(status -> status.setDoImport(false));
}
tagsetDefinitionImportStatusList.stream().filter(status -> status.getTagset().isEmpty()).forEach(status -> status.setDoImport(false));
if (!annotationCollection.isEmpty()) {
project.importCollection(tagsetDefinitionImportStatusList, annotationCollection);
}
} catch (Exception e) {
Logger.getLogger(ProjectView.class.getName()).log(Level.SEVERE, "Error importing the CATMA 5 Corpus: " + entryName, e);
String errorMsg = e.getMessage();
if ((errorMsg == null) || (errorMsg.trim().isEmpty())) {
errorMsg = "";
}
Notification.show("Error", String.format("Error importing the CATMA 5 Corpus! " + "This Collection will be skipped!\n The underlying error message was:\n%1$s", errorMsg), Type.ERROR_MESSAGE);
}
});
} else {
final CorpusImportDocumentMetadata documentMetadata = documentMetadataList.stream().filter(metadata -> metadata.getSourceDocID().equals(idUri)).findFirst().orElse(null);
final Locale locale = LocaleUtils.toLocale(documentMetadata.getSourceDocLocale());
final boolean useApostrophe = Arrays.asList(documentMetadata.getSourceDocSepChars()).contains(String.valueOf(UploadFile.APOSTROPHE));
final String title = (documentMetadata.getSourceDocName() == null || documentMetadata.getSourceDocName().isEmpty()) ? documentId : documentMetadata.getSourceDocName();
progressListener.setProgress("Importing Document %1$s", title);
final File tempFile = new File(new File(tempDir), documentId);
if (tempFile.exists()) {
tempFile.delete();
}
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
IOUtils.copy(taIs, fos);
}
ui.accessSynchronously(() -> {
IDGenerator idGenerator = new IDGenerator();
IndexInfoSet indexInfoSet = new IndexInfoSet(Collections.emptyList(), useApostrophe ? Lists.newArrayList(UploadFile.APOSTROPHE) : Collections.emptyList(), locale);
TechInfoSet techInfoSet = new TechInfoSet(documentId, FileType.TEXT.getMimeType(), tempFile.toURI());
ContentInfoSet contentInfoSet = new ContentInfoSet(documentMetadata.getSourceDocAuthor(), documentMetadata.getSourceDocDescription(), documentMetadata.getSourceDocPublisher(), title);
techInfoSet.setCharset(Charset.forName("UTF-8"));
SourceDocumentInfo documentInfo = new SourceDocumentInfo(indexInfoSet, contentInfoSet, techInfoSet);
AbstractSourceContentHandler handler = null;
boolean loadIntrinsicMarkup = false;
if (entryName.endsWith("xml2")) {
handler = new XML2ContentHandler();
loadIntrinsicMarkup = true;
} else if (entryName.endsWith("xml")) {
handler = new OldXMLContentHandler();
loadIntrinsicMarkup = true;
} else {
handler = new StandardContentHandler();
}
handler.setSourceDocumentInfo(documentInfo);
SourceDocument document = new SourceDocument(documentId, handler);
try {
project.insert(document, false);
if (loadIntrinsicMarkup) {
final TagManager tagmanager = new TagManager(new TagLibrary());
XmlMarkupCollectionSerializationHandler markupHandler = new XmlMarkupCollectionSerializationHandler(tagmanager, (XML2ContentHandler) handler, project.getUser().getIdentifier());
try (FileInputStream fis = new FileInputStream(tempFile)) {
AnnotationCollection intrinsicMarkupCollection = markupHandler.deserialize(document, idGenerator.generateCollectionId(), fis);
Collection<TagsetImport> tagsetImports = new ArrayList<TagsetImport>();
String defaultIntrinsicXMLElmentsName = "Default Intrinsic XML Elements";
for (TagsetDefinition tagset : tagmanager.getTagLibrary()) {
if (!tagset.isEmpty()) {
TagsetDefinition targetTagset = project.getTagManager().getTagLibrary().getTagsetDefinition(tagset.getUuid());
boolean inProject = false;
if (targetTagset == null) {
targetTagset = tagset;
} else {
inProject = true;
}
String namespace = tagset.getName() == null ? "none" : tagset.getName();
if (tagset.getName() == null) {
tagset.setName(defaultIntrinsicXMLElmentsName);
}
TagsetImport tagsetImport = new TagsetImport(namespace, tagset, targetTagset, inProject ? TagsetImportState.WILL_BE_MERGED : TagsetImportState.WILL_BE_CREATED);
tagsetImports.add(tagsetImport);
}
}
// Creating Tagsets
tagsetImports.stream().filter(ti -> ti.getImportState().equals(TagsetImportState.WILL_BE_CREATED)).forEach(tagsetImport -> {
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);
}
}
});
// Merging Tagsets
tagsetImports.stream().filter(ti -> ti.getImportState().equals(TagsetImportState.WILL_BE_MERGED)).forEach(tagsetImport -> {
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));
}
});
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);
}
}
});
project.importCollection(Collections.emptyList(), intrinsicMarkupCollection);
}
if (tempFile.exists()) {
tempFile.delete();
}
}
} catch (Exception e) {
Logger.getLogger(ProjectView.class.getName()).log(Level.SEVERE, "Error importing the CATMA 5 Corpus: " + entryName, e);
String errorMsg = e.getMessage();
if ((errorMsg == null) || (errorMsg.trim().isEmpty())) {
errorMsg = "";
}
Notification.show("Error", String.format("Error importing the CATMA 5 Corpus! " + "This Document will be skipped!\n The underlying error message was:\n%1$s", errorMsg), Type.ERROR_MESSAGE);
}
});
}
entry = taIs.getNextTarEntry();
}
}
return null;
}
Aggregations