Search in sources :

Example 1 with IDGenerator

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;
}
Also used : TagDefinition(de.catma.tag.TagDefinition) TagInstance(de.catma.tag.TagInstance) IDGenerator(de.catma.util.IDGenerator) Property(de.catma.tag.Property) Nodes(nu.xom.Nodes)

Example 2 with IDGenerator

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);
}
Also used : URI(java.net.URI) FileInputStream(java.io.FileInputStream) CatmaApplication(de.catma.ui.CatmaApplication) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IDGenerator(de.catma.util.IDGenerator) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) Html5File(com.vaadin.ui.Html5File) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with IDGenerator

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;
}
Also used : PropertyDefinition(de.catma.tag.PropertyDefinition) Project(de.catma.project.Project) Collection(java.util.Collection) UI(com.vaadin.ui.UI) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) FileInputStream(java.io.FileInputStream) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) ColorConverter(de.catma.util.ColorConverter) CSVFormat(org.apache.commons.csv.CSVFormat) ErrorHandler(de.catma.ui.module.main.ErrorHandler) TagDefinition(de.catma.tag.TagDefinition) TagsetDefinition(de.catma.tag.TagsetDefinition) OutputStreamWriter(java.io.OutputStreamWriter) IDGenerator(de.catma.util.IDGenerator) Collections(java.util.Collections) CSVPrinter(org.apache.commons.csv.CSVPrinter) InputStream(java.io.InputStream) StreamSource(com.vaadin.server.StreamResource.StreamSource) TagDefinition(de.catma.tag.TagDefinition) ArrayList(java.util.ArrayList) PropertyDefinition(de.catma.tag.PropertyDefinition) FileInputStream(java.io.FileInputStream) CSVPrinter(org.apache.commons.csv.CSVPrinter) TagsetDefinition(de.catma.tag.TagsetDefinition) Project(de.catma.project.Project) UI(com.vaadin.ui.UI) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) IDGenerator(de.catma.util.IDGenerator)

Example 4 with IDGenerator

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);
        }
    });
}
Also used : SourceDocumentInfo(de.catma.document.source.SourceDocumentInfo) ArrayList(java.util.ArrayList) BackgroundServiceProvider(de.catma.backgroundservice.BackgroundServiceProvider) ArrayList(java.util.ArrayList) List(java.util.List) TechInfoSet(de.catma.document.source.TechInfoSet) TagLibrary(de.catma.tag.TagLibrary) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) SourceDocument(de.catma.document.source.SourceDocument) XML2ContentHandler(de.catma.document.source.contenthandler.XML2ContentHandler) FileInputStream(java.io.FileInputStream) TagsetDefinition(de.catma.tag.TagsetDefinition) TagManager(de.catma.tag.TagManager) XmlMarkupCollectionSerializationHandler(de.catma.serialization.intrinsic.xml.XmlMarkupCollectionSerializationHandler) IDGenerator(de.catma.util.IDGenerator) File(java.io.File)

Example 5 with IDGenerator

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;
}
Also used : TagLibrary(de.catma.tag.TagLibrary) TeiDocumentFactory(de.catma.serialization.tei.TeiDocumentFactory) TeiTagLibrarySerializer(de.catma.serialization.tei.TeiTagLibrarySerializer) FileInputStream(java.io.FileInputStream) TagsetDefinition(de.catma.tag.TagsetDefinition) Project(de.catma.project.Project) TagManager(de.catma.tag.TagManager) ContentInfoSet(de.catma.document.source.ContentInfoSet) UI(com.vaadin.ui.UI) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IDGenerator(de.catma.util.IDGenerator) TeiDocument(de.catma.serialization.tei.TeiDocument)

Aggregations

IDGenerator (de.catma.util.IDGenerator)25 File (java.io.File)12 TagsetDefinition (de.catma.tag.TagsetDefinition)11 FileInputStream (java.io.FileInputStream)10 Version (de.catma.tag.Version)8 ContentInfoSet (de.catma.document.source.ContentInfoSet)6 SourceDocumentInfo (de.catma.document.source.SourceDocumentInfo)6 SingleTextInputDialog (de.catma.ui.dialog.SingleTextInputDialog)6 ArrayList (java.util.ArrayList)6 EventBus (com.google.common.eventbus.EventBus)5 UI (com.vaadin.ui.UI)5 BackgroundService (de.catma.backgroundservice.BackgroundService)5 IndexInfoSet (de.catma.document.source.IndexInfoSet)5 SourceDocument (de.catma.document.source.SourceDocument)5 TechInfoSet (de.catma.document.source.TechInfoSet)5 Project (de.catma.project.Project)5 TagDefinition (de.catma.tag.TagDefinition)5 JGitRepoManager (de.catma.repository.git.managers.JGitRepoManager)4 TagLibrary (de.catma.tag.TagLibrary)4 TagManager (de.catma.tag.TagManager)4