private List<DescriptiveMetadata> getDescriptiveMetadata(AIP aip, String representationId) { List<DescriptiveMetadata> descriptiveMetadataList = Collections.emptyList(); if (representationId == null) { // AIP descriptive metadata descriptiveMetadataList = aip.getDescriptiveMetadata(); } else { // Representation descriptive metadata Optional<Representation> oRep = aip.getRepresentations().stream() .filter(rep -> rep.getId().equals(representationId)).findFirst(); if (oRep.isPresent()) { descriptiveMetadataList = oRep.get().getDescriptiveMetadata(); } } return descriptiveMetadataList; }
public static List<List<String>> getDescriptiveMetadataInformation(List<String> fields, AIP aip, ModelService model, StorageService storage) { List<List<String>> descriptiveMetadataInformation = new ArrayList<>(); for (DescriptiveMetadata dm : aip.getDescriptiveMetadata()) { descriptiveMetadataInformation.add(retrieveDescriptiveMetadataInfo(fields, aip, dm, storage)); } if (aip.getRepresentations() != null) { for (Representation r : aip.getRepresentations()) { for (DescriptiveMetadata dm : r.getDescriptiveMetadata()) { descriptiveMetadataInformation.add(retrieveDescriptiveMetadataInfo(fields, aip, dm, storage)); } } } return descriptiveMetadataInformation; }
private ReturnWithExceptions<Void, ModelObserver> representationsStateUpdated(final AIP aip) { ReturnWithExceptions<Void, ModelObserver> ret = new ReturnWithExceptions<>(this); for (Representation representation : aip.getRepresentations()) { representationStateUpdated(aip, representation).addTo(ret); } return ret; }
private ReturnWithExceptions<Void, ModelObserver> representationsPermissionsUpdated(final AIP aip) { ReturnWithExceptions<Void, ModelObserver> ret = new ReturnWithExceptions<>(this); for (Representation representation : aip.getRepresentations()) { representationPermissionsUpdated(aip, representation).addTo(ret); } return ret; }
public static Representation convertResourceToRepresentation(Resource resource) throws GenericException, NotFoundException, AuthorizationDeniedException, RequestNotValidException { if (resource == null) { throw new RequestNotValidException(RESOURCE_CANNOT_BE_NULL); } StoragePath resourcePath = resource.getStoragePath(); String id = resourcePath.getName(); String aipId = ModelUtils.extractAipId(resourcePath).orElse(null); AIP aip = RodaCoreFactory.getModelService().retrieveAIP(aipId); Optional<Representation> rep = aip.getRepresentations().stream().filter(r -> r.getId().equals(id)).findFirst(); if (rep.isPresent()) { return rep.get(); } else { throw new NotFoundException("Unable to find representation with storage path " + resourcePath); } }
private ReturnWithExceptions<Void, ModelObserver> indexRepresentations(final AIP aip, final List<String> ancestors) { ReturnWithExceptions<Void, ModelObserver> ret = new ReturnWithExceptions<>(this); for (Representation representation : aip.getRepresentations()) { indexRepresentation(aip, representation, ancestors).addTo(ret); } return ret; }
public void changeRepresentationStates(String aipId, String representationId, List<String> newStates, String updatedBy) throws RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); AIP aip = retrieveAIP(aipId); Iterator<Representation> it = aip.getRepresentations().iterator(); Optional<Representation> representation = Optional.empty(); while (it.hasNext()) { Representation next = it.next(); if (next.getId().equals(representationId)) { representation = Optional.of(next); break; } } if (representation.isPresent()) { representation.get().setRepresentationStates(newStates); representation.get().setUpdatedOn(new Date()); representation.get().setUpdatedBy(updatedBy); updateAIPMetadata(aip); notifyRepresentationUpdated(representation.get()).failOnError(); } }
private CloseableIterable<OptionalWithCause<Representation>> listRepresentations() throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { CloseableIterable<OptionalWithCause<AIP>> aips = listAIPs(); return CloseableIterables.concat(aips, aip -> { if (aip.isPresent()) { List<Representation> representations = aip.get().getRepresentations(); return CloseableIterables .fromList(representations.stream().map(rep -> OptionalWithCause.of(rep)).collect(Collectors.toList())); } else { return CloseableIterables.empty(); } }); }
/**********************************************************/ public Representation retrieveRepresentation(String aipId, String representationId) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { AIP aip = ResourceParseUtils.getAIPMetadata(getStorage(), aipId); Representation ret = null; for (Representation representation : aip.getRepresentations()) { if (representation.getId().equals(representationId)) { ret = representation; break; } } if (ret == null) { throw new NotFoundException("Could not find representation: " + representationId); } return ret; }
public void deleteRepresentation(String aipId, String representationId) throws RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); StoragePath representationPath = ModelUtils.getRepresentationStoragePath(aipId, representationId); storage.deleteResource(representationPath); // update AIP metadata AIP aip = ResourceParseUtils.getAIPMetadata(getStorage(), aipId); for (Iterator<Representation> it = aip.getRepresentations().iterator(); it.hasNext();) { Representation representation = it.next(); if (representation.getId().equals(representationId)) { it.remove(); break; } } updateAIPMetadata(aip); notifyRepresentationDeleted(aipId, representationId).failOnError(); }
public void changeRepresentationType(String aipId, String representationId, String type, String updatedBy) throws RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); AIP aip = retrieveAIP(aipId); Iterator<Representation> it = aip.getRepresentations().iterator(); while (it.hasNext()) { Representation representation = it.next(); if (representation.getId().equals(representationId)) { representation.setType(type); representation.setUpdatedOn(new Date()); representation.setUpdatedBy(updatedBy); notifyRepresentationUpdated(representation).failOnError(); break; } } updateAIPMetadata(aip); }
public static List<List<String>> getDataInformation(List<String> fields, AIP aip, ModelService model, StorageService storage) { List<List<String>> dataInformation = new ArrayList<>(); for (Representation representation : aip.getRepresentations()) { boolean recursive = true; try (CloseableIterable<OptionalWithCause<File>> representationFiles = model.listFilesUnder(aip.getId(), representation.getId(), recursive)) { for (OptionalWithCause<File> subfile : representationFiles) { if (subfile.isPresent()) { dataInformation.add(retrieveFileInfo(fields, subfile.get(), aip, model, storage)); } else { LOGGER.error("Cannot retrieve file information", subfile.getCause()); } } } catch (NotFoundException | GenericException | RequestNotValidException | AuthorizationDeniedException | IOException e) { LOGGER.error("Error retrieving files of representation '{}' of AIP '{}': " + e.getMessage(), representation.getId(), aip.getId()); } } return dataInformation; }
private void processAIP(ModelService model, StorageService storage, JobPluginInfo jobPluginInfo, CSVPrinter csvFilePrinter, AIP aip) { if (csvFilePrinter == null) { LOGGER.warn("CSVPrinter is NULL! Skipping..."); return; } try { if (outputDataInformation && aip.getRepresentations() != null) { List<List<String>> dataInformation = InventoryReportPluginUtils.getDataInformation(fields, aip, model, storage); csvFilePrinter.printRecords(dataInformation); } if (outputDescriptiveMetadataInformation && aip.getDescriptiveMetadata() != null) { List<List<String>> dataInformation = InventoryReportPluginUtils.getDescriptiveMetadataInformation(fields, aip, model, storage); csvFilePrinter.printRecords(dataInformation); } if (otherMetadataTypes != null && !otherMetadataTypes.isEmpty()) { for (String otherMetadataType : otherMetadataTypes) { List<List<String>> otherMetadataInformation = InventoryReportPluginUtils.getOtherMetadataInformation(fields, otherMetadataType, aip, model, storage); csvFilePrinter.printRecords(otherMetadataInformation); } } jobPluginInfo.incrementObjectsProcessedWithSuccess(); } catch (IOException e) { LOGGER.error("Error writing CSV file", e); jobPluginInfo.incrementObjectsProcessedWithFailure(); } }
public CloseableIterable<OptionalWithCause<DescriptiveMetadata>> listDescriptiveMetadata(String aipId, boolean includeRepresentations) throws RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException { StoragePath storagePath = ModelUtils.getDescriptiveMetadataStoragePath(aipId, null); CloseableIterable<OptionalWithCause<DescriptiveMetadata>> aipDescriptiveMetadata; try { boolean recursive = true; CloseableIterable<Resource> resources = storage.listResourcesUnderDirectory(storagePath, recursive); aipDescriptiveMetadata = ResourceParseUtils.convert(getStorage(), resources, DescriptiveMetadata.class); } catch (NotFoundException e) { // check if AIP exists storage.getDirectory(ModelUtils.getAIPStoragePath(aipId)); // if no exception was sent by above method, return empty list aipDescriptiveMetadata = new EmptyClosableIterable<>(); } if (includeRepresentations) { List<CloseableIterable<OptionalWithCause<DescriptiveMetadata>>> list = new ArrayList<>(); list.add(aipDescriptiveMetadata); // list from all representations AIP aip = retrieveAIP(aipId); for (Representation representation : aip.getRepresentations()) { CloseableIterable<OptionalWithCause<DescriptiveMetadata>> rpm = listDescriptiveMetadata(aipId, representation.getId()); list.add(rpm); } return CloseableIterables.concat(list); } else { return aipDescriptiveMetadata; } }
public CloseableIterable<OptionalWithCause<PreservationMetadata>> listPreservationMetadata(String aipId, boolean includeRepresentations) throws RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException { StoragePath storagePath = ModelUtils.getAIPPreservationMetadataStoragePath(aipId); CloseableIterable<OptionalWithCause<PreservationMetadata>> aipPreservationMetadata; try { boolean recursive = true; CloseableIterable<Resource> resources = storage.listResourcesUnderDirectory(storagePath, recursive); aipPreservationMetadata = ResourceParseUtils.convert(getStorage(), resources, PreservationMetadata.class); } catch (NotFoundException e) { // check if AIP exists storage.getDirectory(ModelUtils.getAIPStoragePath(aipId)); // if no exception was sent by above method, return empty list aipPreservationMetadata = new EmptyClosableIterable<>(); } if (includeRepresentations) { List<CloseableIterable<OptionalWithCause<PreservationMetadata>>> list = new ArrayList<>(); list.add(aipPreservationMetadata); // list from all representations AIP aip = retrieveAIP(aipId); for (Representation representation : aip.getRepresentations()) { CloseableIterable<OptionalWithCause<PreservationMetadata>> rpm = listPreservationMetadata(aipId, representation.getId()); list.add(rpm); } return CloseableIterables.concat(list); } else { return aipPreservationMetadata; } }
@Override public ReturnWithExceptions<Void, ModelObserver> representationCreated(Representation representation) { ReturnWithExceptions<Void, ModelObserver> ret = new ReturnWithExceptions<>(this); try { AIP aip = model.retrieveAIP(representation.getAipId()); List<String> ancestors = SolrUtils.getAncestors(aip.getParentId(), model); indexRepresentation(aip, representation, ancestors).addTo(ret); if (ret.isEmpty()) { indexPreservationsEvents(aip.getId(), representation.getId()).addTo(ret); if (aip.getRepresentations().size() == 1) { SolrUtils.update(index, IndexedAIP.class, aip.getId(), Collections.singletonMap(RodaConstants.AIP_HAS_REPRESENTATIONS, true), (ModelObserver) this).addTo(ret); } } } catch (RequestNotValidException | NotFoundException | GenericException | AuthorizationDeniedException e) { LOGGER.error("Cannot index representation: {}", representation, e); ret.add(e); } return ret; }
private ReturnWithExceptions<Void, ModelObserver> updateRepresentationAndFileAncestors(AIP aip, List<String> ancestors) { ReturnWithExceptions<Void, ModelObserver> ret = new ReturnWithExceptions<>(this); for (Representation representation : aip.getRepresentations()) { SolrUtils.update(index, IndexedRepresentation.class, IdUtils.getRepresentationId(representation), Collections.singletonMap(RodaConstants.REPRESENTATION_ANCESTORS, ancestors), (ModelObserver) this).addTo(ret); if (ret.isEmpty()) { try (CloseableIterable<OptionalWithCause<File>> allFiles = model.listFilesUnder(aip.getId(), representation.getId(), true)) { for (OptionalWithCause<File> oFile : allFiles) { if (oFile.isPresent()) { File file = oFile.get(); SolrUtils.update(index, IndexedFile.class, IdUtils.getFileId(file), Collections.singletonMap(RodaConstants.FILE_ANCESTORS, ancestors), (ModelObserver) this).addTo(ret); } } } catch (RequestNotValidException | GenericException | AuthorizationDeniedException | IOException | NotFoundException e) { LOGGER.error("Error updating file ancestors", e); ret.add(e); } } } return ret; }
@Override public ReturnWithExceptions<Void, ModelObserver> representationDeleted(String aipId, String representationId, boolean deleteIncidences) { ReturnWithExceptions<Void, ModelObserver> ret = new ReturnWithExceptions<>(this); String representationUUID = IdUtils.getRepresentationId(aipId, representationId); deleteDocumentFromIndex(IndexedRepresentation.class, representationUUID).addTo(ret); deleteDocumentsFromIndex(IndexedFile.class, RodaConstants.FILE_REPRESENTATION_UUID, representationUUID).addTo(ret); deleteDocumentsFromIndex(IndexedPreservationEvent.class, RodaConstants.PRESERVATION_EVENT_REPRESENTATION_UUID, representationUUID).addTo(ret); if (deleteIncidences) { deleteDocumentsFromIndex(RiskIncidence.class, RodaConstants.RISK_INCIDENCE_REPRESENTATION_ID, representationId) .addTo(ret); } try { AIP aip = model.retrieveAIP(aipId); if (aip.getRepresentations().size() == 0) { SolrUtils.update(index, IndexedAIP.class, aip.getId(), Collections.singletonMap(RodaConstants.AIP_HAS_REPRESENTATIONS, false), (ModelObserver) this).addTo(ret); } } catch (AuthorizationDeniedException | RequestNotValidException | NotFoundException | GenericException e) { LOGGER.error("Cannot update hasRepresentations flag on AIP", e); ret.add(e); } return ret; }
public Representation createRepresentation(String aipId, String representationId, boolean original, String type, boolean notify, String createdBy) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException, AlreadyExistsException { RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); Representation representation = new Representation(representationId, aipId, original, type); representation.setCreatedBy(createdBy); representation.setUpdatedBy(createdBy); StoragePath directoryPath = ModelUtils.getRepresentationStoragePath(aipId, representationId); storage.createDirectory(directoryPath); // update AIP metadata AIP aip = ResourceParseUtils.getAIPMetadata(getStorage(), aipId); aip.getRepresentations().add(representation); updateAIPMetadata(aip); if (notify) { notifyRepresentationCreated(representation).failOnError(); } return representation; }
public Representation createRepresentation(String aipId, String representationId, boolean original, String type, StorageService sourceStorage, StoragePath sourcePath, boolean justData, String createdBy) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException, AlreadyExistsException { Representation representation; RodaCoreFactory.checkIfWriteIsAllowedAndIfFalseThrowException(nodeType); if (justData) { StoragePath dataPath = ModelUtils.getRepresentationDataStoragePath(aipId, representationId); StoragePath sourceDataPath = DefaultStoragePath.parse(sourcePath, RodaConstants.STORAGE_DIRECTORY_DATA); storage.copy(sourceStorage, sourceDataPath, dataPath); } else { StoragePath directoryPath = ModelUtils.getRepresentationStoragePath(aipId, representationId); // verify structure of source representation // 20170324 should we validate the representation??? storage.copy(sourceStorage, sourcePath, directoryPath); } representation = new Representation(representationId, aipId, original, type); representation.setCreatedBy(createdBy); representation.setUpdatedBy(createdBy); // update AIP metadata AIP aip = ResourceParseUtils.getAIPMetadata(getStorage(), aipId); aip.getRepresentations().add(representation); updateAIPMetadata(aip); notifyRepresentationCreated(representation).failOnError(); return representation; }