Source code: org/acs/damsel/srvr/schema/SchemaMgr.java
1 package org.acs.damsel.srvr.schema;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.*;
6
7 import org.acs.damsel.srvr.*;
8 import org.acs.damsel.srvr.db.*;
9 import org.acs.damsel.srvr.user.*;
10 import org.apache.log4j.*;
11
12 /**
13 * <p>Title: SchemaMgr</p>
14 * <p>Description: The SchemaMgr class provides access to metadata Schemas,
15 * which in turn contain MetaDataTags and their associated information
16 * (i.e., controlled vocabulary lists). Aside from adding, removing, and
17 * getting handles on Schemas, SchemaMgr also provides easy access to a Vector
18 * of Strings containing the names of the available Schemas (i.e., for display
19 * purposes). SchemaMgr (as well as Schema and MetaDataTag) supports
20 * serialization and the current state of all Schemas can easily be stored and
21 * loaded using the save() and newSchemaMgrFromFile() methods. Note that
22 * newSchemaMgrFromFile should be used instead of the constructor, unless the
23 * schema file does not exist yet.</p>
24 * @version 1.0
25 */
26 public class SchemaMgr implements Serializable {
27
28 private static Logger log = Logger.getLogger(SchemaMgr.class);
29 private AssetDB assetDB = null;
30
31 /**
32 * Default constructor. Initializes an empty schema list.
33 */
34 public SchemaMgr() {
35 BasicConfigurator.resetConfiguration();
36 PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
37 try {
38 assetDB = AssetDB.instance();
39 }
40 catch (SQLException ex) {
41 log.warn("Unexpected SQLException caught in SchemaMgr");
42 }
43 }
44
45 /**
46 * Method adds a Schema to the schema list.
47 * @param sch Schema to be added
48 * @return int, number of rows affected in SchemaTagsTable (1 if successful,
49 * 0 otherwise).
50 * @throws SQLException
51 */
52 public int addSchema(Schema sch) throws SQLException {
53 log.debug("enter addSchema");
54 return assetDB.addSchema(sch);
55 }
56
57 /**Obtain all the metadata tags used in the schematagstable
58 * @return Vector of Strings: all unique metadata tags */
59 public Vector getAllMetadataTags() {
60 try {
61 return AssetDB.instance().getAllMetadataTags();
62 }
63 catch (SQLException ex) {
64 log.warn("Unexpected SQLException caught in getAllMetadataTags " + ex.getMessage());
65 return null;
66 }
67 }
68
69 /**
70 * Method adds a metadata tag to the schema specified by the schema name
71 * String.
72 * @param tag MetaDataTag to be added
73 * @param schemaName String referencing schema to be added to
74 * @return int, number of rows affected in the SchemaTagsTable (1 if
75 * successful, 0 otherwise).
76 * @throws SQLException
77 */
78 public int addSchemaTag(MetaDataTag tag, String schemaName) throws
79 SQLException {
80 return assetDB.addSchemaTag(tag, schemaName);
81 }
82
83 /**
84 * Method deletes the metadata tag from the schema specified by the schema name.
85 * @param tag MetaDataTag to be deleted
86 * @param schemaName String referencing schema to delete from
87 * @return int, number of rows affect in SchemaTagsTable (1 if successful, 0
88 * otherwise).
89 * @throws SQLException
90 */
91 public int deleteSchemaTag(MetaDataTag tag, String schemaName) throws SQLException{
92 return assetDB.deleteSchemaTag(tag, schemaName);
93 }
94
95 /**
96 * Method removes the Schema with the given name from the schema list.
97 * A SchemaMgrException is thrown if no such Schema is in the schema list.
98 * @param schName String containing the schema name
99 * @throws SchemaMgrException
100 */
101 public void deleteSchema(String schName) throws SchemaMgrException{
102 log.debug("enter removeSchema");
103 try {
104 Schema sch = AssetDB.instance().getSchema(schName);
105 if (sch != null)
106 assetDB.deleteSchema(sch);
107 else {
108 throw new SchemaMgrException("Schema " + schName + " does not exist");
109 }
110 }
111 catch (SchemaException ex) {
112 log.error(ex);
113 throw new SchemaMgrException("Unable to remove schema: " + schName);
114 }
115 catch (SQLException ex) {
116 log.error(ex);
117 throw new SchemaMgrException("Unable to remove schema: " + schName);
118 }
119 log.debug("exit removeTag");
120 }
121
122 /**
123 * Method returns the Schema with the given name. A SchemaMgrException is
124 * thrown if no such Schema is in the schema list.
125 * @param schName String containing the schema name
126 * @return Schema corresponding to the specified schema name
127 * @throws SchemaMgrException
128 */
129 public Schema getSchema(String schName) throws SchemaMgrException {
130 log.debug("enter getSchema");
131 try {
132 Schema sch = assetDB.getSchema(schName);
133 if (sch == null) {
134 log.error("Schema requested does not exist");
135 throw new SchemaMgrException("No schema exists for schema " + schName);
136 }
137 return sch;
138 }
139 catch (SchemaException ex) {
140 log.error(ex);
141 throw new SchemaMgrException("Unable to find schema: " + schName);
142 }
143 catch (SQLException ex) {
144 log.error(ex);
145 throw new SchemaMgrException("Unable to find schema: " + schName);
146 }
147 }
148
149 /**
150 * Method returns the schema corresponding to the specified collection.
151 * @param collName the collection containing the desired schema
152 * @return Schema, the schema of the specified collection
153 * @throws SchemaMgrException
154 */
155 public Schema getSchemaOfCollection(String collName) throws SchemaMgrException{
156 log.debug("enter getSchemaOfCollection");
157 Schema sch = new Schema();
158 try {
159 sch = assetDB.getSchemaOfCollection(collName);
160 }
161 catch (SchemaException ex) {
162 log.error(ex);
163 throw new SchemaMgrException("Unable to get schema for collection: " + collName);
164 }
165 catch (SQLException ex) {
166 log.error(ex);
167 throw new SchemaMgrException("Unable to get schema for collection: " + collName);
168 }
169 log.debug("exit newSchemaMgrFromFile");
170 return sch;
171 }
172
173 /**
174 * Method returns a vector of strings containing all of the collections
175 * with schemas called schemaName.
176 * @param schemaName String containing the schema name.
177 * @return Vector, vector of collection names with specified schema.
178 * @throws SQLException
179 */
180 public Vector getCollectionsWithSchema(String schemaName) throws SQLException{
181 return assetDB.getCollectionsWithSchema(schemaName);
182 }
183
184 /**
185 * Returns list of schema names found in database.
186 * @return Vector, vector of strings containing all of the schemas found in the
187 * SchemaTagsTable in the database.
188 */
189 public Vector schemaNames() {
190 try {
191 return AssetDB.instance().getSchemaNames();
192 }
193 catch (SQLException ex) {
194 log.warn("Caught unexpected SQLException in schemaNames");
195 ex.printStackTrace();
196 return null;
197 }
198 }
199
200 /**
201 * Returns a vector of strings containing the names of the schemas that can
202 * be edited by the user.
203 * @param user User that can edit schemas
204 * @return Vector, vector of strings of schema names.
205 */
206 public Vector getEditableSchemas(User user){
207 try {
208 return DBUtils.instance().getEditableSchemas(user);
209 }
210 catch (SQLException ex) {
211 log.warn("Caught unexpected SQLException in SchemaMgr.getEditableSchemas - "+ex.getSQLState());
212 return null;
213 }
214 }
215
216 /**
217 * Updates the entry for the specified schema in the SchemaTable of the
218 * database.
219 * @param s Schema to be updated
220 * @return int, number of rows affected by update
221 */
222 public int updateSchema(Schema s){
223 try {
224 return AssetDB.instance().updateSchema(s);
225 }
226 catch (SQLException ex) {
227 log.warn("Caught unexpected SQLException in SchemaMgr.updateSchema - "+ex.getSQLState());
228 return 0;
229 }
230 }
231 }