Source code: org/acs/damsel/client/add/AddCollectionAction.java
1 package org.acs.damsel.client.add;
2
3 import org.apache.struts.action.*;
4 import org.apache.log4j.*;
5 import javax.servlet.http.*;
6 import java.util.*;
7 import org.acs.damsel.srvr.db.*;
8 import org.acs.damsel.srvr.schema.*;
9 import java.sql.*;
10 import org.acs.damsel.srvr.collection.*;
11 import org.acs.damsel.srvr.collection.Collection;
12 import org.acs.damsel.srvr.user.*;
13 import org.acs.damsel.srvr.auth.*;
14 import org.acs.damsel.srvr.*;
15 import org.acs.damsel.client.ClientApp;
16
17 public class AddCollectionAction
18 extends Action {
19 private Logger log = Logger.getLogger(AddCollectionAction.class);
20
21 public AddCollectionAction() {
22 BasicConfigurator.resetConfiguration();
23 PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
24 }
25
26 public ActionForward execute(ActionMapping actionMapping,
27 ActionForm actionForm,
28 HttpServletRequest httpServletRequest,
29 HttpServletResponse httpServletResponse) {
30
31 AddCollectionForm addCollectionForm = (AddCollectionForm) actionForm;
32 addCollectionForm.setCollectionName(addCollectionForm.getCollectionName());
33
34 try {
35 // Access the vector of the 9 permissions which was initialized in the
36 // addCollection.jsp page.
37 Vector allPerms = (Vector) httpServletRequest.getSession().getAttribute(
38 "allCollectionPermissions");
39
40 // Build a vector of containing 9 "false" strings to correspond to the
41 // 9 permissions in the allPerms vector.
42 Vector allFalse = new Vector();
43 for (int i = 0; i < allPerms.size(); i++) {
44 allFalse.add("false");
45 }
46
47 AssetDB assetDB = AssetDB.instance();
48 String potentialName;
49 MetaDataTag name = new MetaDataTag();
50 String value = new String();
51 Schema resultsSchema = new Schema();
52 Schema viewSchema = new Schema();
53 Vector fieldVec = new Vector();
54 CollectionMgr cmgr = new CollectionMgr();
55 Vector valueVec = new Vector();
56 boolean found = false;
57 int indexOfPerm = 0;
58
59 // If a schema has been selected the page is refreshed to display the
60 // corresponding table of metadata tags followed by checkboxes for the
61 // asset view and the results view of an asset in the collection.
62 if (addCollectionForm.isChangedSchema()) {
63 addCollectionForm.setChangedSchema(false);
64 return actionMapping.findForward("reset");
65 }
66 // If no collection name is specified an error is outputted to the screen
67 // for the user to see, and the collection is not added.
68 if (addCollectionForm.getCollectionName().equals("")) {
69 ActionErrors errors = new ActionErrors();
70 errors.add("collection", new ActionError("collection.noname"));
71 this.saveErrors(httpServletRequest, errors);
72 return actionMapping.findForward("reset");
73
74 }
75
76 // The names of the resultsMask and assetViewMask are set according the
77 // collection name specified by the user.
78 resultsSchema.setName(addCollectionForm.getCollectionName() +
79 "ResultsMask");
80 viewSchema.setName(addCollectionForm.getCollectionName() +
81 "AssetViewMask");
82
83 //goes through the checkboxes to see if they are results or view checkboxes
84 //and then adds the checked metadata tags to the proper schema
85 for (Enumeration e = httpServletRequest.getParameterNames();
86 e.hasMoreElements(); ) {
87 potentialName = (String) e.nextElement();
88 if (potentialName.indexOf("ResultsMask") != -1) {
89 name = new MetaDataTag();
90 name.setName(potentialName.substring(potentialName.indexOf(
91 "ResultsMask") + 11));
92 value = httpServletRequest.getParameter(potentialName);
93 if (value.equals("true")) {
94 resultsSchema.addTag(name);
95 }
96 }
97 else if (potentialName.indexOf("AssetViewMask") != -1) {
98 name = new MetaDataTag();
99 name.setName(potentialName.substring(potentialName.indexOf(
100 "AssetViewMask") + 13));
101 value = httpServletRequest.getParameter(potentialName);
102 if (value.equals("true")) {
103 viewSchema.addTag(name);
104 }
105 }
106
107 //finds checked checkboxes dealing with the delete, write, read permissions
108 else if (potentialName.indexOf("Delete") != -1 ||
109 potentialName.indexOf("Write") != -1 ||
110 potentialName.indexOf("Read") != -1) {
111 value = httpServletRequest.getParameter(potentialName);
112 if (value.equals("true")) {
113
114 //goes through the vector allPerms until it finds the name of the
115 //checkbox in the vector. Once it finds the name it sets the value
116 //to true. All permissions that were not selected remain false.
117 //If the name of the checkbox is not found in the list of permissions
118 //the checkbox will not be counted as checked.
119 while (!found && indexOfPerm < allPerms.size()) {
120 if (allPerms.elementAt(indexOfPerm).equals(potentialName)) {
121 found = true;
122 allFalse.setElementAt("true", indexOfPerm);
123 }
124 else indexOfPerm++;
125 }
126 //reset found and index so that the next checked permission can be found
127 //by searching through the vector of allPerms
128 found = false;
129 indexOfPerm = 0;
130 }
131 }
132 }
133 AuthMgr amgr = new AuthMgr();
134
135 //get the permissionID that matches the true and false permisssions sent
136 //to getAllPermissions
137 int intPerm = amgr.getAllPermissions(allPerms, allFalse);
138 String perm = Integer.toString(intPerm);
139 addCollectionForm.setSelectPermission(perm);
140
141
142 // Set the Schemas to have the right owner name and the same permissions
143 // as the base schema.
144
145 try {
146 User user = (User) httpServletRequest.getSession().getAttribute("User");
147 resultsSchema.setOwnerName(user.getUserName());
148 viewSchema.setOwnerName(user.getUserName());
149
150 SchemaMgr smgr = new SchemaMgr();
151 Schema s = smgr.getSchema(addCollectionForm.getSelectSchema());
152 resultsSchema.setPermissionID(s.getPermissionID());
153 viewSchema.setPermissionID(s.getPermissionID());
154
155 resultsSchema.setGroupName(addCollectionForm.getSelectGroup());
156 viewSchema.setGroupName(addCollectionForm.getSelectGroup());
157 }
158 catch (SchemaMgrException ex1) {
159 log.error("Unexpected SchemaMgrException caught in AddCollectionAction.");
160 ex1.printStackTrace();
161 }
162
163
164 //adds the results and view schemas to the database
165 assetDB.addSchema(resultsSchema);
166 assetDB.addSchema(viewSchema);
167 Collection myCol = new Collection(addCollectionForm.getCollectionName());
168 myCol.setOwnerName( ( (User) httpServletRequest.getSession().getAttribute(
169 "User")).getUserName());
170 myCol.setDescription(addCollectionForm.getDescription());
171 myCol.setSchemaName(addCollectionForm.getSelectSchema());
172 myCol.setRepositoryName("DefaultRepository");
173 myCol.setPermissionID(perm);
174 myCol.setGroupName(addCollectionForm.getSelectGroup());
175 assetDB.addCollection(myCol);
176 }
177 catch (SchemaException ex) {
178 log.warn("Threw a schema exception");
179 return actionMapping.findForward("reset");
180 }
181 catch (SQLException ex) {
182 log.warn("Threw a sql exception " + ex.getMessage());
183 ActionErrors errors = new ActionErrors();
184 errors.add("collection", new ActionError("collection.exists"));
185 this.saveErrors(httpServletRequest, errors);
186 return actionMapping.findForward("reset");
187 }
188
189
190 addCollectionForm.reset(actionMapping, httpServletRequest);
191 return actionMapping.findForward("success");
192 }
193 }