Search in sources :

Example 11 with MCRPI

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

the class MCRPIJobRegistrationService method updateStartRegistrationDate.

/**
 * Can be used to update the startRegistration date in the database. The most {@link MCRPIJobRegistrationService}
 * only add the pi to the object and then to the database, with registration or startRegistration date of null.
 * After a job is created the Registration service should update the date.
 * <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 updateStartRegistrationDate(MCRObjectID mycoreID, String additional, Date date) {
    MCRPI pi = MCRPersistentIdentifierManager.getInstance().get(this.getRegistrationServiceID(), mycoreID.toString(), additional);
    pi.setRegistrationStarted(date);
    updateFlag(mycoreID, additional, pi);
}
Also used : MCRPI(org.mycore.pi.backend.MCRPI)

Example 12 with MCRPI

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

the class MCRPIRegistrationService method updateFlag.

public void updateFlag(MCRObjectID id, String additional, MCRPI mcrpi) {
    MCRBase obj = MCRMetadataManager.retrieve(id);
    MCRObjectService service = obj.getService();
    ArrayList<String> flags = service.getFlags(MCRPIRegistrationService.PI_FLAG);
    Gson gson = getGson();
    String stringFlag = flags.stream().filter(_stringFlag -> {
        MCRPI flag = gson.fromJson(_stringFlag, MCRPI.class);
        return flag.getAdditional().equals(additional) && flag.getIdentifier().equals(mcrpi.getIdentifier());
    }).findAny().orElseThrow(() -> new MCRException(new MCRPersistentIdentifierException("Could find flag to update (" + id + "," + additional + "," + mcrpi.getIdentifier() + ")")));
    int flagIndex = service.getFlagIndex(stringFlag);
    service.removeFlag(flagIndex);
    addFlagToObject(obj, mcrpi);
    try {
        MCRMetadataManager.update(obj);
    } catch (Exception e) {
        throw new MCRException("Could not update flags of object " + id, e);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) Gson(com.google.gson.Gson) MCRPI(org.mycore.pi.backend.MCRPI) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRException(org.mycore.common.MCRException) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 13 with MCRPI

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

the class MCRPIRegistrationService method register.

/**
 * Validates everything, registers a new Identifier, inserts the identifier to object metadata and writes a
 * information to the Database.
 *
 * @param obj the object which has to be identified
 * @param additional additional information for the persistent identifier
 * @param updateObject if true this method calls {@link MCRMetadataManager#update(MCRBase)}
 * @return the assigned Identifier
 * @throws MCRAccessException               the current User doesn't have the rights to insert the Identifier to Metadata
 * @throws MCRActiveLinkException           the {@link MCRPersistentIdentifierMetadataManager} lets
 * {@link org.mycore.datamodel.metadata.MCRMetadataManager#update(MCRObject)} throw this
 * @throws MCRPersistentIdentifierException see {@link org.mycore.pi.exceptions}
 */
public T register(MCRBase obj, String additional, boolean updateObject) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    this.validateRegistration(obj, additional);
    T identifier = this.registerIdentifier(obj, additional);
    this.getMetadataManager().insertIdentifier(identifier, obj, additional);
    MCRPI databaseEntry = insertIdentifierToDatabase(obj, additional, identifier);
    addFlagToObject(obj, databaseEntry);
    if (updateObject) {
        if (obj instanceof MCRObject) {
            MCRMetadataManager.update((MCRObject) obj);
        } else if (obj instanceof MCRDerivate) {
            MCRMetadataManager.update((MCRDerivate) obj);
        }
    }
    return identifier;
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRPI(org.mycore.pi.backend.MCRPI) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate)

Example 14 with MCRPI

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

the class MCRPIRegistrationService method removeFlagFromObject.

/**
 * Removes a flag from a {@link MCRObject}
 * @param obj the object
 * @param databaseEntry the database entry
 * @return the remove entry parsed from json or null
 */
public static MCRPI removeFlagFromObject(MCRBase obj, MCRPI databaseEntry) {
    MCRObjectService service = obj.getService();
    ArrayList<String> flags = service.getFlags(MCRPIRegistrationService.PI_FLAG);
    int flagCount = flags.size();
    for (int flagIndex = 0; flagIndex < flagCount; flagIndex++) {
        String flag = flags.get(flagIndex);
        MCRPI pi = getGson().fromJson(flag, MCRPI.class);
        if (pi.getIdentifier().equals(databaseEntry.getIdentifier()) && pi.getAdditional().equals(databaseEntry.getAdditional()) && pi.getService().equals(databaseEntry.getService()) && pi.getType().equals(databaseEntry.getType())) {
            service.removeFlag(flagIndex);
            return databaseEntry;
        }
    }
    return null;
}
Also used : MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) MCRPI(org.mycore.pi.backend.MCRPI)

Example 15 with MCRPI

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

the class MCRPersistentIdentifierManager method getList.

public List<MCRPIRegistrationInfo> getList(String type, int from, int count) {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRPIRegistrationInfo> getQuery = cb.createQuery(MCRPIRegistrationInfo.class);
    Root<MCRPI> pi = getQuery.from(MCRPI.class);
    CriteriaQuery<MCRPIRegistrationInfo> all = getQuery.select(pi);
    if (type != null) {
        all = all.where(cb.equal(pi.get(MCRPI_.type), type));
    }
    TypedQuery<MCRPIRegistrationInfo> typedQuery = em.createQuery(all);
    if (from != -1) {
        typedQuery = typedQuery.setFirstResult(from);
    }
    if (count != -1) {
        typedQuery = typedQuery.setMaxResults(count);
    }
    return typedQuery.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) 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