001    package org.deepamehta.plugins.wikidata.migrations;
002    
003    import de.deepamehta.core.AssociationType;
004    import de.deepamehta.core.Topic;
005    import de.deepamehta.core.TopicType;
006    import de.deepamehta.core.model.*;
007    import de.deepamehta.core.service.Migration;
008    import java.util.logging.Logger;
009    
010    
011    /*
012     * A very basic plugin for searching and turning wikidata-properties into association-types.
013     *
014     * @author Malte Rei├čig (<malte@mikromedia.de>)
015     * @website https://github.com/mukil/dm4-wikidata
016     */
017    
018    public class Migration6 extends Migration {
019    
020        private Logger log = Logger.getLogger(getClass().getName());
021    
022        private final static String WS_WIKIDATA_URI = "org.deepamehta.workspaces.wikidata";
023    
024        private final static String WD_SEARCH_ENTITY = "org.deepamehta.wikidata.search_entity";
025    
026        private final static String WD_CLAIM_EDGE_TYPE_URI = "org.deepamehta.wikidata.claim_edge";
027    
028        private final static String WD_TEXT_TYPE_URI = "org.deepamehta.wikidata.text";
029        private final static String WD_COMMONS_MEDIA_TYPE_URI = "org.deepamehta.wikidata.commons_media";
030        private final static String WD_GLOBE_COORDINATE_TYPE_URI = "org.deepamehta.wikidata.globe_coordinate";
031    
032        @Override
033        public void run() {
034    
035            // 1) Assign all new wikidata types to the \"Wikidata\"-Workspace
036            AssociationType claim = dms.getAssociationType(WD_CLAIM_EDGE_TYPE_URI);
037            assignWorkspace(claim);
038            TopicType coordinate = dms.getTopicType(WD_GLOBE_COORDINATE_TYPE_URI);
039            assignWorkspace(coordinate);
040            TopicType commons_media = dms.getTopicType(WD_COMMONS_MEDIA_TYPE_URI);
041            assignWorkspace(commons_media);
042            TopicType text = dms.getTopicType(WD_TEXT_TYPE_URI);
043            assignWorkspace(text);
044            log.info("1) Assigned \"Wikidata Text\", \"Wikidata Commons Media\", \"Wikidata Globe Coordinate\" "
045                    + "to \"Wikidata\"-Workspace");
046            // 2) Make \"Wikidata Search Entity\" (type=property) part of each \"Wikidata Claim\"-Edge
047            TopicType searchEntity = dms.getTopicType(WD_SEARCH_ENTITY);
048            claim.addAssocDef(new AssociationDefinitionModel("dm4.core.aggregation_def",
049                    claim.getUri(), searchEntity.getUri(), "dm4.core.one", "dm4.core.one"));
050            log.info("2) Assigned \"Wikidata Search Entity\" to \"Wikidata Claim\"");
051            // 3) Add "Ordinal Number" to "Search Entity"
052            TopicType ordinalNr = dms.getTopicType("org.deepamehta.wikidata.search_ordinal_nr");
053            searchEntity.addAssocDef(new AssociationDefinitionModel("dm4.core.composition_def",
054                    searchEntity.getUri(), ordinalNr.getUri(), "dm4.core.one", "dm4.core.one"));
055            log.info("3) Assigned \"Search Entity Ordinal Nr\" to \"Search Entity\"");
056    
057        }
058    
059        // === Workspace ===
060    
061        private void assignWorkspace(Topic topic) {
062            Topic defaultWorkspace = dms.getTopic("uri", new SimpleValue(WS_WIKIDATA_URI), false);
063            dms.createAssociation(new AssociationModel("dm4.core.aggregation",
064                new TopicRoleModel(topic.getId(), "dm4.core.parent"),
065                new TopicRoleModel(defaultWorkspace.getId(), "dm4.core.child")
066            ), null);
067        }
068    
069    }