use of nikita.common.model.noark5.v4.DocumentObject in project nikita-noark5-core by HiOA-ABI.
the class NoarkImportService method checkAndSetDefaultValues.
/**
*
* Check a Noark entity (fonds, series, file, etc) for valid properties and set any values that have not been set
* that should be set with default values.
*
* @param entity
* @throws Exception
*/
public void checkAndSetDefaultValues(INoarkGeneralEntity entity) throws Exception {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
// Not all Noark objects, i.e DocumentDescription and DocumentObject have a documentMedium property
if (entity instanceof IDocumentMedium) {
if (!NoarkUtils.NoarkEntity.Create.checkDocumentMediumValid(((IDocumentMedium) entity))) {
logger.error("Document medium not a valid document medium. Assigned value is " + ((IDocumentMedium) entity).getDocumentMedium());
// An exception will be thrown by checkDocumentMediumValid if not valid
//TODO: Look at the logic here, as a change checkDocumentMediumValid kinda makes
// the if redundant
}
}
// uniqueness via systemId
if (entity.getSystemId() == null) {
entity.setSystemId(UUID.randomUUID().toString());
} else if (entity.getSystemId().length() != Constants.UUIDLength) {
String randomUUID = UUID.randomUUID().toString();
logger.info("Noark object of type [ " + entity.getClass().getName() + "] does not have a valid UUID. Original value [" + entity.getSystemId() + entity.toString() + "] is being changed and assigned with [" + randomUUID + "]");
entity.setSystemId(randomUUID);
}
if (entity.getCreatedDate() == null) {
logger.info("Noark object of type [ " + entity.getClass().getName() + "] does not have a createdDate value. createdDate is being set to [" + new Date().toString() + "]");
entity.setCreatedDate(new Date());
}
if (entity.getCreatedBy() == null) {
logger.info("Noark object of type [ " + entity.getClass().getName() + "] does not have a createdBy value. createdBy is being set to [" + username + "]");
entity.setCreatedBy(username);
}
if (entity.getOwnedBy() == null) {
logger.info("Noark object of type [ " + entity.getClass().getName() + "] does not have a createdBy value. createdBy is being set to [" + username + "]");
entity.setOwnedBy(username);
}
entity.setDeleted(false);
}
use of nikita.common.model.noark5.v4.DocumentObject in project nikita-noark5-core by HiOA-ABI.
the class DocumentDescriptionHateoasController method findAllDocumentDescriptionAssociatedWithRecord.
// Retrieve all DocumentObjects associated with a DocumentDescription identified by systemId
// GET [contextPath][api]/arkivstruktur/dokumentbeskrivelse/{systemId}/dokumentobjekt
@ApiOperation(value = "Retrieves a list of DocumentObjects associated with a DocumentDescription", response = DocumentObjectHateoas.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "DocumentObject returned", response = DocumentObjectHateoas.class), @ApiResponse(code = 401, message = API_MESSAGE_UNAUTHENTICATED_USER), @ApiResponse(code = 403, message = API_MESSAGE_UNAUTHORISED_FOR_USER), @ApiResponse(code = 500, message = API_MESSAGE_INTERNAL_SERVER_ERROR) })
@Counted
@Timed
@RequestMapping(value = SLASH + LEFT_PARENTHESIS + SYSTEM_ID + RIGHT_PARENTHESIS + SLASH + DOCUMENT_OBJECT, method = RequestMethod.GET)
public ResponseEntity<DocumentObjectHateoas> findAllDocumentDescriptionAssociatedWithRecord(final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response, @ApiParam(name = "systemID", value = "systemID of the file to retrieve associated Record", required = true) @PathVariable("systemID") final String systemID) {
DocumentDescription documentDescription = documentDescriptionService.findBySystemIdOrderBySystemId(systemID);
if (documentDescription == null) {
throw new NoarkEntityNotFoundException("Could not find DocumentDescription object with systemID " + systemID);
}
DocumentObjectHateoas documentObjectHateoas = new DocumentObjectHateoas(new ArrayList<>(documentDescription.getReferenceDocumentObject()));
documentObjectHateoasHandler.addLinks(documentObjectHateoas, request, new Authorisation());
return ResponseEntity.status(HttpStatus.OK).allow(CommonUtils.WebUtils.getMethodsForRequestOrThrow(request.getServletPath())).body(documentObjectHateoas);
}
use of nikita.common.model.noark5.v4.DocumentObject in project nikita-noark5-core by HiOA-ABI.
the class DocumentDescriptionHateoasController method createDefaultDocumentObject.
// Create a DocumentObject with default values
// GET [contextPath][api]/arkivstruktur/dokumentbeskrivelse/{systemId}/ny-dokumentobjekt
@ApiOperation(value = "Create a DocumentObject with default values", response = DocumentObjectHateoas.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "DocumentObject returned", response = DocumentObjectHateoas.class), @ApiResponse(code = 401, message = API_MESSAGE_UNAUTHENTICATED_USER), @ApiResponse(code = 403, message = API_MESSAGE_UNAUTHORISED_FOR_USER), @ApiResponse(code = 500, message = API_MESSAGE_INTERNAL_SERVER_ERROR) })
@Counted
@Timed
@RequestMapping(value = SLASH + LEFT_PARENTHESIS + SYSTEM_ID + RIGHT_PARENTHESIS + SLASH + NEW_DOCUMENT_OBJECT, method = RequestMethod.GET)
public ResponseEntity<DocumentObjectHateoas> createDefaultDocumentObject(final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response) {
DocumentObject defaultDocumentObject = new DocumentObject();
// This is just temporary code as this will have to be replaced if this ever goes into production
defaultDocumentObject.setMimeType(MediaType.APPLICATION_XML.toString());
defaultDocumentObject.setVariantFormat(PRODUCTION_VERSION);
defaultDocumentObject.setFormat("XML");
defaultDocumentObject.setVersionNumber(1);
DocumentObjectHateoas documentObjectHateoas = new DocumentObjectHateoas(defaultDocumentObject);
documentObjectHateoasHandler.addLinksOnNew(documentObjectHateoas, request, new Authorisation());
return ResponseEntity.status(HttpStatus.OK).allow(CommonUtils.WebUtils.getMethodsForRequestOrThrow(request.getServletPath())).body(documentObjectHateoas);
}
use of nikita.common.model.noark5.v4.DocumentObject in project nikita-noark5-core by HiOA-ABI.
the class DocumentObjectHateoasController method deleteDocumentObjectBySystemId.
// Delete a DocumentObject identified by systemID
// DELETE [contextPath][api]/arkivstruktur/dokumentobjekt/{systemId}/
@ApiOperation(value = "Deletes a single DocumentObject entity identified by systemID", response = HateoasNoarkObject.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "Parent entity (DocumentDescription or Record) returned", response = HateoasNoarkObject.class), @ApiResponse(code = 401, message = API_MESSAGE_UNAUTHENTICATED_USER), @ApiResponse(code = 403, message = API_MESSAGE_UNAUTHORISED_FOR_USER), @ApiResponse(code = 500, message = API_MESSAGE_INTERNAL_SERVER_ERROR) })
@Counted
@Timed
@RequestMapping(value = SLASH + LEFT_PARENTHESIS + SYSTEM_ID + RIGHT_PARENTHESIS, method = RequestMethod.DELETE)
public ResponseEntity<HateoasNoarkObject> deleteDocumentObjectBySystemId(final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response, @ApiParam(name = "systemID", value = "systemID of the documentObject to delete", required = true) @PathVariable("systemID") final String systemID) {
DocumentObject documentObject = documentObjectService.findBySystemIdOrderBySystemId(systemID);
NoarkEntity parentEntity = documentObject.chooseParent();
documentObjectService.deleteEntity(systemID);
HateoasNoarkObject hateoasNoarkObject;
if (parentEntity instanceof DocumentDescription) {
hateoasNoarkObject = new DocumentDescriptionHateoas(parentEntity);
documentDescriptionHateoasHandler.addLinks(hateoasNoarkObject, request, new Authorisation());
} else if (parentEntity instanceof Record) {
hateoasNoarkObject = new RecordHateoas(parentEntity);
recordHateoasHandler.addLinks(hateoasNoarkObject, request, new Authorisation());
} else {
throw new NikitaException("Internal error. Could process" + request.getRequestURI());
}
applicationEventPublisher.publishEvent(new AfterNoarkEntityDeletedEvent(this, documentObject));
return ResponseEntity.status(HttpStatus.OK).allow(CommonUtils.WebUtils.getMethodsForRequestOrThrow(request.getServletPath())).body(hateoasNoarkObject);
}
use of nikita.common.model.noark5.v4.DocumentObject in project nikita-noark5-core by HiOA-ABI.
the class DocumentObjectHateoasController method handleFileDownload.
// Get a file identified by systemID retrievable with referanseFile
// GET [contextPath][api]/arkivstruktur/dokumentobjekt/{systemID}/referanseFil
@ApiOperation(value = "Downloads a file associated with the documentObject identified by a systemId", response = DocumentObjectHateoas.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "File uploaded successfully", response = DocumentObjectHateoas.class), @ApiResponse(code = 401, message = API_MESSAGE_UNAUTHENTICATED_USER), @ApiResponse(code = 403, message = API_MESSAGE_UNAUTHORISED_FOR_USER), @ApiResponse(code = 500, message = API_MESSAGE_INTERNAL_SERVER_ERROR) })
@Counted
@Timed
@RequestMapping(value = SLASH + LEFT_PARENTHESIS + SYSTEM_ID + RIGHT_PARENTHESIS + SLASH + REFERENCE_FILE, method = RequestMethod.GET)
public void handleFileDownload(final UriComponentsBuilder uriBuilder, HttpServletRequest request, final HttpServletResponse response, @ApiParam(name = "systemID", value = "systemID of the documentObject that has a file associated with it", required = true) @PathVariable("systemID") final String documentObjectSystemId) throws IOException {
DocumentObject documentObject = documentObjectService.findBySystemIdOrderBySystemId(documentObjectSystemId);
if (documentObject == null) {
throw new NoarkEntityNotFoundException(documentObjectSystemId);
}
Resource fileResource = documentObjectService.loadAsResource(documentObject);
String acceptType = request.getHeader(HttpHeaders.ACCEPT);
if (acceptType != null && !acceptType.equalsIgnoreCase(documentObject.getMimeType())) {
if (!acceptType.equals("*/*")) {
throw new NoarkNotAcceptableException("The request [" + request.getRequestURI() + "] is not acceptable. " + "You have issued an Accept: " + acceptType + ", while the mimeType you are trying to retrieve " + "is [" + documentObject.getMimeType() + "].");
}
}
response.setContentType(documentObject.getMimeType());
response.setContentLength(documentObject.getFileSize().intValue());
response.addHeader("Content-disposition", "inline; filename=" + documentObject.getOriginalFilename());
response.addHeader("Content-Type", documentObject.getMimeType());
InputStream filestream = fileResource.getInputStream();
try {
long bytesTotal = IOUtils.copyLarge(filestream, response.getOutputStream());
filestream.close();
} finally {
try {
// Try close without exceptions if copy() threw an
// exception. If close() is called twice, the second
// close() should be ignored.
filestream.close();
} catch (IOException e) {
// swallow any error to expose exceptions from
// IOUtil.copy() if the second close() failed.
}
}
response.flushBuffer();
}
Aggregations