]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/techtype/requests/WriteTechTypeTable.java
Add enable/disable feature for tech type tables
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / techtype / requests / WriteTechTypeTable.java
1 package org.simantics.district.network.techtype.requests;
2
3 import java.util.UUID;
4
5 import org.simantics.Simantics;
6 import org.simantics.db.Resource;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
9 import org.simantics.db.common.request.WriteRequest;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.request.PossibleActiveModel;
12 import org.simantics.db.layer0.util.Layer0Utils;
13 import org.simantics.district.network.ontology.DistrictNetworkResource;
14 import org.simantics.layer0.Layer0;
15
16 public final class WriteTechTypeTable extends WriteRequest {
17         private final String data;
18         private final Resource componentType;
19
20         public WriteTechTypeTable(Resource componentType, String data) {
21                 this.data = data;
22                 this.componentType = componentType;
23         }
24
25         @Override
26         public void perform(WriteGraph graph) throws DatabaseException {
27                 Layer0 L0 = Layer0.getInstance(graph);
28                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
29
30                 Resource model = graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
31                 if (model == null)
32                         throw new DatabaseException("No active model for storing tech type data");
33
34                 Resource ttt = graph.syncRequest(new PossibleTechTypeTable(model, componentType), TransientCacheListener.instance());
35
36                 graph.getSession().markUndoPoint();
37                 
38                 if (ttt == null) {
39                         ttt = graph.newResource();
40                         graph.claim(ttt, L0.InstanceOf, DN.TechType_TechTypeTable);
41                         graph.claimLiteral(ttt, L0.HasName, UUID.randomUUID().toString());
42                         graph.claim(model, L0.ConsistsOf, ttt);
43                         graph.claim(ttt, DN.TechType_TechTypeTable_HasComponentType, componentType);
44                         Layer0Utils.addCommentMetadata(graph, "Create tech type table");
45                 } else {
46                         graph.deny(ttt, DN.TechType_TechTypeTable_HasEnabledItems);
47                         Layer0Utils.addCommentMetadata(graph, "Update tech type table");
48                 }
49
50                 graph.claimLiteral(ttt, DN.TechType_TechTypeTable_HasData, data);
51         }
52 }