Search in sources :

Example 1 with MCRObjectService

use of org.mycore.datamodel.metadata.MCRObjectService in project mycore by MyCoRe-Org.

the class MCRMigrationCommands method addServFlags.

@MCRCommand(syntax = "migrate author servflags for {0}", help = "Create missing servflags for createdby and modifiedby for object {0}. (MCR-786)", order = 10)
public static void addServFlags(String id) throws IOException, MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    MCRBase obj = MCRMetadataManager.retrieve(objectID);
    MCRObjectService service = obj.getService();
    if (!service.isFlagTypeSet(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
        // the egg
        MCRVersionedMetadata versionedMetadata = MCRXMLMetadataManager.instance().getVersionedMetaData(objectID);
        String createUser = null, modifyUser = null;
        if (versionedMetadata == null) {
            LOGGER.warn("Cannot restore author servflags as there are no versions available. Setting to current user.");
            createUser = MCRSessionMgr.getCurrentSession().getUserInformation().getUserID();
            modifyUser = createUser;
        } else {
            List<MCRMetadataVersion> versions = versionedMetadata.listVersions();
            MCRMetadataVersion firstVersion = versions.get(0);
            for (MCRMetadataVersion version : versions) {
                if (version.getType() == 'A') {
                    // get last 'added'
                    firstVersion = version;
                }
            }
            MCRMetadataVersion lastVersion = versions.get(versions.size() - 1);
            createUser = firstVersion.getUser();
            modifyUser = lastVersion.getUser();
        }
        service.addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, createUser);
        LOGGER.info("{}, created by: {}", objectID, createUser);
        if (!service.isFlagTypeSet(MCRObjectService.FLAG_TYPE_MODIFIEDBY)) {
            // the chicken
            // have to restore also modifiedby from version history.
            LOGGER.info("{}, modified by: {}", objectID, modifyUser);
            service.addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, modifyUser);
        }
        obj.setImportMode(true);
        if (obj instanceof MCRDerivate) {
            MCRMetadataManager.updateMCRDerivateXML((MCRDerivate) obj);
        } else {
            MCRMetadataManager.update((MCRObject) obj);
        }
    }
}
Also used : MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 2 with MCRObjectService

use of org.mycore.datamodel.metadata.MCRObjectService in project mycore by MyCoRe-Org.

the class MCRSwordUtil method addDatesToEntry.

public static void addDatesToEntry(Entry entry, MCRObject mcrObject) {
    MCRObjectService serviceElement = mcrObject.getService();
    ArrayList<String> flags = serviceElement.getFlags(MCRObjectService.FLAG_TYPE_CREATEDBY);
    flags.addAll(serviceElement.getFlags(MCRObjectService.FLAG_TYPE_MODIFIEDBY));
    Set<String> clearedFlags = new LinkedHashSet<>(flags);
    clearedFlags.forEach(entry::addAuthor);
    Date modifyDate = serviceElement.getDate(MCRObjectService.DATE_TYPE_MODIFYDATE);
    Date createDate = serviceElement.getDate(MCRObjectService.DATE_TYPE_CREATEDATE);
    entry.setEdited(modifyDate);
    entry.setPublished(createDate);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) Date(java.util.Date)

Example 3 with MCRObjectService

use of org.mycore.datamodel.metadata.MCRObjectService 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 4 with MCRObjectService

use of org.mycore.datamodel.metadata.MCRObjectService 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)

Aggregations

MCRObjectService (org.mycore.datamodel.metadata.MCRObjectService)4 MCRBase (org.mycore.datamodel.metadata.MCRBase)2 MCRPI (org.mycore.pi.backend.MCRPI)2 Gson (com.google.gson.Gson)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Date (java.util.Date)1 LinkedHashSet (java.util.LinkedHashSet)1 MCRAccessException (org.mycore.access.MCRAccessException)1 MCRException (org.mycore.common.MCRException)1 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)1 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)1 MCRMetadataVersion (org.mycore.datamodel.ifs2.MCRMetadataVersion)1 MCRVersionedMetadata (org.mycore.datamodel.ifs2.MCRVersionedMetadata)1 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)1 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)1 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)1 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)1