]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/RelatedEntityNameModifier.java
Prevent paste to resources that are `L0.Entity_published`
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / RelatedEntityNameModifier.java
1 /*******************************************************************************
2  * Copyright (c) 2014 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.adapter.StringModifier;
20 import org.simantics.layer0.Layer0;
21
22 /**
23  * A modifier that enforces all the same naming validations as
24  * {@link EntityNameModifier} but also makes sure that entities directly related
25  * to the specified entity with the specified relation are renamed at the same
26  * time.
27  * 
28  * @author Tuukka Lehtonen
29  * @see EntityNameModifier
30  */
31 public final class RelatedEntityNameModifier implements StringModifier {
32
33         private EntityNameModifier entityModifier;
34         private Resource entity;
35         private Resource relation;
36
37         public RelatedEntityNameModifier(ReadGraph graph, Resource entity, Resource property, Resource relation) throws DatabaseException {
38                 this.entityModifier = new EntityNameModifier(graph, entity, property);
39                 this.entity = entity;
40                 this.relation = relation;
41         }
42
43         @Override
44         public String getValue() {
45                 return entityModifier.getValue();
46         }
47
48         @Override
49         public String isValid(String value) {
50                 return entityModifier.isValid(value);
51         }
52
53         @Override
54         final public void modify(WriteGraph graph, String value) throws DatabaseException {
55                 entityModifier.modify(graph, value);
56
57                 Layer0 L0 = Layer0.getInstance(graph);
58                 String v = entityModifier.finalValue(value); 
59                 for (Resource related : graph.getObjects(entity, relation))
60                         graph.claimLiteral(related, L0.HasName, L0.NameOf, v, Bindings.STRING);
61         }
62
63 }