001    package de.deepamehta.plugins.boxrenderer.dom.migrations;
002    
003    import de.deepamehta.core.Association;
004    import de.deepamehta.core.RelatedTopic;
005    import de.deepamehta.core.Topic;
006    import de.deepamehta.core.service.Migration;
007    
008    import java.util.logging.Logger;
009    
010    
011    
012    /**
013     * Moves the BoxRenderer properties from topics to "Topic Mapcontext" associations.
014     * Runs only in UPDATE mode.
015     * <p>
016     * Part of DM 4.6
017     */
018    public class Migration1 extends Migration {
019    
020        // ------------------------------------------------------------------------------------------------------- Constants
021    
022        private static final String PROP_COLOR    = "dm4.boxrenderer.color";
023        private static final String PROP_EXPANDED = "dm4.boxrenderer.expanded";
024    
025        private static final String TOPIC_MAPCONTEXT = "dm4.topicmaps.topic_mapcontext";
026    
027        // ---------------------------------------------------------------------------------------------- Instance Variables
028    
029        private long topics = 0, props = 0;
030    
031        private Logger logger = Logger.getLogger(getClass().getName());
032    
033        // -------------------------------------------------------------------------------------------------- Public Methods
034    
035        @Override
036        public void run() {
037            logger.info("########## Moving BoxRenderer properties from topics to \"Topic Mapcontext\" associations");
038            //
039            for (Topic topic : dms.getAllTopics()) {
040                migrateBoxRendererProperties(topic);
041            }
042            //
043            logger.info("########## Moving BoxRenderer properties complete.\n    Topics processed: " +
044                topics + "\n    Topics with BoxRenderer properties: " + props);
045        }
046    
047        // ------------------------------------------------------------------------------------------------- Private Methods
048    
049        private void migrateBoxRendererProperties(Topic topic) {
050            String color = null;
051            Boolean expanded = null;
052            topics++;
053            //
054            if (topic.hasProperty(PROP_COLOR)) {
055                color = (String) topic.getProperty(PROP_COLOR);
056            }
057            if (topic.hasProperty(PROP_EXPANDED)) {
058                expanded = (Boolean) topic.getProperty(PROP_EXPANDED);
059            }
060            //
061            if (color != null || expanded != null) {
062                props++;
063                for (RelatedTopic topicmap : topic.getRelatedTopics(TOPIC_MAPCONTEXT, "dm4.topicmaps.topicmap_topic",
064                                                                    "dm4.core.default", "dm4.topicmaps.topicmap", 0)) {
065                    Association mapcontextAssoc = topicmap.getRelatingAssociation();
066                    if (color != null) {
067                        mapcontextAssoc.setProperty(PROP_COLOR, color, false);          // addToIndex = false
068                    }
069                    if (expanded != null) {
070                        mapcontextAssoc.setProperty(PROP_EXPANDED, expanded, false);    // addToIndex = false
071                    }
072                }
073                // 
074                topic.removeProperty(PROP_COLOR);
075                topic.removeProperty(PROP_EXPANDED);
076            }
077        }
078    }