001package systems.dmx.topicmaps.migrations;
002
003import systems.dmx.core.Association;
004import systems.dmx.core.ChildTopics;
005import systems.dmx.core.Topic;
006import systems.dmx.core.service.Migration;
007
008import java.util.logging.Logger;
009
010
011
012/**
013 * Converts the "Topic Mapcontext" association's child topics into properties.
014 * Runs only in UPDATE mode.
015 * <p>
016 * Part of DM 4.6
017 */
018public class Migration4 extends Migration {
019
020    // ---------------------------------------------------------------------------------------------- Instance Variables
021
022    private long assocs = 0, topicsDeleted = 0, typesDeleted = 0;
023
024    private Logger logger = Logger.getLogger(getClass().getName());
025
026    // -------------------------------------------------------------------------------------------------- Public Methods
027
028    @Override
029    public void run() {
030        logger.info("########## Converting \"Topic Mapcontext\" associations");
031        //
032        // 1) convert the "Topic Mapcontext" association's child topics into properties
033        for (Association assoc : dmx.getAssociationsByType("dmx.topicmaps.topic_mapcontext")) {
034            migrateMapcontextAssociation(assoc);
035        }
036        //
037        // 2) delete "Topic Mapcontext" child types
038        deleteTopicType("dmx.topicmaps.x");
039        deleteTopicType("dmx.topicmaps.y");
040        deleteTopicType("dmx.topicmaps.visibility");
041        //
042        // 3) make "Topic Mapcontext" a simple type
043        dmx.getAssociationType("dmx.topicmaps.topic_mapcontext").setDataTypeUri("dmx.core.text");
044        //
045        logger.info("########## Converting \"Topic Mapcontext\" associations complete\n    Associations processed: " +
046            assocs + "\n    X, Y, Visibility topics deleted: " + topicsDeleted + "\n    Topic types deleted: " +
047            typesDeleted);
048    }
049
050    // ------------------------------------------------------------------------------------------------- Private Methods
051
052    private void migrateMapcontextAssociation(Association assoc) {
053        assocs++;
054        //
055        ChildTopics childs = assoc.getChildTopics();
056        int x = childs.getInt("dmx.topicmaps.x");
057        int y = childs.getInt("dmx.topicmaps.y");
058        boolean visibility = childs.getBoolean("dmx.topicmaps.visibility");
059        //
060        assoc.setProperty("dmx.topicmaps.x", x, false);                     // addToIndex = false
061        assoc.setProperty("dmx.topicmaps.y", y, false);                     // addToIndex = false
062        assoc.setProperty("dmx.topicmaps.visibility", visibility, false);   // addToIndex = false
063    }
064
065    private void deleteTopicType(String topicTypeUri) {
066        typesDeleted++;
067        // delete instances
068        for (Topic topic : dmx.getTopicsByType(topicTypeUri)) {
069            topic.delete();
070            topicsDeleted++;
071        }
072        // delete type
073        dmx.deleteTopicType(topicTypeUri);
074    }
075}