001package de.deepamehta.accesscontrol.migrations;
002
003import de.deepamehta.core.Association;
004import de.deepamehta.core.DeepaMehtaObject;
005import de.deepamehta.core.Topic;
006import de.deepamehta.core.service.Migration;
007
008import java.util.logging.Logger;
009
010
011
012/**
013 * Removes "owner" and "acl" properties.
014 * Runs only in UPDATE mode.
015 * <p>
016 * Part of DM 4.5
017 */
018public class Migration6 extends Migration {
019
020    // ---------------------------------------------------------------------------------------------- Instance Variables
021
022    private long objects = 0, ownerRemoved = 0, aclRemoved = 0;
023
024    private Logger logger = Logger.getLogger(getClass().getName());
025
026    // -------------------------------------------------------------------------------------------------- Public Methods
027
028    @Override
029    public void run() {
030        logger.info("########## Removing owner and acl properties");
031        for (Topic topic : dm4.getAllTopics()) {
032            migrateObject(topic, "topic");
033        }
034        for (Association assoc : dm4.getAllAssociations()) {
035            migrateObject(assoc, "association");
036        }
037        logger.info("########## Removing owner and acl properties complete\n    Objects processed: " + objects +
038            "\n    Owner properties removed: " + ownerRemoved + "\n    ACL properties removed: " + aclRemoved);
039    }
040
041    // ------------------------------------------------------------------------------------------------- Private Methods
042
043    private void migrateObject(DeepaMehtaObject object, String type) {
044        try {
045            objects++;
046            if (object.hasProperty("dm4.accesscontrol.owner")) {
047                // only workspaces keep the "owner" property
048                if (!object.getTypeUri().equals("dm4.workspaces.workspace")) {
049                    object.removeProperty("dm4.accesscontrol.owner");
050                    ownerRemoved++;
051                }
052            }
053            if (object.hasProperty("dm4.accesscontrol.acl")) {
054                object.removeProperty("dm4.accesscontrol.acl");
055                aclRemoved++;
056            }
057        } catch (Exception e) {
058            throw new RuntimeException("Migrating " + type + " " + object.getId() + " failed (" + object + ")", e);
059        }
060    }
061}