001package de.deepamehta.topicmaps.migrations;
002
003import de.deepamehta.core.Association;
004import de.deepamehta.core.ChildTopics;
005import de.deepamehta.core.Topic;
006import de.deepamehta.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 : dm4.getAssociationsByType("dm4.topicmaps.topic_mapcontext")) {
034            migrateMapcontextAssociation(assoc);
035        }
036        //
037        // 2) delete "Topic Mapcontext" child types
038        deleteTopicType("dm4.topicmaps.x");
039        deleteTopicType("dm4.topicmaps.y");
040        deleteTopicType("dm4.topicmaps.visibility");
041        //
042        // 3) make "Topic Mapcontext" a simple type
043        dm4.getAssociationType("dm4.topicmaps.topic_mapcontext").setDataTypeUri("dm4.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("dm4.topicmaps.x");
057        int y = childs.getInt("dm4.topicmaps.y");
058        boolean visibility = childs.getBoolean("dm4.topicmaps.visibility");
059        //
060        assoc.setProperty("dm4.topicmaps.x", x, false);                     // addToIndex = false
061        assoc.setProperty("dm4.topicmaps.y", y, false);                     // addToIndex = false
062        assoc.setProperty("dm4.topicmaps.visibility", visibility, false);   // addToIndex = false
063    }
064
065    private void deleteTopicType(String topicTypeUri) {
066        typesDeleted++;
067        // delete instances
068        for (Topic topic : dm4.getTopicsByType(topicTypeUri)) {
069            topic.delete();
070            topicsDeleted++;
071        }
072        // delete type
073        dm4.deleteTopicType(topicTypeUri);
074    }
075}