use of org.apache.uima.UIMAException in project webanno by webanno.
the class ImportExportServiceImpl method exportCasToFile.
/**
* A new directory is created using UUID so that every exported file will reside in its own
* directory. This is useful as the written file can have multiple extensions based on the
* Writer class used.
*/
@Override
public File exportCasToFile(CAS cas, SourceDocument aDocument, String aFileName, @SuppressWarnings("rawtypes") Class aWriter, boolean aStripExtension) throws IOException, UIMAException {
// Update the source file name in case it is changed for some reason. This is necessary
// for the writers to create the files under the correct names.
Project project = aDocument.getProject();
File currentDocumentUri = new File(dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/" + project.getId() + "/" + DOCUMENT_FOLDER + "/" + aDocument.getId() + "/" + SOURCE_FOLDER);
DocumentMetaData documentMetadata = DocumentMetaData.get(cas.getJCas());
documentMetadata.setDocumentUri(new File(currentDocumentUri, aFileName).toURI().toURL().toExternalForm());
documentMetadata.setDocumentBaseUri(currentDocumentUri.toURI().toURL().toExternalForm());
documentMetadata.setCollectionId(currentDocumentUri.toURI().toURL().toExternalForm());
documentMetadata.setDocumentUri(new File(dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/" + project.getId() + "/" + DOCUMENT_FOLDER + "/" + aDocument.getId() + "/" + SOURCE_FOLDER + "/" + aFileName).toURI().toURL().toExternalForm());
// update with the correct tagset name
List<AnnotationFeature> features = annotationService.listAnnotationFeature(project);
for (AnnotationFeature feature : features) {
TagSet tagSet = feature.getTagset();
if (tagSet == null) {
continue;
} else if (!feature.getLayer().getType().equals(WebAnnoConst.CHAIN_TYPE)) {
updateCasWithTagSet(cas, feature.getLayer().getName(), tagSet.getName());
}
}
File exportTempDir = File.createTempFile("webanno", "export");
try {
exportTempDir.delete();
exportTempDir.mkdirs();
AnalysisEngineDescription writer;
if (aWriter.getName().equals("de.tudarmstadt.ukp.clarin.webanno.tsv.WebannoTsv3Writer")) {
List<AnnotationLayer> layers = annotationService.listAnnotationLayer(aDocument.getProject());
List<String> slotFeatures = new ArrayList<>();
List<String> slotTargets = new ArrayList<>();
List<String> linkTypes = new ArrayList<>();
Set<String> spanLayers = new HashSet<>();
Set<String> slotLayers = new HashSet<>();
for (AnnotationLayer layer : layers) {
if (layer.getType().contentEquals(WebAnnoConst.SPAN_TYPE)) {
// TSV will not use this
if (!annotationExists(cas, layer.getName())) {
continue;
}
boolean isslotLayer = false;
for (AnnotationFeature f : annotationService.listAnnotationFeature(layer)) {
if (MultiValueMode.ARRAY.equals(f.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(f.getLinkMode())) {
isslotLayer = true;
slotFeatures.add(layer.getName() + ":" + f.getName());
slotTargets.add(f.getType());
linkTypes.add(f.getLinkTypeName());
}
}
if (isslotLayer) {
slotLayers.add(layer.getName());
} else {
spanLayers.add(layer.getName());
}
}
}
spanLayers.addAll(slotLayers);
List<String> chainLayers = new ArrayList<>();
for (AnnotationLayer layer : layers) {
if (layer.getType().contentEquals(WebAnnoConst.CHAIN_TYPE)) {
if (!chainAnnotationExists(cas, layer.getName() + "Chain")) {
continue;
}
chainLayers.add(layer.getName());
}
}
List<String> relationLayers = new ArrayList<>();
for (AnnotationLayer layer : layers) {
if (layer.getType().contentEquals(WebAnnoConst.RELATION_TYPE)) {
// TSV will not use this
if (!annotationExists(cas, layer.getName())) {
continue;
}
relationLayers.add(layer.getName());
}
}
writer = createEngineDescription(aWriter, JCasFileWriter_ImplBase.PARAM_TARGET_LOCATION, exportTempDir, JCasFileWriter_ImplBase.PARAM_STRIP_EXTENSION, aStripExtension, "spanLayers", spanLayers, "slotFeatures", slotFeatures, "slotTargets", slotTargets, "linkTypes", linkTypes, "chainLayers", chainLayers, "relationLayers", relationLayers);
} else {
writer = createEngineDescription(aWriter, JCasFileWriter_ImplBase.PARAM_TARGET_LOCATION, exportTempDir, JCasFileWriter_ImplBase.PARAM_STRIP_EXTENSION, aStripExtension);
}
runPipeline(cas, writer);
// If the writer produced more than one file, we package it up as a ZIP file
File exportFile;
if (exportTempDir.listFiles().length > 1) {
exportFile = new File(exportTempDir.getAbsolutePath() + ".zip");
try {
ZipUtils.zipFolder(exportTempDir, exportFile);
} catch (Exception e) {
try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(project.getId()))) {
log.info("Unable to create zip File");
}
}
} else {
exportFile = new File(exportTempDir.getParent(), exportTempDir.listFiles()[0].getName());
FileUtils.copyFile(exportTempDir.listFiles()[0], exportFile);
}
return exportFile;
} finally {
if (exportTempDir != null) {
FileUtils.forceDelete(exportTempDir);
}
}
}
use of org.apache.uima.UIMAException in project webanno by webanno.
the class CorrectionPage method commonInit.
private void commonInit() {
setVersioned(false);
setModel(Model.of(new AnnotatorStateImpl(Mode.CORRECTION)));
WebMarkupContainer rightSidebar = new WebMarkupContainer("rightSidebar");
// Override sidebar width from preferences
rightSidebar.add(new AttributeModifier("style", LambdaModel.of(() -> String.format("flex-basis: %d%%;", getModelObject().getPreferences().getSidebarSize()))));
rightSidebar.setOutputMarkupId(true);
add(rightSidebar);
List<UserAnnotationSegment> segments = new LinkedList<>();
UserAnnotationSegment userAnnotationSegment = new UserAnnotationSegment();
if (getModelObject().getDocument() != null) {
userAnnotationSegment.setSelectionByUsernameAndAddress(annotationSelectionByUsernameAndAddress);
userAnnotationSegment.setAnnotatorState(getModelObject());
segments.add(userAnnotationSegment);
}
suggestionView = new SuggestionViewPanel("correctionView", new ListModel<>(segments)) {
private static final long serialVersionUID = 2583509126979792202L;
@Override
public void onChange(AjaxRequestTarget aTarget) {
AnnotatorState state = CorrectionPage.this.getModelObject();
aTarget.addChildren(getPage(), IFeedback.class);
try {
// update begin/end of the curation segment based on bratAnnotatorModel changes
// (like sentence change in auto-scroll mode,....
curationContainer.setBratAnnotatorModel(state);
JCas editorCas = getEditorCas();
setCurationSegmentBeginEnd(editorCas);
suggestionView.updatePanel(aTarget, curationContainer, annotationSelectionByUsernameAndAddress, curationSegment);
annotationEditor.requestRender(aTarget);
aTarget.add(getOrCreatePositionInfoLabel());
update(aTarget);
} catch (UIMAException e) {
LOG.error("Error: " + e.getMessage(), e);
error("Error: " + ExceptionUtils.getRootCauseMessage(e));
} catch (Exception e) {
LOG.error("Error: " + e.getMessage(), e);
error("Error: " + e.getMessage());
}
}
};
suggestionView.setOutputMarkupId(true);
add(suggestionView);
rightSidebar.add(detailEditor = createDetailEditor());
annotationEditor = new BratAnnotationEditor("mergeView", getModel(), detailEditor, this::getEditorCas);
add(annotationEditor);
curationContainer = new CurationContainer();
curationContainer.setBratAnnotatorModel(getModelObject());
add(documentNamePanel = createDocumentInfoLabel());
add(getOrCreatePositionInfoLabel());
add(openDocumentsModal = new OpenDocumentDialog("openDocumentsModal", getModel(), getAllowedProjects()) {
private static final long serialVersionUID = 5474030848589262638L;
@Override
public void onDocumentSelected(AjaxRequestTarget aTarget) {
// Reload the page using AJAX. This does not add the project/document ID to the URL,
// but being AJAX it flickers less.
actionLoadDocument(aTarget);
}
});
add(preferencesModal = new AnnotationPreferencesDialog("preferencesDialog", getModel()));
preferencesModal.setOnChangeAction(this::actionCompletePreferencesChange);
add(exportDialog = new ExportDocumentDialog("exportDialog", getModel()));
add(guidelinesDialog = new GuidelinesDialog("guidelinesDialog", getModel()));
Form<Void> gotoPageTextFieldForm = new Form<>("gotoPageTextFieldForm");
gotoPageTextField = new NumberTextField<>("gotoPageText", Model.of(1), Integer.class);
// FIXME minimum and maximum should be obtained from the annotator state
gotoPageTextField.setMinimum(1);
gotoPageTextField.setOutputMarkupId(true);
gotoPageTextFieldForm.add(gotoPageTextField);
LambdaAjaxSubmitLink gotoPageLink = new LambdaAjaxSubmitLink("gotoPageLink", gotoPageTextFieldForm, this::actionGotoPage);
gotoPageTextFieldForm.setDefaultButton(gotoPageLink);
gotoPageTextFieldForm.add(gotoPageLink);
add(gotoPageTextFieldForm);
add(new LambdaAjaxLink("showOpenDocumentModal", this::actionShowOpenDocumentDialog));
add(new LambdaAjaxLink("showPreferencesDialog", this::actionShowPreferencesDialog));
add(new ActionBarLink("showGuidelinesDialog", guidelinesDialog::show));
add(new LambdaAjaxLink("showExportDialog", exportDialog::show) {
private static final long serialVersionUID = -708400631769656072L;
{
setOutputMarkupId(true);
setOutputMarkupPlaceholderTag(true);
}
@Override
protected void onConfigure() {
super.onConfigure();
AnnotatorState state = CorrectionPage.this.getModelObject();
setVisible(state.getProject() != null && (SecurityUtil.isAdmin(state.getProject(), projectService, state.getUser()) || !state.getProject().isDisableExport()));
}
});
add(new LambdaAjaxLink("showPreviousDocument", t -> actionShowPreviousDocument(t)).add(new InputBehavior(new KeyType[] { KeyType.Shift, KeyType.Page_up }, EventType.click)));
add(new LambdaAjaxLink("showNextDocument", t -> actionShowNextDocument(t)).add(new InputBehavior(new KeyType[] { KeyType.Shift, KeyType.Page_down }, EventType.click)));
add(new LambdaAjaxLink("showNext", t -> actionShowNextPage(t)).add(new InputBehavior(new KeyType[] { KeyType.Page_down }, EventType.click)));
add(new LambdaAjaxLink("showPrevious", t -> actionShowPreviousPage(t)).add(new InputBehavior(new KeyType[] { KeyType.Page_up }, EventType.click)));
add(new LambdaAjaxLink("showFirst", t -> actionShowFirstPage(t)).add(new InputBehavior(new KeyType[] { KeyType.Home }, EventType.click)));
add(new LambdaAjaxLink("showLast", t -> actionShowLastPage(t)).add(new InputBehavior(new KeyType[] { KeyType.End }, EventType.click)));
add(new LambdaAjaxLink("toggleScriptDirection", this::actionToggleScriptDirection));
add(createOrGetResetDocumentDialog());
add(createOrGetResetDocumentLink());
add(finishDocumentDialog = new ConfirmationDialog("finishDocumentDialog", new StringResourceModel("FinishDocumentDialog.title", this, null), new StringResourceModel("FinishDocumentDialog.text", this, null)));
add(finishDocumentLink = new LambdaAjaxLink("showFinishDocumentDialog", this::actionFinishDocument) {
private static final long serialVersionUID = 874573384012299998L;
@Override
protected void onConfigure() {
super.onConfigure();
AnnotatorState state = CorrectionPage.this.getModelObject();
setEnabled(state.getDocument() != null && !documentService.isAnnotationFinished(state.getDocument(), state.getUser()));
}
});
finishDocumentIcon = new FinishImage("finishImage", getModel());
finishDocumentIcon.setOutputMarkupId(true);
finishDocumentLink.add(finishDocumentIcon);
}
use of org.apache.uima.UIMAException in project webanno by webanno.
the class ProjectCasDoctorPanel method actionCheck.
private void actionCheck(AjaxRequestTarget aTarget, Form<?> aForm) throws IOException, UIMAException, ClassNotFoundException {
casStorageService.disableCache();
CasDoctor casDoctor = new CasDoctor();
casDoctor.setApplicationContext(ApplicationContextProvider.getApplicationContext());
casDoctor.setFatalChecks(false);
casDoctor.setCheckClasses(CasDoctor.scanChecks());
Project project = getModelObject();
formModel.messageSets = new ArrayList<>();
for (SourceDocument sd : documentService.listSourceDocuments(project)) {
{
LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [INITIAL]");
JCas initialCas;
try {
if (documentService.existsInitialCas(sd)) {
initialCas = documentService.readInitialCas(sd, false);
} else {
messageSet.messages.add(new LogMessage(getClass(), LogLevel.INFO, "No initial CAS for [" + sd.getName() + "]"));
initialCas = documentService.createInitialCas(sd, false);
}
casDoctor.analyze(project, initialCas.getCas(), messageSet.messages);
} catch (Exception e) {
messageSet.messages.add(new LogMessage(getClass(), LogLevel.ERROR, "Error reading initial CAS for [" + sd.getName() + "]: " + e.getMessage()));
LOG.error("Error reading initial CAS for [" + sd.getName() + "]", e);
}
noticeIfThereAreNoMessages(messageSet);
formModel.messageSets.add(messageSet);
}
for (AnnotationDocument ad : documentService.listAnnotationDocuments(sd)) {
if (documentService.existsAnnotationCas(ad)) {
LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [" + ad.getUser() + "]");
JCas userCas = documentService.readAnnotationCas(ad, false);
casDoctor.analyze(project, userCas.getCas(), messageSet.messages);
noticeIfThereAreNoMessages(messageSet);
formModel.messageSets.add(messageSet);
}
}
}
aTarget.add(this);
}
use of org.apache.uima.UIMAException 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++;
}
}
Aggregations