1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.connector.store.jpa.util;
25
26 import java.util.UUID;
27 import javax.persistence.EntityManager;
28 import org.modeshape.common.util.CheckArg;
29 import org.modeshape.connector.store.jpa.Model;
30
31
32
33
34 public class StoreOptions {
35
36 public static final class Dna {
37 public static final String ROOT_NODE_UUID = "org.jboss.dna.store.rootNodeUuid";
38 public static final String VERSION = "org.jboss.dna.store.version";
39 public static final String MODEL = "org.jboss.dna.store.model";
40 }
41
42 public static final String ROOT_NODE_UUID = "org.modeshape.store.rootNodeUuid";
43 public static final String VERSION = "org.modeshape.store.version";
44 public static final String MODEL = "org.modeshape.store.model";
45
46 private final EntityManager entityManager;
47
48 public StoreOptions( EntityManager manager ) {
49 CheckArg.isNotNull(manager, "manager");
50 this.entityManager = manager;
51 }
52
53 public UUID getRootNodeUuid() {
54 String value = getOption(ROOT_NODE_UUID);
55 if (value == null) {
56
57 value = getOption(Dna.ROOT_NODE_UUID);
58 if (value != null) {
59
60 setOption(ROOT_NODE_UUID, value);
61 }
62 }
63 return value != null ? UUID.fromString(value) : null;
64 }
65
66 public void setRootNodeUuid( UUID uuid ) {
67 CheckArg.isNotNull(uuid, "uuid");
68 setOption(ROOT_NODE_UUID, uuid.toString());
69 }
70
71 public String getVersion() {
72 String value = getOption(VERSION);
73 if (value == null) {
74
75 value = getOption(Dna.VERSION);
76 if (value != null) {
77
78 setOption(VERSION, value);
79 }
80 }
81 return value;
82 }
83
84 public void setVersion( String version ) {
85 setOption(VERSION, version);
86 }
87
88 public String getModelName() {
89 String value = getOption(MODEL);
90 if (value == null) {
91
92 value = getOption(Dna.MODEL);
93 if (value != null) {
94
95 setOption(MODEL, value);
96 }
97 }
98 return value;
99 }
100
101 public void setModelName( Model model ) {
102 String modelName = model != null ? model.getName() : null;
103 setOption(MODEL, modelName);
104 }
105
106 public String getOption( String name ) {
107 StoreOptionEntity entity = entityManager.find(StoreOptionEntity.class, name);
108 return entity != null ? entity.getValue() : null;
109 }
110
111 public void setOption( String name,
112 String value ) {
113 CheckArg.isNotEmpty(name, "name");
114 if (value != null) value = value.trim();
115 StoreOptionEntity entity = entityManager.find(StoreOptionEntity.class, name);
116 if (entity == null) {
117 if (value != null) {
118
119 entity = new StoreOptionEntity(name, value);
120 entityManager.persist(entity);
121 }
122 } else {
123 if (value != null) {
124
125 entity.setValue(value);
126 } else {
127
128 entityManager.remove(entity);
129 }
130 }
131 }
132
133 public void removeOption( String name ) {
134 StoreOptionEntity entity = new StoreOptionEntity(name);
135 entityManager.remove(entity);
136 }
137 }