Search in sources :

Example 51 with Project

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

the class ExportUtil method exportCuratedDocuments.

/**
 * Copy, if exists, curation documents to a folder that will be exported as Zip file
 *
 * @param aCopyDir
 *            The folder where curated documents are copied to be exported as Zip File
 */
public static void exportCuratedDocuments(DocumentService documentService, ImportExportService importExportService, ProjectExportRequest aModel, File aCopyDir, boolean aIncludeInProgress) throws UIMAException, IOException, ClassNotFoundException, ProjectExportException {
    Project project = aModel.project.getObject();
    // Get all the source documents from the project
    List<de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument> documents = documentService.listSourceDocuments(project);
    // Determine which format to use for export.
    Class<?> writer;
    if (FORMAT_AUTO.equals(aModel.format)) {
        writer = WebannoTsv3XWriter.class;
    } else {
        writer = importExportService.getWritableFormats().get(importExportService.getWritableFormatId(aModel.format));
        if (writer == null) {
            writer = WebannoTsv3XWriter.class;
        }
    }
    int initProgress = aModel.progress - 1;
    int i = 1;
    for (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument sourceDocument : documents) {
        File curationCasDir = new File(aCopyDir + CURATION_AS_SERIALISED_CAS + sourceDocument.getName());
        FileUtils.forceMkdir(curationCasDir);
        File curationDir = new File(aCopyDir + CURATION_FOLDER + sourceDocument.getName());
        FileUtils.forceMkdir(curationDir);
        // finished or also the ones that are in progress
        if ((aIncludeInProgress && SourceDocumentState.CURATION_IN_PROGRESS.equals(sourceDocument.getState())) || SourceDocumentState.CURATION_FINISHED.equals(sourceDocument.getState())) {
            File curationCasFile = documentService.getCasFile(sourceDocument, WebAnnoConst.CURATION_USER);
            if (curationCasFile.exists()) {
                // Copy CAS - this is used when importing the project again
                FileUtils.copyFileToDirectory(curationCasFile, curationCasDir);
                // Copy secondary export format for convenience - not used during import
                try {
                    File curationFile = importExportService.exportAnnotationDocument(sourceDocument, WebAnnoConst.CURATION_USER, writer, WebAnnoConst.CURATION_USER, Mode.CURATION);
                    FileUtils.copyFileToDirectory(curationFile, curationDir);
                    FileUtils.forceDelete(curationFile);
                } catch (Exception e) {
                    // ExceptionUtils.getRootCauseMessage(e) );
                    throw new ProjectExportException("Aborting due to unrecoverable error while exporting!");
                }
            }
        }
        aModel.progress = initProgress + (int) Math.ceil(((double) i) / documents.size() * 10.0);
        i++;
    }
}
Also used : Mode(de.tudarmstadt.ukp.clarin.webanno.model.Mode) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.export.model.SourceDocument) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) File(java.io.File)

Example 52 with Project

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

the class ImportServiceImpl method importProject.

@Override
public Project importProject(File aProjectFile, boolean aGenerateUsers) throws Exception {
    Project importedProject = new Project();
    ZipFile zip = new ZipFile(aProjectFile);
    InputStream projectInputStream = null;
    for (Enumeration<? extends ZipEntry> zipEnumerate = zip.entries(); zipEnumerate.hasMoreElements(); ) {
        ZipEntry entry = (ZipEntry) zipEnumerate.nextElement();
        if (entry.toString().replace("/", "").startsWith(ImportUtil.EXPORTED_PROJECT) && entry.toString().replace("/", "").endsWith(".json")) {
            projectInputStream = zip.getInputStream(entry);
            break;
        }
    }
    // Load the project model from the JSON file
    String text = IOUtils.toString(projectInputStream, "UTF-8");
    de.tudarmstadt.ukp.clarin.webanno.export.model.Project importedProjectSetting = JSONUtil.getJsonConverter().getObjectMapper().readValue(text, de.tudarmstadt.ukp.clarin.webanno.export.model.Project.class);
    // Import the project itself
    importedProject = ImportUtil.createProject(importedProjectSetting, projectService);
    // Import additional project things
    projectService.onProjectImport(zip, importedProjectSetting, importedProject);
    // Import missing users
    if (aGenerateUsers) {
        ImportUtil.createMissingUsers(importedProjectSetting, userRepository);
    }
    applicationEventPublisher.publishEvent(new ProjectImportEvent(this, zip, importedProjectSetting, importedProject));
    // Import layers
    Map<String, AnnotationFeature> featuresMap = ImportUtil.createLayer(importedProject, importedProjectSetting, userRepository, annotationService);
    /*
         * for (TagSet tagset : importedProjectSetting.getTagSets()) {
         * ImportUtil.createTagset(importedProject, tagset, projectRepository, annotationService); }
         */
    // Import source document
    ImportUtil.createSourceDocument(importedProjectSetting, importedProject, documentService);
    // Import Training document
    if (automationService != null) {
        ImportUtil.createTrainingDocument(importedProjectSetting, importedProject, automationService, featuresMap);
    }
    // Import source document content
    ImportUtil.createSourceDocumentContent(zip, importedProject, documentService);
    // Import training document content
    if (automationService != null) {
        ImportUtil.createTrainingDocumentContent(zip, importedProject, automationService);
    }
    // Import automation settings
    if (automationService != null) {
        ImportUtil.createMiraTemplate(importedProjectSetting, automationService, featuresMap);
    }
    // Import annotation document content
    ImportUtil.createAnnotationDocument(importedProjectSetting, importedProject, documentService);
    // Import annotation document content
    ImportUtil.createAnnotationDocumentContent(zip, importedProject, documentService);
    // Import curation document content
    ImportUtil.createCurationDocumentContent(zip, importedProject, documentService);
    return importedProject;
}
Also used : InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ZipFile(java.util.zip.ZipFile) ProjectImportEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.ProjectImportEvent) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 53 with Project

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

