use of de.catma.util.IDGenerator in project catma by forTEXT.
the class TeiUserMarkupCollectionDeserializer method createTagInstance.
private TagInstance createTagInstance(String tagInstanceID) {
TeiElement tagInstanceElement = teiDocument.getElementByID(tagInstanceID);
TagDefinition tagDefinition = tagLibrary.getTagDefinition(tagInstanceElement.getAttributeValue(Attribute.type));
if (!old2newTagInstanceIDs.containsKey(tagInstanceElement.getID())) {
old2newTagInstanceIDs.put(tagInstanceElement.getID(), new IDGenerator().generate());
}
final TagInstance tagInstance = new TagInstance(old2newTagInstanceIDs.get(tagInstanceElement.getID()), tagDefinition.getUuid(), tagDefinition.getAuthor(), ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), tagDefinition.getUserDefinedPropertyDefinitions(), tagDefinition.getTagsetDefinitionUuid());
Nodes systemPropertyElements = tagInstanceElement.getChildNodes(TeiElementName.f, AttributeValue.f_name_catma_system_property.getStartsWithFilter());
addProperties(tagDefinition, new AddPropertyHandler() {
public void addProperty(Property property) {
tagInstance.addSystemProperty(property);
}
}, systemPropertyElements);
Nodes userDefinedPropertyElements = tagInstanceElement.getChildNodes(TeiElementName.f, AttributeValue.f_name_catma_system_property.getNotStartsWithFilter());
addProperties(tagDefinition, new AddPropertyHandler() {
public void addProperty(Property property) {
tagInstance.addUserDefinedProperty(property);
}
}, userDefinedPropertyElements);
return tagInstance;
}
use of de.catma.util.IDGenerator in project catma by forTEXT.
the class UploadStep method handleZipFile.
private void handleZipFile(UploadFile uploadFile) throws IOException {
URI uri = uploadFile.getTempFilename();
ZipFile zipFile = new ZipFile(uri.getPath());
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
String tempDir = ((CatmaApplication) UI.getCurrent()).accquirePersonalTempFolder();
IDGenerator idGenerator = new IDGenerator();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
String fileName = FilenameUtils.getName(entry.getName());
if (fileName.startsWith(".")) {
// we treat them as hidden files, that's probably what most users would expect
continue;
}
final String fileId = idGenerator.generateDocumentId();
File entryDestination = new File(tempDir, fileId);
if (entryDestination.exists()) {
entryDestination.delete();
}
entryDestination.getParentFile().mkdirs();
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
try (BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entryDestination))) {
IOUtils.copy(bis, bos);
}
try (FileInputStream fis = new FileInputStream(entryDestination)) {
String type = tika.detect(fis, fileName);
UploadFile extractedUploadFile = new UploadFile(fileId, entryDestination.toURI(), fileName, type, entry.getSize());
fileList.add(extractedUploadFile);
fileDataProvider.refreshAll();
stepChangeListener.stepChanged(this);
}
}
}
ZipFile.closeQuietly(zipFile);
}
use of de.catma.util.IDGenerator in project catma by forTEXT.
the class TagsetCSVExportStreamSource 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()) {
try {
File tempFile = File.createTempFile(new IDGenerator().generate() + "_TagLibrary_Export", "xml");
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile))) {
try (CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL.withDelimiter(';'))) {
csvPrinter.print("Tagset");
csvPrinter.print("Tagset ID");
csvPrinter.print("Tag");
csvPrinter.print("Tag ID");
csvPrinter.print("Tag Path");
csvPrinter.print("Tag Parent ID");
csvPrinter.print("Tag Color");
csvPrinter.print("Tag Author");
csvPrinter.print("Project Name");
csvPrinter.print("Project ID");
csvPrinter.print("Tag Properties & Values & Property ID");
csvPrinter.println();
for (TagsetDefinition tagset : tagsets) {
Collection<TagDefinition> sortedTags = tagset.stream().sorted((t1, t2) -> {
if (t1.getName().equals(t2.getName())) {
return t1.getUuid().compareTo(t2.getUuid());
}
return t1.getName().compareTo(t2.getName());
}).collect(Collectors.toList());
for (TagDefinition tag : sortedTags) {
csvPrinter.print(tagset.getName());
csvPrinter.print(tagset.getUuid());
csvPrinter.print(tag.getName());
csvPrinter.print(tag.getUuid());
csvPrinter.print(tagset.getTagPath(tag));
csvPrinter.print(tag.getParentUuid());
csvPrinter.print("#" + ColorConverter.toHex(tag.getColor()));
csvPrinter.print(tag.getAuthor());
csvPrinter.print(project.getName());
csvPrinter.print(project.getProjectId());
ArrayList<PropertyDefinition> sortedProperties = new ArrayList<>(tag.getUserDefinedPropertyDefinitions());
Collections.sort(sortedProperties, (p1, p2) -> {
if (p1.getName().equals(p2.getName())) {
return p1.getUuid().compareTo(p2.getUuid());
}
return p1.getName().compareTo(p2.getName());
});
for (PropertyDefinition propertyDef : sortedProperties) {
csvPrinter.print(propertyDef.getName() + propertyDef.getPossibleValueList().stream().sorted().collect(Collectors.toList()) + " " + propertyDef.getUuid());
}
csvPrinter.println();
}
}
}
}
return new FileInputStream(tempFile);
} catch (Exception e) {
((ErrorHandler) ui).showAndLogError("Error exporting Tagsets to XML!", e);
}
}
return null;
}
use of de.catma.util.IDGenerator in project catma by forTEXT.
the class ImportIntrinsicMarkupStep method enter.
@Override
public void enter(boolean back) {
if (back) {
return;
}
contentPanel.setEnabled(false);
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
@SuppressWarnings("unchecked") final ArrayList<UploadFile> files = new ArrayList<UploadFile>(((Collection<UploadFile>) wizardContext.get(DocumentWizard.WizardContextKey.UPLOAD_FILE_LIST)).stream().filter(uploadFile -> uploadFile.getMimetype().equals(FileType.XML2.getMimeType())).collect(Collectors.toList()));
final TagManager tagmanager = new TagManager(new TagLibrary());
BackgroundServiceProvider backgroundServiceProvider = (BackgroundServiceProvider) UI.getCurrent();
backgroundServiceProvider.submit("inspecting-intrinsic-markup", new DefaultProgressCallable<List<UploadFile>>() {
@Override
public List<UploadFile> call() throws Exception {
IDGenerator idGenerator = new IDGenerator();
for (UploadFile uploadFile : files) {
XML2ContentHandler contentHandler = new XML2ContentHandler();
SourceDocument doc = new SourceDocument(uploadFile.getUuid(), contentHandler);
SourceDocumentInfo documentInfo = new SourceDocumentInfo();
TechInfoSet techInfoSet = new TechInfoSet();
techInfoSet.setURI(uploadFile.getTempFilename());
documentInfo.setTechInfoSet(techInfoSet);
contentHandler.setSourceDocumentInfo(documentInfo);
XmlMarkupCollectionSerializationHandler handler = new XmlMarkupCollectionSerializationHandler(tagmanager, contentHandler, project.getUser().getIdentifier());
try (FileInputStream fis = new FileInputStream(new File(uploadFile.getTempFilename()))) {
AnnotationCollection collection = handler.deserialize(doc, idGenerator.generateCollectionId(), fis);
uploadFile.setIntrinsicMarkupCollection(collection);
}
}
return files;
}
}, new ExecutionListener<List<UploadFile>>() {
@Override
public void done(List<UploadFile> result) {
contentPanel.setEnabled(true);
progressBar.setVisible(false);
progressBar.setIndeterminate(false);
fileList.clear();
fileList.addAll(result);
fileDataProvider.refreshAll();
tagsetImportList.clear();
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);
tagsetImportList.add(tagsetImport);
}
}
tagsetDataProvider.refreshAll();
wizardContext.put(DocumentWizard.WizardContextKey.TAGSET_IMPORT_LIST, tagsetImportList);
if (stepChangeListener != null) {
stepChangeListener.stepChanged(ImportIntrinsicMarkupStep.this);
}
}
@Override
public void error(Throwable t) {
Logger.getLogger(ImportIntrinsicMarkupStep.class.getName()).log(Level.SEVERE, "Error inspecting files", t);
String errorMsg = t.getMessage();
if ((errorMsg == null) || (errorMsg.trim().isEmpty())) {
errorMsg = "";
}
Notification.show("Error", String.format("Error inspecting the contents! " + "\n The underlying error message was:\n%1$s", errorMsg), Type.ERROR_MESSAGE);
}
});
}
use of de.catma.util.IDGenerator 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;
}
Aggregations