001    package de.deepamehta.plugins.workspaces.migrations;
002    
003    import de.deepamehta.plugins.workspaces.service.WorkspacesService;
004    
005    import de.deepamehta.core.AssociationDefinition;
006    import de.deepamehta.core.AssociationType;
007    import de.deepamehta.core.DeepaMehtaObject;
008    import de.deepamehta.core.Topic;
009    import de.deepamehta.core.TopicType;
010    import de.deepamehta.core.Type;
011    import de.deepamehta.core.service.Inject;
012    import de.deepamehta.core.service.Migration;
013    
014    import java.util.logging.Logger;
015    
016    
017    
018    /**
019     * Assigns standard association definitions (and their view config topics) to DeepaMehta workspace.
020     * Runs only in UPDATE mode.
021     * <p>
022     * Part of DM 4.6
023     */
024    public class Migration6 extends Migration {
025    
026        // ------------------------------------------------------------------------------------------------------- Constants
027    
028        // Note: copy in WorkspacesPlugin.java
029        private static final String PROP_WORKSPACE_ID = "dm4.workspaces.workspace_id";
030    
031        // ---------------------------------------------------------------------------------------------- Instance Variables
032    
033        @Inject
034        private WorkspacesService wsService;
035    
036        private long deepamehtaWorkspaceId;
037    
038        private long types = 0, standardTypes = 0, assocDefs = 0, configTopics = 0;
039    
040        private Logger logger = Logger.getLogger(getClass().getName());
041    
042        // -------------------------------------------------------------------------------------------------- Public Methods
043    
044        @Override
045        public void run() {
046            logger.info("########## Assigning standard association definitions (and their view config topics) to " +
047                "DeepaMehta workspace");
048            deepamehtaWorkspaceId = getDeepaMehtaWorkspace().getId();
049            //
050            for (TopicType topicType : dms.getAllTopicTypes()) {
051                assignWorkspace(topicType);
052            }
053            for (AssociationType assocType : dms.getAllAssociationTypes()) {
054                assignWorkspace(assocType);
055            }
056            //
057            logger.info("########## Assigning standard association definitions (and their view config topics) to " +
058                "DeepaMehta workspace complete.\n    Types processed: " + types + "\n    Standard types: " +
059                standardTypes + "\n    Association definitions: " + assocDefs + "\n    View config topics: " +
060                configTopics);
061        }
062    
063        // ------------------------------------------------------------------------------------------------- Private Methods
064    
065        void assignWorkspace(Type type) {
066            types++;
067            if (isDeepaMehtaStandardType(type)) {
068                standardTypes++;
069                for (AssociationDefinition assocDef : type.getAssocDefs()) {
070                    assocDefs++;
071                    assignToDeepamehtaWorkspace(assocDef);
072                    for (Topic configTopic : assocDef.getViewConfig().getConfigTopics()) {
073                        configTopics++;
074                        assignToDeepamehtaWorkspace(configTopic);
075                    }
076                }
077            }
078        }
079    
080        void assignToDeepamehtaWorkspace(DeepaMehtaObject object) {
081            wsService.assignToWorkspace(object, deepamehtaWorkspaceId);
082        }
083    
084        // ### copy in WorkspacesPlugin.java
085        private Topic getDeepaMehtaWorkspace() {
086            return wsService.getWorkspace(WorkspacesService.DEEPAMEHTA_WORKSPACE_URI);
087        }
088    
089        // ### copy in WorkspacesPlugin.java
090        private boolean isDeepaMehtaStandardType(Type type) {
091            return type.getUri().startsWith("dm4.");
092        }
093    }