Search in sources :

Example 6 with MCRPI

use of org.mycore.pi.backend.MCRPI in project mycore by MyCoRe-Org.

the class MCRPIRegistrationService method insertIdentifierToDatabase.

public MCRPI insertIdentifierToDatabase(MCRBase obj, String additional, T identifier) {
    MCRPI databaseEntry = new MCRPI(identifier.asString(), getType(), obj.getId().toString(), additional, this.getRegistrationServiceID(), provideRegisterDate(obj, additional), null);
    MCRHIBConnection.instance().getSession().save(databaseEntry);
    return databaseEntry;
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI)

Example 7 with MCRPI

use of org.mycore.pi.backend.MCRPI in project mycore by MyCoRe-Org.

the class MCRPersistentIdentifierManager method delete.

public void delete(String objectID, String additional, String type, String service) {
    Objects.requireNonNull(objectID, "objectId may not be null");
    Objects.requireNonNull(type, "type may not be null");
    Objects.requireNonNull(additional, "additional may not be null");
    Objects.requireNonNull(service, "service may not be null");
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRPI> getQuery = cb.createQuery(MCRPI.class);
    Root<MCRPI> pi = getQuery.from(MCRPI.class);
    em.remove(em.createQuery(getQuery.where(cb.equal(pi.get(MCRPI_.mycoreID), objectID), cb.equal(pi.get(MCRPI_.type), type), cb.equal(pi.get(MCRPI_.additional), additional), cb.equal(pi.get(MCRPI_.service), service))).getSingleResult());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRPI(org.mycore.pi.backend.MCRPI)

Example 8 with MCRPI

use of org.mycore.pi.backend.MCRPI in project mycore by MyCoRe-Org.

the class MCRPICommands method removeControlFromObject.

@MCRCommand(syntax = "remove control {0} with service {1} with additional {2}", help = "This commands removes the " + "pi control from the object {0}(object id) with the serivce {1}(service id) and the additional {2}")
public static void removeControlFromObject(String objectIDString, String serviceID, String additional) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    MCRObjectID objectID = MCRObjectID.getInstance(objectIDString);
    MCRPI mcrpi = MCRPersistentIdentifierManager.getInstance().get(serviceID, objectIDString, additional != null ? additional.trim() : null);
    MCRPersistentIdentifierManager.getInstance().delete(mcrpi.getMycoreID(), mcrpi.getAdditional(), mcrpi.getType(), mcrpi.getService());
    MCRBase base = MCRMetadataManager.retrieve(objectID);
    if (MCRPIRegistrationService.removeFlagFromObject(base, mcrpi) == null) {
        throw new MCRPersistentIdentifierException("Could not delete Flag of object (flag not found)!");
    }
    MCRMetadataManager.update(base);
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 9 with MCRPI

use of org.mycore.pi.backend.MCRPI in project mycore by MyCoRe-Org.

the class MCRPICommands method controlObjectWithServiceAndAdditional.

@MCRCommand(syntax = "try to control {0} with service {1} with additional {2}", help = "This command tries to" + " read a pi from the object {0} with the MetadataManager from the specified service {1}." + " If the service configuration is right then the pi is under control of MyCoRe.")
public static void controlObjectWithServiceAndAdditional(String objectIDString, String serviceID, final String additional) throws MCRAccessException, MCRActiveLinkException, IOException {
    String trimAdditional = additional.trim();
    MCRPIRegistrationService<MCRPersistentIdentifier> service = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(serviceID);
    MCRPersistentIdentifierMetadataManager<MCRPersistentIdentifier> metadataManager = service.getMetadataManager();
    MCRObjectID objectID = MCRObjectID.getInstance(objectIDString);
    MCRBase mcrBase = MCRMetadataManager.retrieve(objectID);
    Optional<MCRPersistentIdentifier> identifier;
    try {
        identifier = metadataManager.getIdentifier(mcrBase, trimAdditional);
    } catch (MCRPersistentIdentifierException e) {
        LOGGER.info("Could not detect any identifier with service {}", serviceID, e);
        return;
    }
    if (!identifier.isPresent()) {
        LOGGER.info("Could not detect any identifier with service {}", serviceID);
        return;
    }
    MCRPersistentIdentifier persistentIdentifier = identifier.get();
    if (service.isRegistered(objectID, trimAdditional)) {
        LOGGER.info("Already present in Database: {}", serviceID);
        return;
    }
    MCRPI mcrpi = service.insertIdentifierToDatabase(mcrBase, trimAdditional, persistentIdentifier);
    MCRPIRegistrationService.addFlagToObject(mcrBase, mcrpi);
    MCRMetadataManager.update(mcrBase);
    LOGGER.info("{}:{} is now under control of {}", objectID, trimAdditional, serviceID);
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 10 with MCRPI

use of org.mycore.pi.backend.MCRPI in project mycore by MyCoRe-Org.

the class MCRPIJobRegistrationService method updateRegistrationDate.

/**
 * Can be used to update the registration date in the database. The most {@link MCRPIJobRegistrationService}
 * only add the pi to the object and then to the database, with registration date of null. Later the job will
 * register the pi and then change the registration date to the right value.
 * <b>If you use this methods from a job you should have called {@link #validateJobUserRights} before!</b>
 * @param mycoreID the id of the {@link org.mycore.datamodel.metadata.MCRBase} which has the pi assigned
 * @param additional information like path to a file
 * @param date the new registration date
 */
protected void updateRegistrationDate(MCRObjectID mycoreID, String additional, Date date) {
    MCRPI pi = MCRPersistentIdentifierManager.getInstance().get(this.getRegistrationServiceID(), mycoreID.toString(), additional);
    pi.setRegistered(date);
    updateFlag(mycoreID, additional, pi);
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI)

Aggregations

MCRPI (org.mycore.pi.backend.MCRPI)23 Date (java.util.Date)8 EntityManager (javax.persistence.EntityManager)6 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)6 MCRBase (org.mycore.datamodel.metadata.MCRBase)4 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)4 Session (org.hibernate.Session)3 MCRAccessException (org.mycore.access.MCRAccessException)3 MCRException (org.mycore.common.MCRException)3 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)3 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRObject (org.mycore.datamodel.metadata.MCRObject)3 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Test (org.junit.Test)2