Search in sources :

Example 6 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class ImportUtil method createAnnotationDocumentContent.

/**
 * copy annotation documents (serialized CASs) from the exported project
 * @param zip the ZIP file.
 * @param aProject the project.
 * @param aRepository the repository service.
 * @throws IOException if an I/O error occurs.
 */
@SuppressWarnings("rawtypes")
public static void createAnnotationDocumentContent(ZipFile zip, Project aProject, DocumentService aRepository) throws IOException {
    for (Enumeration zipEnumerate = zip.entries(); zipEnumerate.hasMoreElements(); ) {
        ZipEntry entry = (ZipEntry) zipEnumerate.nextElement();
        // Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985)
        String entryName = normalizeEntryName(entry);
        if (entryName.startsWith(ANNOTATION_AS_SERIALISED_CAS + "/")) {
            String fileName = entryName.replace(ANNOTATION_AS_SERIALISED_CAS + "/", "");
            if (fileName.trim().isEmpty()) {
                continue;
            }
            // the user annotated the document is file name minus extension (anno1.ser)
            String username = FilenameUtils.getBaseName(fileName).replace(".ser", "");
            // name of the annotation document
            fileName = fileName.replace(FilenameUtils.getName(fileName), "").replace("/", "");
            de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument sourceDocument = aRepository.getSourceDocument(aProject, fileName);
            File annotationFilePath = aRepository.getCasFile(sourceDocument, username);
            FileUtils.copyInputStreamToFile(zip.getInputStream(entry), annotationFilePath);
            LOG.info("Imported annotation document content for user [" + username + "] for source document [" + sourceDocument.getId() + "] in project [" + aProject.getName() + "] with id [" + aProject.getId() + "]");
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ZipEntry(java.util.zip.ZipEntry) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 7 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class ImportUtil method createSourceDocument.

/**
 * Create s {@link SourceDocument} from the exported {@link SourceDocument}
 *
 * @param aImportedProjectSetting  the exported project.
 * @param aImportedProject the project.
 * @param aRepository the repository service.
 * @throws IOException if an I/O error occurs.
 */
public static void createSourceDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.Project aImportedProjectSetting, Project aImportedProject, DocumentService aRepository) throws IOException {
    for (de.tudarmstadt.ukp.clarin.webanno.export.model.SourceDocument importedSourceDocument : aImportedProjectSetting.getSourceDocuments()) {
        SourceDocument sourceDocument = new SourceDocument();
        sourceDocument.setFormat(importedSourceDocument.getFormat());
        sourceDocument.setName(importedSourceDocument.getName());
        sourceDocument.setState(importedSourceDocument.getState());
        sourceDocument.setProject(aImportedProject);
        sourceDocument.setTimestamp(importedSourceDocument.getTimestamp());
        sourceDocument.setSentenceAccessed(importedSourceDocument.getSentenceAccessed());
        sourceDocument.setCreated(importedSourceDocument.getCreated());
        sourceDocument.setUpdated(importedSourceDocument.getUpdated());
        aRepository.createSourceDocument(sourceDocument);
    }
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)

Example 8 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class RemoteApiController method curationDocumentRead.

/**
 * Download curated document with requested parameters
 *
 * Test when running in Eclipse: Open your browser, paste following URL with appropriate values:
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/curationdoc/
 * {aSourceDocumentId}?format=xmi
 *
 * @param response
 *            HttpServletResponse.
 * @param aProjectId
 *            {@link Project} ID.
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID.
 * @param format
 *            Export format.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + CURATION + "/{" + PARAM_DOCUMENT_ID + "}", method = RequestMethod.GET)
public void curationDocumentRead(HttpServletResponse response, @PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId, @RequestParam(value = PARAM_FORMAT, required = false) String format) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        response.sendError(HttpStatus.BAD_REQUEST.value(), "User [" + username + "] not found.");
        return;
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Project" + aProjectId + "] not found.");
        return;
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        response.sendError(HttpStatus.FORBIDDEN.value(), "User [" + username + "] is not allowed to access project [" + aProjectId + "]");
        return;
    }
    // Get source document
    SourceDocument srcDocument;
    try {
        srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
        return;
    }
    // Check if curation is complete
    if (!SourceDocumentState.CURATION_FINISHED.equals(srcDocument.getState())) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Curation of source document [" + aSourceDocumentId + "] not yet complete.");
        return;
    }
    String formatId;
    if (format == null) {
        formatId = srcDocument.getFormat();
    } else {
        formatId = format;
    }
    Class<?> writer = importExportService.getWritableFormats().get(formatId);
    if (writer == null) {
        LOG.info("[" + srcDocument.getName() + "] No writer found for format [" + formatId + "] - exporting as WebAnno TSV instead.");
        writer = WebannoTsv3XWriter.class;
    }
    // Temporary file of annotation document
    File downloadableFile = importExportService.exportAnnotationDocument(srcDocument, WebAnnoConst.CURATION_USER, writer, srcDocument.getName(), Mode.CURATION);
    try {
        // Set mime type
        String mimeType = URLConnection.guessContentTypeFromName(downloadableFile.getName());
        if (mimeType == null) {
            LOG.info("mimetype is not detectable, will take default");
            mimeType = "application/octet-stream";
        }
        // Set response
        response.setContentType(mimeType);
        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition", "inline; filename=\"" + downloadableFile.getName() + "\"");
        response.setContentLength((int) downloadableFile.length());
        InputStream inputStream = new BufferedInputStream(new FileInputStream(downloadableFile));
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    } catch (Exception e) {
        LOG.info("Exception occured" + e.getMessage());
    } finally {
        if (downloadableFile.exists()) {
            downloadableFile.delete();
        }
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) NoResultException(javax.persistence.NoResultException) ZipFile(java.util.zip.ZipFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream) NoResultException(javax.persistence.NoResultException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class RemoteApiController method uploadSourceDocumentFile.

private void uploadSourceDocumentFile(InputStream is, String name, Project project, String aFileType) throws IOException, UIMAException {
    // Check if it is a property file
    if (name.equals("source-meta-data.properties")) {
        projectRepository.savePropertiesFile(project, is, name);
    } else {
        SourceDocument document = new SourceDocument();
        document.setName(name);
        document.setProject(project);
        document.setFormat(aFileType);
        // Meta data entry to the database
        // Import source document to the project repository folder
        documentRepository.uploadSourceDocument(is, document);
    }
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)

Example 10 with SourceDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.

the class RemoteApiController method uploadSourceDocument.

private void uploadSourceDocument(ZipFile zip, ZipEntry entry, Project project, String aFileType) throws IOException, UIMAException {
    String fileName = FilenameUtils.getName(entry.toString());
    InputStream zipStream = zip.getInputStream(entry);
    SourceDocument document = new SourceDocument();
    document.setName(fileName);
    document.setProject(project);
    document.setFormat(aFileType);
    // Meta data entry to the database
    // Import source document to the project repository folder
    documentRepository.uploadSourceDocument(zipStream, document);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)

Aggregations

SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)59 JCas (org.apache.uima.jcas.JCas)24 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)22 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)22 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 File (java.io.File)13 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 ApiOperation (io.swagger.annotations.ApiOperation)9 IOException (java.io.IOException)9 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)8 Map (java.util.Map)8 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)7 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)7 DiffResult (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult)6 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)6 NoResultException (javax.persistence.NoResultException)6