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 void addDescriptiveMetadata(DescriptiveMetadata descriptiveMetadata) { if (descriptiveMetadata.isFromAIP()) { this.descriptiveMetadata.add(descriptiveMetadata); } else { for (Representation representation : this.representations) { if (representation.getId().equals(descriptiveMetadata.getRepresentationId())) { representation.addDescriptiveMetadata(descriptiveMetadata); break; } } } }
public static String getRepresentationId(Representation representation) { return getRepresentationId(representation.getAipId(), representation.getId()); }
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); } }
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; }
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 void processRepresentation(ModelService model, Report report, JobPluginInfo jobPluginInfo, Job job, List<Representation> representations) { for (Representation representation : representations) { Report reportItem = PluginHelper.initPluginReportItem(this, representation.getId(), Representation.class); PluginHelper.updatePartialJobReport(this, model, reportItem, false, job); PluginState state = PluginState.SUCCESS; try { model.changeRepresentationType(representation.getAipId(), representation.getId(), newType, job.getUsername()); } catch (RequestNotValidException | NotFoundException | GenericException | AuthorizationDeniedException e) { state = PluginState.FAILURE; } finally { jobPluginInfo.incrementObjectsProcessed(state); reportItem.setPluginState(state); StringBuilder outcomeText = new StringBuilder().append("The representation '").append(representation.getId()) .append(" of AIP ").append(representation.getAipId()).append("' changed its type from '") .append(representation.getType()).append("' to '").append(newType).append("'."); model.createUpdateAIPEvent(representation.getAipId(), representation.getId(), null, null, PreservationEventType.UPDATE, EVENT_DESCRIPTION, state, outcomeText.toString(), details, job.getUsername(), true); report.addReport(reportItem); PluginHelper.updatePartialJobReport(this, model, reportItem, true, job); } } }
/**********************************************************/ 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 static <T extends IsRODAObject> List<LinkingIdentifier> runSiegfriedOnRepresentation(ModelService model, Representation representation) throws GenericException, RequestNotValidException, NotFoundException, AuthorizationDeniedException, PluginException { StoragePath representationDataPath = ModelUtils.getRepresentationDataStoragePath(representation.getAipId(), representation.getId()); try (DirectResourceAccess directAccess = model.getStorage().getDirectAccess(representationDataPath)) { Path representationFsPath = directAccess.getPath(); List<LinkingIdentifier> sources = runSiegfriedOnRepresentationOrFile(model, representation.getAipId(), representation.getId(), new ArrayList<>(), null, representationFsPath); return sources; } catch (IOException e) { throw new GenericException(e); } }
private void createPremisSkeletonOnRepresentation(ModelService model, String aipId, Representation representation) throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException, ValidationException, IOException, XmlException { List<String> algorithms = RodaCoreFactory.getFixityAlgorithms(); PremisSkeletonPluginUtils.createPremisSkeletonOnRepresentation(model, aipId, representation.getId(), algorithms); model.notifyRepresentationUpdated(representation).failOnError(); }
@Override public ReturnWithExceptions<Void, ModelObserver> representationUpdated(Representation representation) { ReturnWithExceptions<Void, ModelObserver> ret = representationDeleted(representation.getAipId(), representation.getId(), false); representationCreated(representation).addTo(ret); 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); }
private CloseableIterable<OptionalWithCause<File>> listFiles() throws RequestNotValidException, GenericException, NotFoundException, AuthorizationDeniedException { CloseableIterable<OptionalWithCause<Representation>> representations = listRepresentations(); return CloseableIterables.concat(representations, rep -> { if (rep.isPresent()) { Representation representation = rep.get(); try { return listFilesUnder(representation.getAipId(), representation.getId(), true); } catch (RODAException e) { LOGGER.error("Error listing files under representation: {}", representation.getId(), e); return CloseableIterables.empty(); } } else { return CloseableIterables.empty(); } }); }
public void setId(String id) { this.id = id; // As id is not serialized to JSON, set the AIP id in metadata and data if (descriptiveMetadata != null) { for (DescriptiveMetadata dm : descriptiveMetadata) { dm.setAipId(id); } } if (representations != null) { for (Representation representation : representations) { representation.setAipId(id); for (DescriptiveMetadata repDm : representation.getDescriptiveMetadata()) { repDm.setAipId(id); repDm.setRepresentationId(representation.getId()); } } } }
public CloseableIterable<OptionalWithCause<OtherMetadata>> listOtherMetadata(String aipId, String type, boolean includeRepresentations) throws RequestNotValidException, NotFoundException, GenericException, AuthorizationDeniedException { StoragePath storagePath = ModelUtils.getAIPOtherMetadataStoragePath(aipId, type); boolean recursive = true; CloseableIterable<OptionalWithCause<OtherMetadata>> aipOtherMetadata; try { CloseableIterable<Resource> resources = storage.listResourcesUnderDirectory(storagePath, recursive); aipOtherMetadata = ResourceParseUtils.convert(getStorage(), resources, OtherMetadata.class); } catch (NotFoundException e) { // check if AIP exists storage.getDirectory(ModelUtils.getAIPStoragePath(aipId)); // if no exception was sent by above method, return empty list aipOtherMetadata = new EmptyClosableIterable<>(); } if (includeRepresentations) { List<CloseableIterable<OptionalWithCause<OtherMetadata>>> list = new ArrayList<>(); list.add(aipOtherMetadata); // list from all representations AIP aip = retrieveAIP(aipId); for (Representation representation : aip.getRepresentations()) { CloseableIterable<OptionalWithCause<OtherMetadata>> representationOtherMetadata = listOtherMetadata(aipId, representation.getId(), null, null, type); list.add(representationOtherMetadata); } return CloseableIterables.concat(list); } else { return aipOtherMetadata; } }
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> 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> representationPermissionsUpdated(final AIP aip, final Representation representation) { ReturnWithExceptions<Void, ModelObserver> ret = new ReturnWithExceptions<>(this); SolrUtils.update(index, IndexedRepresentation.class, IdUtils.getRepresentationId(representation), SolrUtils.getPermissionsAsPreCalculatedFields(aip.getPermissions()), (ModelObserver) this).addTo(ret); if (ret.isEmpty()) { try (CloseableIterable<OptionalWithCause<File>> allFiles = model.listFilesUnder(representation.getAipId(), representation.getId(), true)) { for (OptionalWithCause<File> file : allFiles) { if (file.isPresent()) { SolrUtils.update(index, IndexedFile.class, IdUtils.getFileId(file.get()), SolrUtils.getPermissionsAsPreCalculatedFields(aip.getPermissions()), (ModelObserver) this).addTo(ret); } else { LOGGER.error("Cannot do a partial update on file", file.getCause()); ret.add(file.getCause()); } } } catch (AuthorizationDeniedException | IOException | NotFoundException | GenericException | RequestNotValidException e) { LOGGER.error("Cannot do a partial update", e); ret.add(e); } } return ret; }
public static AIP bagitToAip(SIP bagit, ModelService model, String metadataFilename, List<String> ingestSIPIds, String ingestJobId, Optional<String> computedParentId, String createdBy, Permissions permissions, String ingestSIPUUID) throws RequestNotValidException, NotFoundException, GenericException, AlreadyExistsException, AuthorizationDeniedException { String metadataAsString = BagitUtils .generateMetadataFile(bagit.getDescriptiveMetadata().get(0).getMetadata().getPath()); ContentPayload metadataAsPayload = new StringContentPayload(metadataAsString); AIPState state = AIPState.INGEST_PROCESSING; String aipType = RodaConstants.AIP_TYPE_MIXED; boolean notify = false; AIP aip = model.createAIP(state, computedParentId.orElse(null), aipType, permissions, ingestSIPUUID, ingestSIPIds, ingestJobId, notify, createdBy); model.createDescriptiveMetadata(aip.getId(), metadataFilename, metadataAsPayload, METADATA_TYPE, METADATA_VERSION, notify); boolean original = true; String representationType = RodaConstants.REPRESENTATION_TYPE_MIXED; for (IPRepresentation irep : bagit.getRepresentations()) { Representation rep = model.createRepresentation(aip.getId(), irep.getRepresentationID(), original, representationType, notify, createdBy); for (IPFile bagFile : irep.getData()) { ContentPayload payload = new FSPathContentPayload(bagFile.getPath()); model.createFile(aip.getId(), rep.getId(), bagFile.getRelativeFolders(), bagFile.getFileName(), payload, notify); } } model.notifyAipCreated(aip.getId()); return model.retrieveAIP(aip.getId()); }