Source code: org/acs/damsel/client/cb/CbSchemaInfoAction.java
1 package org.acs.damsel.client.cb;
2
3 import java.sql.*;
4 import java.util.*;
5 import javax.servlet.http.*;
6
7 import org.acs.damsel.client.*;
8 import org.acs.damsel.srvr.*;
9 import org.acs.damsel.srvr.collection.*;
10 import org.acs.damsel.srvr.collection.Collection;
11 import org.acs.damsel.srvr.group.*;
12 import org.acs.damsel.srvr.schema.*;
13 import org.acs.damsel.srvr.user.*;
14 import org.apache.log4j.*;
15 import org.apache.struts.action.*;
16
17 public class CbSchemaInfoAction
18 extends Action {
19
20 private Logger log = Logger.getLogger(CbCollectionInfoAction.class);
21
22 public CbSchemaInfoAction() {
23 BasicConfigurator.resetConfiguration();
24 PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
25 }
26
27 public ActionForward execute(ActionMapping actionMapping,
28 ActionForm actionForm,
29 HttpServletRequest httpServletRequest,
30 HttpServletResponse httpServletResponse) {
31
32 CbSchemaInfoForm cbSchemaInfoForm = (CbSchemaInfoForm) actionForm;
33 String submitType = cbSchemaInfoForm.getSubmitType();
34 Vector schemaVector = cbSchemaInfoForm.getSchemaVector();
35 String allFields = cbSchemaInfoForm.getAllFields();
36 String fieldsInSchema = cbSchemaInfoForm.getFieldsInSchema();
37 String schemaSelect = cbSchemaInfoForm.getSchemaSelect();
38 String newField = cbSchemaInfoForm.getNewField();
39 String schemaName = cbSchemaInfoForm.getSchemaName();
40 ActionErrors errors;
41 SchemaMgr sMgr = ClientApp.instance().getSchemaMgr();
42 Vector schemaNames = sMgr.schemaNames();
43
44 CbCollectionInfoForm cbCollectionInfoForm = (CbCollectionInfoForm)
45 httpServletRequest.getSession().getAttribute("cbCollectionInfoForm");
46
47 CbGroupInfoForm cbGroupInfoForm = (CbGroupInfoForm)
48 httpServletRequest.getSession().getAttribute("cbGroupInfoForm");
49
50 User user = (User) httpServletRequest.getSession().getAttribute("User");
51
52 /*Check if collectionName is blank (if the user reached the confirmation
53 page and did a browser refresh) */
54 if (cbCollectionInfoForm.getCollectionName().trim().equals("")) {
55 return actionMapping.findForward("success");
56 }
57
58 /*Check if user has logged in */
59 if (user == null) {
60 return actionMapping.findForward("refresh");
61 }
62
63 // Throws an error if the user enters a schema name that already exists
64 if (schemaNames.contains(cbSchemaInfoForm.getSchemaName())) {
65 errors = new ActionErrors();
66 errors.add("cb", new ActionError("schema.name.already.exists"));
67 this.saveErrors(httpServletRequest, errors);
68 return actionMapping.findForward("refresh");
69 }
70
71 /*A schema has been selected...*/
72 if (submitType.equals("refresh")) {
73 cbSchemaInfoForm.setSchemaName("");
74 Schema schema = null;
75 try {
76 schema = ClientApp.instance().getSchemaMgr().getSchema(schemaSelect);
77 }
78 catch (SchemaMgrException ex3) {
79 log.error("Caught unexpected SchemaMgrException in CbSchemaInfoAction :" +
80 ex3.getMessage());
81 errors = new ActionErrors();
82 errors.add("cb", new ActionError("collection.builder.error.caught"));
83 this.saveErrors(httpServletRequest, errors);
84 return actionMapping.findForward("failure");
85 }
86 Vector tags = schema.getTags();
87 /*Convert tags to strings */
88 Vector convertedTags = new Vector();
89 for (int i = 0; i < tags.size(); i ++ )
90 convertedTags.add(((MetaDataTag) tags.elementAt(i)).getName());
91 cbSchemaInfoForm.setSchemaVector(convertedTags);
92 return actionMapping.findForward("refresh");
93 }
94
95 if (submitType.equals("New")) {
96 /*Clear out the vector if the user made a new schema */
97 if (!schemaName.trim().equals("")) {
98 schemaVector.clear();
99 cbSchemaInfoForm.setSchemaSelect("");
100 }
101 return actionMapping.findForward("refresh");
102 }
103
104 /*Add tag from "All Fields" */
105 if (submitType.equals("add")) {
106 if (!schemaVector.contains(allFields)) {
107 schemaVector.add(allFields);
108 }
109 return actionMapping.findForward("refresh");
110 }
111
112 /*Remove tag from "Fields in Schema" */
113 if (submitType.equals("remove")) {
114 schemaVector.remove(fieldsInSchema);
115 return actionMapping.findForward("refresh");
116 }
117
118 /*Add tag from "New Field" */
119 if (submitType.equals("newFieldAdd")) {
120 if (!schemaVector.contains(newField) && !newField.trim().equals("")) {
121 schemaVector.add(newField);
122 }
123 return actionMapping.findForward("refresh");
124 }
125
126 if (submitType.equals("next")) {
127
128
129 /*Check for no name and no schema selected */
130 if (schemaName.trim().equals("") && schemaSelect.trim().equals("")) {
131 errors = new ActionErrors();
132 errors.add("collectionbuilder",
133 new ActionError("no.schema.name.or.selection"));
134 this.saveErrors(httpServletRequest, errors);
135 return actionMapping.findForward("refresh");
136 }
137
138 /*Add the collection!! */
139 Collection collection = new Collection(cbCollectionInfoForm.
140 getCollectionName());
141 collection.setDescription(cbCollectionInfoForm.getDescription());
142 collection.setOwnerName(user.getUserName());
143 collection.setRepositoryName("DefaultRepository");
144
145 /*If collection is private */
146 if (cbCollectionInfoForm.getPrivateCollection().equals("true")) {
147 String perms = "rwdrwd---";
148 if (cbGroupInfoForm.getRead() == null) {
149 perms = perms.substring(0, 3) + "-" + perms.substring(4);
150 }
151 if (cbGroupInfoForm.getWrite() == null) {
152 perms = perms.substring(0, 4) + "-" + perms.substring(5);
153 }
154 if (cbGroupInfoForm.getDelete() == null) {
155 perms = perms.substring(0, 5) + "-" + perms.substring(6);
156 }
157
158 int permID = ClientApp.instance().getAuthMgr().getAllPermissions(perms);
159 collection.setPermissionID(Integer.toString(permID));
160
161 String grpName = cbGroupInfoForm.getNewGroupName();
162 GroupMgr gmgr = new GroupMgr();
163 Vector usrs = cbGroupInfoForm.getMoveList();
164 String userName = new String();
165
166 //if you created a new group, then add the group to the groups table
167 if (!cbGroupInfoForm.getNewGroupName().trim().equals("")) {
168 collection.setGroupName(cbGroupInfoForm.getNewGroupName());
169
170 Group grp = new Group();
171 String descrip = cbGroupInfoForm.getDescription();
172
173 grp.setDescription(descrip);
174 grp.setGroupName(grpName);
175 gmgr.addGroup(grp);
176 }
177 else {
178 collection.setGroupName(cbGroupInfoForm.getSelectGroup());
179
180 }
181 gmgr.deleteMultipleUsersFromGroup(grpName);
182 //adds the users back to the users groups table
183 for (int i = 0; i < usrs.size(); i++) {
184 userName = (String) usrs.elementAt(i);
185 try {
186 gmgr.addUserToGroup(userName, grpName);
187 }
188 catch (SQLException ex2) {
189 log.error("Caught unexpected SQLException in CbSchemaInfoAction :" +
190 ex2.getMessage());
191 errors = new ActionErrors();
192 errors.add("cb", new ActionError("collection.builder.error.caught"));
193 this.saveErrors(httpServletRequest, errors);
194 return actionMapping.findForward("failure");
195 }
196 }
197
198 }
199 else {
200 collection.setPermissionID("127");
201 collection.setGroupName("Administrators");
202 }
203
204 /*Using an existing schema */
205 if (schemaName.trim().equals("")) {
206 collection.setSchemaName(schemaSelect);
207 /*set schema name for future confirmation use */
208 cbSchemaInfoForm.setSchemaName(schemaSelect);
209
210 try {
211 //set collections AssetViewMask to be same as schema
212 Schema assetSchema = new Schema();
213 assetSchema = sMgr.getSchema(schemaSelect);
214 assetSchema.setName(collection.getCollectionName() + "AssetViewMask");
215 sMgr.addSchema(assetSchema);
216
217 //set collections ResultMask to only have FileName and WebFileName
218 Schema resultSchema = new Schema();
219 resultSchema.setName(collection.getCollectionName()+"ResultsMask");
220 resultSchema.setGroupName(assetSchema.getGroupName());
221 resultSchema.setOwnerName(assetSchema.getName());
222 resultSchema.setPermissionID(assetSchema.getPermissionID());
223 MetaDataTag fileTag = new MetaDataTag();
224 fileTag.setName("FileName");
225 resultSchema.addTag(fileTag);
226 sMgr.addSchema(resultSchema);
227 }
228 catch (Exception ex4) {
229 //catch SQLException, SchemaMgrException, or SchemaException
230 log.error("Unexpected exception ("+ex4.getClass()+") caught in CbSchemaInfoAction :"+ex4.getMessage());
231 errors = new ActionErrors();
232 errors.add("cb", new ActionError("collection.builder.error.caught"));
233 this.saveErrors(httpServletRequest, errors);
234 return actionMapping.findForward("failure");
235 }
236 }
237
238 /*Using a new schema */
239 else {
240 Schema schema = new Schema();
241 schema.setName(schemaName);
242 schema.setOwnerName(user.getUserName());
243 MetaDataTag tag;
244 Vector tags = new Vector();
245 String tagName = null;
246
247 /*Add tags to a vector */
248 for (int i = 0; i < schemaVector.size(); i++) {
249 tagName = schemaVector.elementAt(i).toString();
250 if (!tagName.equals("FileName") && !tagName.equals("WebFileName")) {
251 tag = new MetaDataTag();
252 tag.setName(tagName);
253 tags.add(tag);
254 }
255 }
256 schema.setTags(tags);
257 /*Add schema to DB */
258 try {
259 schema.setPermissionID(collection.getPermissionID());
260 schema.setGroupName(collection.getGroupName());
261 collection.setSchemaName(schema.getName());
262 ClientApp.instance().getSchemaMgr().addSchema(schema);
263
264 //add assetViewMask to schemaTable with same properties and tags as schema
265 schema.setName(collection.getCollectionName() + "AssetViewMask");
266 ClientApp.instance().getSchemaMgr().addSchema(schema);
267
268 //add resultsMask to schemaTable with save properties as schema, but
269 //only the FileName tag
270 Schema resultSchema = new Schema();
271 resultSchema.setGroupName(schema.getGroupName());
272 resultSchema.setName(collection.getCollectionName() + "ResultsMask");
273 resultSchema.setOwnerName(schema.getOwnerName());
274 resultSchema.setPermissionID(schema.getPermissionID());
275 MetaDataTag fileTag = new MetaDataTag();
276 fileTag.setName("FileName");
277 resultSchema.addTag(fileTag);
278 ClientApp.instance().getSchemaMgr().addSchema(resultSchema);
279 }
280 catch (SQLException ex) {
281 log.error("Caught unexpected SQLException in CbSchemaInfoAction :" +
282 ex.getMessage());
283 errors = new ActionErrors();
284 errors.add("cb", new ActionError("collection.builder.error.caught"));
285 this.saveErrors(httpServletRequest, errors);
286 return actionMapping.findForward("failure");
287 }
288 catch (SchemaException ex5) {
289 log.error("Caught unexpected SchemaException in CbSchemaInfoAction :" +
290 ex5.getMessage());
291 errors = new ActionErrors();
292 errors.add("cb", new ActionError("collection.builder.error.caught"));
293 this.saveErrors(httpServletRequest, errors);
294 return actionMapping.findForward("failure");
295 }
296 }
297
298 /*Add collection to DB */
299 try {
300 ClientApp.instance().getCollectionMgr().addCollection(collection);
301 }
302 catch (CollectionMgrException ex1) {
303 log.error(
304 "Caught unexpected CollectionMgrException in CbSchemaInfoAction :" +
305 ex1.getMessage());
306 errors = new ActionErrors();
307 errors.add("cb", new ActionError("collection.builder.error.caught"));
308 this.saveErrors(httpServletRequest, errors);
309 return actionMapping.findForward("failure");
310 }
311 return actionMapping.findForward("success");
312 }
313
314 /*Go back to previous page */
315 if (submitType.equals("back")) {
316 if (cbCollectionInfoForm.getPrivateCollection().equals("true")) {
317 return actionMapping.findForward("groupInfo");
318 }
319 return actionMapping.findForward("collectionInfo");
320 }
321
322 return actionMapping.findForward("refresh");
323 }
324 }