View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * A utility class that provides easy access to the options stored within a database.
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              // See if there is an option for an existing DNA uuid ...
57              value = getOption(Dna.ROOT_NODE_UUID);
58              if (value != null) {
59                  // There was, so set the ModeShape uuid ...
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              // See if there is an option for an existing DNA version ...
75              value = getOption(Dna.VERSION);
76              if (value != null) {
77                  // There was, so set the ModeShape version ...
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              // See if there is an option for an existing DNA version ...
92              value = getOption(Dna.MODEL);
93              if (value != null) {
94                  // There was, so set the ModeShape version ...
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                 // There is no existing entity, but there is a valid value ...
119                 entity = new StoreOptionEntity(name, value);
120                 entityManager.persist(entity);
121             }
122         } else {
123             if (value != null) {
124                 // Set value on the entity ...
125                 entity.setValue(value);
126             } else {
127                 // The existing entity is to be removed ...
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 }