]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/migration/DocumentCleanupMigrationStep.java
Merge "Fetch all audit logging events"
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / migration / DocumentCleanupMigrationStep.java
1 package org.simantics.modeling.migration;
2
3 import org.eclipse.core.runtime.IProgressMonitor;
4 import org.simantics.db.Resource;
5 import org.simantics.db.Session;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.common.request.WriteRequest;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.migration.MigrationState;
10 import org.simantics.db.layer0.migration.MigrationStep;
11 import org.simantics.db.layer0.migration.MigrationUtils;
12 import org.simantics.db.layer0.util.RemoverUtil;
13 import org.simantics.document.DocumentResource;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.modeling.ModelingUtils;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class DocumentCleanupMigrationStep implements MigrationStep {
20
21     private static final Logger LOGGER = LoggerFactory.getLogger(DocumentCleanupMigrationStep.class);
22
23     @Override
24     public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
25         
26         final Resource indexRoot = MigrationUtils.getResource(monitor, session, state);
27         if(indexRoot == null) return;
28
29         session.sync(new WriteRequest() {
30                 
31             @Override
32             public void perform(WriteGraph graph) throws DatabaseException {
33                 
34                 Layer0 L0 = Layer0.getInstance(graph);
35                 DocumentResource DOC = DocumentResource.getInstance(graph);
36                 
37                 for(Resource model : graph.getObjects(indexRoot, L0.ConsistsOf)) {
38                                 for(Resource instance : ModelingUtils.searchByType(graph, model, DOC.ScenegraphDocument)) {
39                                         if(!graph.hasStatement(instance, DOC.HasDocumentation_Inverse)) {
40                                                 LOGGER.info("Removing stray document " + graph.getURI(instance));
41                                                 RemoverUtil.remove(graph, instance);
42                                         }
43                                 }
44                 }
45
46             }
47
48         });
49
50     }
51
52 }