001package systems.dmx.files.migrations;
002
003import systems.dmx.core.Topic;
004import systems.dmx.core.model.SimpleValue;
005import systems.dmx.core.service.Migration;
006
007import java.util.List;
008import java.util.logging.Logger;
009
010
011
012/**
013 * Renames root Folder topics, in case of dmx.filerepo.per_workspace=true.
014 * Renames topic type "Disk Quota" -> "Disk Quota (MB)".
015 * Installs the file size renderer.
016 * <p>
017 * Runs only in UPDATE mode.
018 * <p>
019 * Part of DM 4.8
020 */
021public class Migration4 extends Migration {
022
023    // ------------------------------------------------------------------------------------------------------- Constants
024
025    private static final boolean FILE_REPOSITORY_PER_WORKSPACE = Boolean.getBoolean("dmx.filerepo.per_workspace");
026
027    // ---------------------------------------------------------------------------------------------- Instance Variables
028
029    private Logger logger = Logger.getLogger(getClass().getName());
030
031    // -------------------------------------------------------------------------------------------------- Public Methods
032
033    @Override
034    public void run() {
035        // 1) Rename root Folder topics
036        if (FILE_REPOSITORY_PER_WORKSPACE) {
037            List<Topic> workspaces = dmx.getTopicsByType("dmx.workspaces.workspace");
038            logger.info("########## Renaming root Folder topics of " + workspaces.size() + " possible workspaces");
039            int renamed = 0;
040            for (Topic workspace : workspaces) {
041                Topic folderTopic = fetchFolderTopic("/workspace-" + workspace.getId());
042                if (folderTopic != null) {
043                    folderTopic.getChildTopics().set("dmx.files.folder_name", workspace.getSimpleValue().toString());
044                    renamed++;
045                }
046            }
047            logger.info("########## Root Folder topics renamed: " + renamed);
048        } else {
049            logger.info("########## Renaming root Folder topics SKIPPED -- per-workspace file repositories are " +
050                "switched off");
051        }
052        //
053        // 2) Rename topic type "Disk Quota"
054        dmx.getTopicType("dmx.files.disk_quota").setSimpleValue("Disk Quota (MB)");
055        //
056        // 3) Install file size renderer
057        setTopicTypeViewConfigValue("dmx.files.size", "simple_renderer_uri", "dmx.files.file_size_renderer");
058    }
059
060    // ------------------------------------------------------------------------------------------------- Private Methods
061
062    /**
063     * Fetches the Folder topic representing the directory at the given repository path.
064     * If no such Folder topic exists <code>null</code> is returned.
065     */
066    private Topic fetchFolderTopic(String repoPath) {
067        Topic topic = dmx.getTopicByValue("dmx.files.path", new SimpleValue(repoPath));
068        return topic != null ? topic.getRelatedTopic("dmx.core.composition", "dmx.core.child", "dmx.core.parent",
069            "dmx.files.folder") : null;
070    }
071}