the class ProjectServiceImpl method listManageableProjects.

@Override
public List<Project> listManageableProjects(User user) {
    List<Project> allowedProject = new ArrayList<>();
    List<Project> allProjects = listProjects();
    // if global admin, show all projects
    if (SecurityUtil.isSuperAdmin(this, user)) {
        return allProjects;
    }
    // else only projects she is admin of
    for (Project project : allProjects) {
        if (SecurityUtil.isProjectAdmin(project, this, user)) {
            allowedProject.add(project);
        }
    }
    return allowedProject;
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) ArrayList(java.util.ArrayList)

Example 54 with Project

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

the class RemoteApiController method sourceDocumentDelete.

/**
 * Delete the source document in project if user has an ADMIN permission
 *
 * To test when running in Eclipse, use the Linux "curl" command.
 *
 * curl -v -X DELETE
 * 'http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/
 * {aSourceDocumentId}'
 *
 * @param aProjectId
 *            {@link Project} ID.
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}", method = RequestMethod.DELETE)
public ResponseEntity<String> sourceDocumentDelete(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        return ResponseEntity.badRequest().body("User [" + username + "] not found.");
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
    }
    LOG.info("Deleting document [" + project.getName() + "]");
    // Get source document
    SourceDocument srcDocument;
    try {
        srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
    }
    documentRepository.removeSourceDocument(srcDocument);
    LOG.info("Successfully deleted project : [" + aProjectId + "]");
    return ResponseEntity.ok("Source document [" + aSourceDocumentId + "] in project [" + aProjectId + "] deleted.");
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) NoResultException(javax.persistence.NoResultException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 55 with Project

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

the class RemoteApiController method annotationDocumentList.

/**
 * List annotation documents for a source document in a projects where user is ADMIN
 *
 * Test when running in Eclipse: Open your browser, paste following URL with appropriate values:
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/{
 * aSourceDocumentId}/annos
 *
 * @param aProjectId
 *            {@link Project} ID
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID
 * @return JSON string of all the annotation documents with their projects.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS, method = RequestMethod.GET)
public ResponseEntity<String> annotationDocumentList(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        return ResponseEntity.badRequest().body("User [" + username + "] not found.");
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
    }
    // Get source document
    SourceDocument srcDocument;
    try {
        srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
    }
    List<AnnotationDocument> annList = documentRepository.listAllAnnotationDocuments(srcDocument);
    JSONArray annDocArr = new JSONArray();
    for (AnnotationDocument annDoc : annList) {
        if (annDoc.getState().equals(AnnotationDocumentState.FINISHED) || annDoc.getState().equals(AnnotationDocumentState.IN_PROGRESS)) {
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ssZ");
            JSONObject annDocObj = new JSONObject();
            annDocObj.put("user", annDoc.getUser());
            annDocObj.put("state", annDoc.getState().getId());
            if (annDoc.getTimestamp() != null) {
                annDocObj.put("timestamp", sdf.format(annDoc.getTimestamp()));
            }
            annDocArr.put(annDocObj);
        }
    }
    JSONObject returnJSON = new JSONObject();
    returnJSON.put(srcDocument.getName(), annDocArr);
    return ResponseEntity.ok(returnJSON.toString());
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) JSONObject(org.apache.wicket.ajax.json.JSONObject) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JSONArray(org.apache.wicket.ajax.json.JSONArray) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) NoResultException(javax.persistence.NoResultException) SimpleDateFormat(java.text.SimpleDateFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)68 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)22 File (java.io.File)22 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)17 IOException (java.io.IOException)15 ApiOperation (io.swagger.annotations.ApiOperation)14 NoResultException (javax.persistence.NoResultException)13 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)12 ArrayList (java.util.ArrayList)12 MDC (org.slf4j.MDC)12 InputStream (java.io.InputStream)11 JCas (org.apache.uima.jcas.JCas)10 UIMAException (org.apache.uima.UIMAException)8 MultipartFile (org.springframework.web.multipart.MultipartFile)8 BufferedInputStream (java.io.BufferedInputStream)7 FileInputStream (java.io.FileInputStream)7 Transactional (org.springframework.transaction.annotation.Transactional)7 ZipFile (java.util.zip.ZipFile)6