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