use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.
the class TagsetXMLExportStreamSource method getStream.
@Override
public InputStream getStream() {
final UI ui = UI.getCurrent();
Set<TagsetDefinition> tagsets = tagsetsSupplier.get();
Project project = projectSupplier.get();
if (tagsets != null && !tagsets.isEmpty()) {
TeiDocumentFactory teiDocumentFactory = new TeiDocumentFactory();
try {
final TeiDocument teiDocument = teiDocumentFactory.createEmptyDocument(null);
final TeiTagLibrarySerializer teiTagSerializer = new TeiTagLibrarySerializer(teiDocument);
final TagManager tagManager = new TagManager(new TagLibrary());
tagsets.forEach(tagset -> tagManager.addTagsetDefinition(tagset));
final ContentInfoSet contentInfoSet = new ContentInfoSet(project.getUser().toString(), project.getDescription(), project.getUser().toString(), project.getName());
teiDocument.getTeiHeader().setValues(contentInfoSet);
teiTagSerializer.serialize(tagManager.getTagLibrary());
File tempFile = File.createTempFile(new IDGenerator().generate() + "_TagLibrary_Export", "xml");
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
teiDocument.printXmlDocument(fos);
}
return new FileInputStream(tempFile);
} catch (Exception e) {
((ErrorHandler) ui).showAndLogError("Error exporting Tagsets to XML!", e);
}
}
return null;
}
use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.
the class GitMarkupCollectionHandler method getCollection.
public AnnotationCollection getCollection(String projectId, String collectionId, TagLibrary tagLibrary, ProgressListener progressListener, boolean hasWritePermission, Function<String, Boolean> hasTagsetIdReadPermissionGetter) throws Exception {
try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
localGitRepoManager.open(projectId, projectRootRepositoryName);
String markupCollectionSubmoduleRelDir = GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME + "/" + collectionId;
File markupCollectionSubmoduleAbsPath = new File(localGitRepoManager.getRepositoryWorkTree().toString(), markupCollectionSubmoduleRelDir);
String markupCollectionRevisionHash = localGitRepoManager.getSubmoduleHeadRevisionHash(markupCollectionSubmoduleRelDir);
// can't call open on an attached instance
localGitRepoManager.detach();
File markupCollectionHeaderFile = new File(markupCollectionSubmoduleAbsPath, HEADER_FILE_NAME);
String serializedMarkupCollectionHeaderFile = FileUtils.readFileToString(markupCollectionHeaderFile, StandardCharsets.UTF_8);
GitMarkupCollectionHeader markupCollectionHeader = new SerializationHelper<GitMarkupCollectionHeader>().deserialize(serializedMarkupCollectionHeaderFile, GitMarkupCollectionHeader.class);
ContentInfoSet contentInfoSet = new ContentInfoSet(markupCollectionHeader.getAuthor(), markupCollectionHeader.getDescription(), markupCollectionHeader.getPublisher(), markupCollectionHeader.getName());
AtomicInteger counter = new AtomicInteger();
ArrayList<TagReference> tagReferences = this.openTagReferences(projectId, collectionId, contentInfoSet.getTitle(), markupCollectionSubmoduleAbsPath, progressListener, counter);
// handle orphan Annotations
ArrayListMultimap<TagInstance, TagReference> tagInstances = ArrayListMultimap.create();
Set<String> orphanAnnotationIds = new HashSet<>();
Iterator<TagReference> tagReferenceIterator = tagReferences.iterator();
while (tagReferenceIterator.hasNext()) {
TagReference tagReference = tagReferenceIterator.next();
if (!orphanAnnotationIds.contains(tagReference.getTagInstanceId())) {
String tagsetId = tagReference.getTagInstance().getTagsetId();
boolean readPermission = hasTagsetIdReadPermissionGetter.apply(tagsetId);
TagsetDefinition tagset = tagLibrary.getTagsetDefinition(tagsetId);
String tagId = tagReference.getTagDefinitionId();
if (readPermission && (tagset == null || tagset.isDeleted(tagId))) {
// Tag/Tagset has been deleted, we remove the stale Annotation as well
orphanAnnotationIds.add(tagReference.getTagInstanceId());
tagReferenceIterator.remove();
} else {
// other orphan Annotations get ignored upon indexing
// until the corresponding Tag or its "deletion" info come along
tagInstances.put(tagReference.getTagInstance(), tagReference);
}
}
}
if (hasWritePermission) {
removeTagInstances(projectId, collectionId, orphanAnnotationIds);
}
// handle orphan Properties
if (hasWritePermission) {
for (TagInstance tagInstance : tagInstances.keySet()) {
TagsetDefinition tagset = tagLibrary.getTagsetDefinition(tagInstance.getTagsetId());
if (tagset != null) {
Collection<Property> properties = tagInstance.getUserDefinedProperties();
for (Property property : new HashSet<>(properties)) {
// deleted property?
if (tagset.isDeleted(property.getPropertyDefinitionId())) {
// yes, we remove the stale property
tagInstance.removeUserDefinedProperty(property.getPropertyDefinitionId());
// and save the change
JsonLdWebAnnotation annotation = new JsonLdWebAnnotation(CATMAPropertyKey.GitLabServerUrl.getValue(), projectId, tagInstances.get(tagInstance), tagLibrary);
createTagInstance(projectId, collectionId, annotation);
}
}
}
}
}
AnnotationCollection userMarkupCollection = new AnnotationCollection(collectionId, contentInfoSet, tagLibrary, tagReferences, markupCollectionHeader.getSourceDocumentId(), markupCollectionHeader.getSourceDocumentVersion());
userMarkupCollection.setRevisionHash(markupCollectionRevisionHash);
return userMarkupCollection;
}
}
use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.
the class GitMarkupCollectionHandler method getCollectionConflict.
public CollectionConflict getCollectionConflict(String projectId, String collectionId) throws Exception {
try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
localGitRepoManager.open(projectId, projectRootRepositoryName);
String collectionSubmoduleRelDir = GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME + "/" + collectionId;
File collectionSubmoduleAbsPath = new File(localGitRepoManager.getRepositoryWorkTree().toString(), collectionSubmoduleRelDir);
localGitRepoManager.detach();
String collectionGitRepositoryName = projectRootRepositoryName + "/" + collectionSubmoduleRelDir;
localGitRepoManager.open(projectId, collectionGitRepositoryName);
Status status = localGitRepoManager.getStatus();
File collectionHeaderFile = new File(collectionSubmoduleAbsPath, HEADER_FILE_NAME);
String serializedCollectionHeaderFile = FileUtils.readFileToString(collectionHeaderFile, StandardCharsets.UTF_8);
CollectionConflict collectionConflict;
if (status.getConflictingStageState().containsKey(HEADER_FILE_NAME)) {
GitMarkupCollectionHeader gitCollectionHeader = resolveCollectionHeaderConflict(serializedCollectionHeaderFile, status.getConflictingStageState().get(HEADER_FILE_NAME));
serializedCollectionHeaderFile = new SerializationHelper<GitMarkupCollectionHeader>().serialize(gitCollectionHeader);
localGitRepoManager.add(collectionHeaderFile.getAbsoluteFile(), serializedCollectionHeaderFile.getBytes(StandardCharsets.UTF_8));
ContentInfoSet contentInfoSet = new ContentInfoSet(gitCollectionHeader.getAuthor(), gitCollectionHeader.getDescription(), gitCollectionHeader.getPublisher(), gitCollectionHeader.getName());
collectionConflict = new CollectionConflict(projectId, collectionId, contentInfoSet, gitCollectionHeader.getSourceDocumentId());
collectionConflict.setHeaderConflict(true);
status = localGitRepoManager.getStatus();
} else {
GitMarkupCollectionHeader gitCollectionHeader = new SerializationHelper<GitMarkupCollectionHeader>().deserialize(serializedCollectionHeaderFile, GitMarkupCollectionHeader.class);
ContentInfoSet contentInfoSet = new ContentInfoSet(gitCollectionHeader.getAuthor(), gitCollectionHeader.getDescription(), gitCollectionHeader.getPublisher(), gitCollectionHeader.getName());
collectionConflict = new CollectionConflict(projectId, collectionId, contentInfoSet, gitCollectionHeader.getSourceDocumentId());
}
for (Entry<String, StageState> entry : status.getConflictingStageState().entrySet()) {
String relativeAnnotationPathname = entry.getKey();
String absAnnotationPathname = collectionSubmoduleAbsPath + "/" + relativeAnnotationPathname;
StageState stageState = entry.getValue();
switch(stageState) {
case BOTH_MODIFIED:
{
String serializedConflictingAnnotation = FileUtils.readFileToString(new File(absAnnotationPathname), StandardCharsets.UTF_8);
AnnotationConflict annotationConflict = getBothModifiedAnnotationConflict(projectId, collectionId, serializedConflictingAnnotation);
collectionConflict.addAnnotationConflict(annotationConflict);
break;
}
case DELETED_BY_THEM:
{
// them is the user on the dev branch here
// in this case the file comes from us (the team on the master branch)
String serializedConflictingAnnotation = FileUtils.readFileToString(new File(absAnnotationPathname), StandardCharsets.UTF_8);
AnnotationConflict annotationConflict = getDeleteByThemAnnotationConflict(projectId, collectionId, serializedConflictingAnnotation);
collectionConflict.addAnnotationConflict(annotationConflict);
break;
}
case DELETED_BY_US:
{
// us is the team on the master branch here
// in this case the file comes from them (the user on the dev branch)
String serializedConflictingAnnotation = FileUtils.readFileToString(new File(absAnnotationPathname), StandardCharsets.UTF_8);
AnnotationConflict annotationConflict = getDeleteByUsAnnotationConflict(projectId, collectionId, serializedConflictingAnnotation);
collectionConflict.addAnnotationConflict(annotationConflict);
break;
}
default:
{
// TODO:
System.out.println("not handled");
}
}
}
return collectionConflict;
}
}
use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.
the class GitMarkupCollectionHandler method getCollectionReference.
public AnnotationCollectionReference getCollectionReference(String projectId, String markupCollectionId) throws Exception {
try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
localGitRepoManager.open(projectId, projectRootRepositoryName);
String markupCollectionSubmoduleRelDir = String.format("%s/%s", GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME, markupCollectionId);
File markupCollectionSubmoduleAbsPath = new File(localGitRepoManager.getRepositoryWorkTree().toString(), markupCollectionSubmoduleRelDir);
String markupCollectionRevisionHash = localGitRepoManager.getSubmoduleHeadRevisionHash(markupCollectionSubmoduleRelDir);
// can't call open on an attached instance
localGitRepoManager.detach();
File markupCollectionHeaderFile = new File(markupCollectionSubmoduleAbsPath, HEADER_FILE_NAME);
String serializedMarkupCollectionHeaderFile = FileUtils.readFileToString(markupCollectionHeaderFile, StandardCharsets.UTF_8);
GitMarkupCollectionHeader markupCollectionHeader = new SerializationHelper<GitMarkupCollectionHeader>().deserialize(serializedMarkupCollectionHeaderFile, GitMarkupCollectionHeader.class);
ContentInfoSet contentInfoSet = new ContentInfoSet(markupCollectionHeader.getAuthor(), markupCollectionHeader.getDescription(), markupCollectionHeader.getPublisher(), markupCollectionHeader.getName());
return new AnnotationCollectionReference(markupCollectionId, markupCollectionRevisionHash, contentInfoSet, markupCollectionHeader.getSourceDocumentId(), markupCollectionHeader.getSourceDocumentVersion());
}
}
use of de.catma.document.source.ContentInfoSet in project catma by forTEXT.
the class GitMarkupCollectionHandler method updateCollection.
public String updateCollection(String projectId, AnnotationCollectionReference collectionRef) throws Exception {
try (ILocalGitRepositoryManager localGitRepoManager = this.localGitRepositoryManager) {
String projectRootRepositoryName = GitProjectManager.getProjectRootRepositoryName(projectId);
String collectionGitRepositoryName = projectRootRepositoryName + "/" + GitProjectHandler.ANNOTATION_COLLECTION_SUBMODULES_DIRECTORY_NAME + "/" + collectionRef.getId();
localGitRepoManager.open(projectId, collectionGitRepositoryName);
ContentInfoSet contentInfoSet = collectionRef.getContentInfoSet();
File targetHeaderFile = new File(localGitRepoManager.getRepositoryWorkTree(), HEADER_FILE_NAME);
GitMarkupCollectionHeader header = new GitMarkupCollectionHeader(contentInfoSet.getTitle(), contentInfoSet.getDescription(), collectionRef.getSourceDocumentId(), collectionRef.getSourceDocumentRevisiohHash());
SerializationHelper<GitMarkupCollectionHeader> serializationHelper = new SerializationHelper<>();
String serializedHeader = serializationHelper.serialize(header);
localGitRepoManager.add(targetHeaderFile, // TODO: why are we doing this and then calling addAndCommit?
serializedHeader.getBytes(StandardCharsets.UTF_8));
String collectionRevision = localGitRepoManager.addAndCommit(targetHeaderFile, serializedHeader.getBytes(StandardCharsets.UTF_8), String.format("Updated metadata of Collection %1$s with ID %2$s", collectionRef.getName(), collectionRef.getId()), remoteGitServerManager.getUsername(), remoteGitServerManager.getEmail());
return collectionRevision;
}
}
Aggregations