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() + "]");
}
}
}
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);
}
}
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();
}
}
}
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);
}
}
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);
}
Aggregations