001package systems.dmx.workspaces.migrations;
002
003import systems.dmx.core.Association;
004import systems.dmx.core.Topic;
005import systems.dmx.core.service.Inject;
006import systems.dmx.core.service.Migration;
007import systems.dmx.facets.FacetsService;
008import systems.dmx.workspaces.WorkspacesService;
009
010import java.util.logging.Logger;
011
012
013
014/**
015 * Deletes the workspace assignments of the "Topic Mapcontext" and "Association Mapcontext" associations.
016 * Runs only in UPDATE mode.
017 * <p>
018 * Part of DM 4.8.3
019 */
020public class Migration9 extends Migration {
021
022    // ---------------------------------------------------------------------------------------------- Instance Variables
023
024    @Inject
025    private WorkspacesService workspacesService;
026
027    @Inject
028    private FacetsService facetsService;
029
030    private long topicContext = 0, assocContext = 0;
031
032    private Logger logger = Logger.getLogger(getClass().getName());
033
034    // -------------------------------------------------------------------------------------------------- Public Methods
035
036    @Override
037    public void run() {
038        logger.info("########## Deleting workspace assignments of \"Mapcontext\" associations");
039        //
040        for (Association assoc : dmx.getAssociationsByType("dmx.topicmaps.topic_mapcontext")) {
041            deleteWorkspaceAssignment(assoc);
042            topicContext++;
043        }
044        for (Association assoc : dmx.getAssociationsByType("dmx.topicmaps.association_mapcontext")) {
045            deleteWorkspaceAssignment(assoc);
046            assocContext++;
047        }
048        //
049        logger.info("########## Deleting workspace assignments of \"Mapcontext\" associations complete\n    " +
050            "\"Topic Mapcontext\" associations processed: " + topicContext + "\n    " +
051            "\"Association Mapcontext\" associations processed: " + assocContext);
052    }
053
054    // ------------------------------------------------------------------------------------------------- Private Methods
055
056    private void deleteWorkspaceAssignment(Association assoc) {
057        Topic workspace = workspacesService.getAssignedWorkspace(assoc.getId());
058        if (workspace != null) {
059            // 1) delete association
060            facetsService.updateFacet(assoc, "dmx.workspaces.workspace_facet",
061                mf.newFacetValueModel("dmx.workspaces.workspace").putDeletionRef(workspace.getId()));
062            //
063            // 2) delete property
064            assoc.removeProperty(WorkspacesService.PROP_WORKSPACE_ID);
065        }
066    }
067}