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);
}
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);
}
}
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;
}
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;
}
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();
}
Aggregations