use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class ImportExportServiceImpl method exportAnnotationDocument.
@SuppressWarnings("rawtypes")
@Override
@Transactional
public File exportAnnotationDocument(SourceDocument aDocument, String aUser, Class aWriter, String aFileName, Mode aMode, boolean aStripExtension) throws UIMAException, IOException, ClassNotFoundException {
File annotationFolder = casStorageService.getAnnotationFolder(aDocument);
String serializedCasFileName;
// (Annotated) or the automated document
if (aMode.equals(Mode.ANNOTATION) || aMode.equals(Mode.AUTOMATION) || aMode.equals(Mode.CORRECTION)) {
serializedCasFileName = aUser + ".ser";
} else // The merge result will be exported
{
serializedCasFileName = WebAnnoConst.CURATION_USER + ".ser";
}
// Read file
File serializedCasFile = new File(annotationFolder, serializedCasFileName);
if (!serializedCasFile.exists()) {
throw new FileNotFoundException("CAS file [" + serializedCasFileName + "] not found in [" + annotationFolder + "]");
}
CAS cas = CasCreationUtils.createCas((TypeSystemDescription) null, null, null);
CasPersistenceUtils.readSerializedCas(cas, serializedCasFile);
// Update type system the CAS
annotationService.upgradeCas(cas, aDocument, aUser);
File exportFile = exportCasToFile(cas, aDocument, aFileName, aWriter, aStripExtension);
Project project = aDocument.getProject();
try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(project.getId()))) {
log.info("Exported annotation document content [{}]({}) for user [{}] from project [{}]({})", aDocument.getName(), aDocument.getId(), aUser, project.getName(), project.getId());
}
return exportFile;
}
use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class ExportUtil method exportProjectSettings.
public static de.tudarmstadt.ukp.clarin.webanno.export.model.Project exportProjectSettings(AnnotationSchemaService annotationService, Optional<AutomationService> automationService, DocumentService documentService, ProjectService projectService, Project aProject, File aProjectSettings, File aExportTempDir) {
de.tudarmstadt.ukp.clarin.webanno.export.model.Project exProjekt = new de.tudarmstadt.ukp.clarin.webanno.export.model.Project();
exProjekt.setDescription(aProject.getDescription());
exProjekt.setName(aProject.getName());
// In older versions of WebAnno, the mode was an enum which was serialized as upper-case
// during export but as lower-case in the database. This is compensating for this case.
exProjekt.setMode(StringUtils.upperCase(aProject.getMode(), Locale.US));
exProjekt.setScriptDirection(aProject.getScriptDirection());
exProjekt.setVersion(aProject.getVersion());
exProjekt.setDisableExport(aProject.isDisableExport());
exProjekt.setCreated(aProject.getCreated());
exProjekt.setUpdated(aProject.getUpdated());
List<de.tudarmstadt.ukp.clarin.webanno.export.model.AnnotationLayer> exLayers = new ArrayList<>();
// Store map of layer and its equivalent exLayer so that the attach type is attached later
Map<AnnotationLayer, de.tudarmstadt.ukp.clarin.webanno.export.model.AnnotationLayer> layerToExLayers = new HashMap<>();
// Store map of feature and its equivalent exFeature so that the attach feature is attached
// later
Map<AnnotationFeature, de.tudarmstadt.ukp.clarin.webanno.export.model.AnnotationFeature> featureToExFeatures = new HashMap<>();
for (AnnotationLayer layer : annotationService.listAnnotationLayer(aProject)) {
exLayers.add(ImportUtil.exportLayerDetails(layerToExLayers, featureToExFeatures, layer, annotationService));
}
// exported feature
for (AnnotationLayer layer : layerToExLayers.keySet()) {
if (layer.getAttachType() != null) {
layerToExLayers.get(layer).setAttachType(layerToExLayers.get(layer.getAttachType()));
}
if (layer.getAttachFeature() != null) {
layerToExLayers.get(layer).setAttachFeature(featureToExFeatures.get(layer.getAttachFeature()));
}
}
exProjekt.setLayers(exLayers);
List<ExportedTagSet> extTagSets = new ArrayList<>();
for (TagSet tagSet : annotationService.listTagSets(aProject)) {
ExportedTagSet exTagSet = new ExportedTagSet();
exTagSet.setCreateTag(tagSet.isCreateTag());
exTagSet.setDescription(tagSet.getDescription());
exTagSet.setLanguage(tagSet.getLanguage());
exTagSet.setName(tagSet.getName());
List<ExportedTag> exTags = new ArrayList<>();
for (Tag tag : annotationService.listTags(tagSet)) {
ExportedTag exTag = new ExportedTag();
exTag.setDescription(tag.getDescription());
exTag.setName(tag.getName());
exTags.add(exTag);
}
exTagSet.setTags(exTags);
extTagSets.add(exTagSet);
}
exProjekt.setTagSets(extTagSets);
List<SourceDocument> sourceDocuments = new ArrayList<>();
List<AnnotationDocument> annotationDocuments = new ArrayList<>();
// add source documents to a project
List<de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument> documents = documentService.listSourceDocuments(aProject);
for (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument sourceDocument : documents) {
SourceDocument exDocument = new SourceDocument();
exDocument.setFormat(sourceDocument.getFormat());
exDocument.setName(sourceDocument.getName());
exDocument.setState(sourceDocument.getState());
exDocument.setTimestamp(sourceDocument.getTimestamp());
exDocument.setSentenceAccessed(sourceDocument.getSentenceAccessed());
exDocument.setCreated(sourceDocument.getCreated());
exDocument.setUpdated(sourceDocument.getUpdated());
// add annotation document to Project
for (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument annotationDocument : documentService.listAnnotationDocuments(sourceDocument)) {
AnnotationDocument annotationDocumentToExport = new AnnotationDocument();
annotationDocumentToExport.setName(annotationDocument.getName());
annotationDocumentToExport.setState(annotationDocument.getState());
annotationDocumentToExport.setUser(annotationDocument.getUser());
annotationDocumentToExport.setTimestamp(annotationDocument.getTimestamp());
annotationDocumentToExport.setSentenceAccessed(annotationDocument.getSentenceAccessed());
annotationDocumentToExport.setCreated(annotationDocument.getCreated());
annotationDocumentToExport.setUpdated(annotationDocument.getUpdated());
annotationDocuments.add(annotationDocumentToExport);
}
sourceDocuments.add(exDocument);
}
exProjekt.setSourceDocuments(sourceDocuments);
exProjekt.setAnnotationDocuments(annotationDocuments);
if (automationService.isPresent()) {
List<de.tudarmstadt.ukp.clarin.webanno.export.model.TrainingDocument> trainDocuments = new ArrayList<>();
List<TrainingDocument> trainingDocuments = automationService.get().listTrainingDocuments(aProject);
Map<String, de.tudarmstadt.ukp.clarin.webanno.export.model.AnnotationFeature> fm = new HashMap<>();
for (de.tudarmstadt.ukp.clarin.webanno.export.model.AnnotationFeature f : featureToExFeatures.values()) {
fm.put(f.getName(), f);
}
for (TrainingDocument trainingDocument : trainingDocuments) {
de.tudarmstadt.ukp.clarin.webanno.export.model.TrainingDocument exDocument = new de.tudarmstadt.ukp.clarin.webanno.export.model.TrainingDocument();
exDocument.setFormat(trainingDocument.getFormat());
exDocument.setName(trainingDocument.getName());
exDocument.setState(trainingDocument.getState());
exDocument.setTimestamp(trainingDocument.getTimestamp());
exDocument.setSentenceAccessed(trainingDocument.getSentenceAccessed());
if (trainingDocument.getFeature() != null) {
exDocument.setFeature(fm.get(trainingDocument.getFeature().getName()));
}
trainDocuments.add(exDocument);
}
exProjekt.setTrainingDocuments(trainDocuments);
} else {
exProjekt.setTrainingDocuments(new ArrayList<>());
}
List<ProjectPermission> projectPermissions = new ArrayList<>();
// add project permissions to the project
for (User user : projectService.listProjectUsersWithPermissions(aProject)) {
for (de.tudarmstadt.ukp.clarin.webanno.model.ProjectPermission permission : projectService.listProjectPermissionLevel(user, aProject)) {
ProjectPermission permissionToExport = new ProjectPermission();
permissionToExport.setLevel(permission.getLevel());
permissionToExport.setUser(user.getUsername());
projectPermissions.add(permissionToExport);
}
}
exProjekt.setProjectPermissions(projectPermissions);
// export automation Mira template
if (automationService.isPresent()) {
List<de.tudarmstadt.ukp.clarin.webanno.export.model.MiraTemplate> exTemplates = new ArrayList<>();
for (MiraTemplate template : automationService.get().listMiraTemplates(aProject)) {
de.tudarmstadt.ukp.clarin.webanno.export.model.MiraTemplate exTemplate = new de.tudarmstadt.ukp.clarin.webanno.export.model.MiraTemplate();
exTemplate.setAnnotateAndPredict(template.isAnnotateAndRepeat());
exTemplate.setAutomationStarted(template.isAutomationStarted());
exTemplate.setCurrentLayer(template.isCurrentLayer());
exTemplate.setResult(template.getResult());
exTemplate.setTrainFeature(featureToExFeatures.get(template.getTrainFeature()));
if (template.getOtherFeatures().size() > 0) {
Set<de.tudarmstadt.ukp.clarin.webanno.export.model.AnnotationFeature> exOtherFeatures = new HashSet<>();
for (AnnotationFeature feature : template.getOtherFeatures()) {
exOtherFeatures.add(featureToExFeatures.get(feature));
}
exTemplate.setOtherFeatures(exOtherFeatures);
}
exTemplates.add(exTemplate);
}
exProjekt.setMiraTemplates(exTemplates);
} else {
exProjekt.setMiraTemplates(new ArrayList<>());
}
return exProjekt;
}
use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class ImportUtil method createProject.
/**
* create new {@link Project} from the
* {@link de.tudarmstadt.ukp.clarin.webanno.export.model.Project} model
* @param aProject the project
* @param aRepository the repository service.
* @return the project.
* @throws IOException if an I/O error occurs.
*/
public static Project createProject(de.tudarmstadt.ukp.clarin.webanno.export.model.Project aProject, ProjectService aRepository) throws IOException {
Project project = new Project();
String projectName = aProject.getName();
if (aRepository.existsProject(projectName)) {
projectName = copyProjectName(aRepository, projectName);
}
project.setName(projectName);
project.setDescription(aProject.getDescription());
// In older versions of WebAnno, the mode was an enum which was serialized as upper-case
// during export but as lower-case in the database. This is compensating for this case.
project.setMode(StringUtils.lowerCase(aProject.getMode(), Locale.US));
project.setDisableExport(aProject.isDisableExport());
project.setCreated(aProject.getCreated());
project.setUpdated(aProject.getUpdated());
// Set default to LTR on import from old WebAnno versions
if (aProject.getScriptDirection() == null) {
project.setScriptDirection(ScriptDirection.LTR);
} else {
project.setScriptDirection(aProject.getScriptDirection());
}
aRepository.createProject(project);
return project;
}
use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class AnnotationSchemaServiceImpl method upgradeCas.
@Override
public void upgradeCas(CAS aCas, SourceDocument aSourceDocument, String aUser) throws UIMAException, IOException {
TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory.createTypeSystemDescription();
TypeSystemDescription projectTypes = getProjectTypes(aSourceDocument.getProject());
TypeSystemDescription allTypes = CasCreationUtils.mergeTypeSystems(asList(projectTypes, builtInTypes));
// Prepare template for new CAS
CAS newCas = JCasFactory.createJCas(allTypes).getCas();
CASCompleteSerializer serializer = Serialization.serializeCASComplete((CASImpl) newCas);
// Save old type system
TypeSystem oldTypeSystem = aCas.getTypeSystem();
// Save old CAS contents
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
Serialization.serializeWithCompression(aCas, os2, oldTypeSystem);
// Prepare CAS with new type system
Serialization.deserializeCASComplete(serializer, (CASImpl) aCas);
// Restore CAS data to new type system
Serialization.deserializeCAS(aCas, new ByteArrayInputStream(os2.toByteArray()), oldTypeSystem, null);
// Make sure JCas is properly initialized too
aCas.getJCas();
try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aSourceDocument.getProject().getId()))) {
Project project = aSourceDocument.getProject();
log.info("Upgraded CAS of user [{}] for " + "document [{}]({}) in project [{}]({})", aUser, aSourceDocument.getName(), aSourceDocument.getId(), project.getName(), project.getId());
}
}
use of de.tudarmstadt.ukp.clarin.webanno.model.Project in project webanno by webanno.
the class AnnotationSchemaServiceImpl method createTagSet.
@Override
@Transactional
public void createTagSet(TagSet aTagSet) {
if (isNull(aTagSet.getId())) {
entityManager.persist(aTagSet);
} else {
entityManager.merge(aTagSet);
}
try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aTagSet.getProject().getId()))) {
Project project = aTagSet.getProject();
log.info("Created tagset [{}]({}) in project [{}]({})", aTagSet.getName(), aTagSet.getId(), project.getName(), project.getId());
}
}
Aggregations