001package systems.dmx.accesscontrol.migrations;
002
003import systems.dmx.core.Association;
004import systems.dmx.core.DMXObject;
005import systems.dmx.core.Topic;
006import systems.dmx.core.service.Migration;
007
008import java.util.logging.Logger;
009
010
011
012public class Migration2 extends Migration {
013
014    // ---------------------------------------------------------------------------------------------- Instance Variables
015
016    private long count;
017
018    private Logger logger = Logger.getLogger(getClass().getName());
019
020    // -------------------------------------------------------------------------------------------------- Public Methods
021
022    @Override
023    public void run() {
024        count = 0;
025        for (Topic topic : dmx.getAllTopics()) {
026            migrateObject(topic, "topic");
027        }
028        count = 0;
029        for (Association assoc : dmx.getAllAssociations()) {
030            migrateObject(assoc, "association");
031        }
032    }
033
034    // ------------------------------------------------------------------------------------------------- Private Methods
035
036    private void migrateObject(DMXObject object, String type) {
037        try {
038            count++;
039            String info = "### Migrating " + type + " " + object.getId() + " (#" + count + ")";
040            if (object.hasProperty("creator")) {
041                logger.info(info);
042                renameProperty(object, "creator", "dmx.accesscontrol.creator", true);   // addToIndex=true
043                renameProperty(object, "owner",   "dmx.accesscontrol.owner",   true);   // addToIndex=true
044                renameProperty(object, "acl",     "dmx.accesscontrol.acl",     false);  // addToIndex=false
045            } else {
046                logger.info(info + " SKIPPED -- Access control information not availble");
047            }
048        } catch (Exception e) {
049            throw new RuntimeException("Migrating " + type + " " + object.getId() + " failed (" + object + ")", e);
050        }
051    }
052
053    private void renameProperty(DMXObject object, String oldPropUri, String newPropUri, boolean addToIndex) {
054        String propValue = (String) object.getProperty(oldPropUri);
055        object.setProperty(newPropUri, propValue, addToIndex);
056        object.removeProperty(oldPropUri);
057    }
058}