Source code: org/acs/damsel/client/edit/EditSchemaAction.java
1 package org.acs.damsel.client.edit;
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.user.*;
11 import org.acs.damsel.srvr.auth.*;
12 import org.acs.damsel.srvr.*;
13 import org.acs.damsel.client.ClientApp;
14
15 public class EditSchemaAction
16 extends Action {
17
18 private Logger log = Logger.getLogger(EditSchemaAction.class);
19
20 public EditSchemaAction() {
21 BasicConfigurator.resetConfiguration();
22 PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
23 }
24
25 public ActionForward execute(ActionMapping actionMapping,
26 ActionForm actionForm,
27 HttpServletRequest httpServletRequest,
28 HttpServletResponse httpServletResponse) {
29 EditSchemaForm editSchemaForm = (EditSchemaForm) actionForm;
30
31 SchemaMgr sMgr = ClientApp.instance().getSchemaMgr();
32 ActionErrors errors;
33 String potentialName;
34 String value;
35 String name;
36
37 // If a schema has been selected the page is refreshed to display the
38 // corresponding table of metadata tags followed by checkboxes for the
39 // asset view and the results view of an asset in the collection.
40
41 if (editSchemaForm.isChangedSchema()) {
42 editSchemaForm.setChangedSchema(false);
43 return actionMapping.findForward("reset");
44 }
45
46 //if add button was clicked the new schema tag is added to the SchemasTagsTable
47 if (editSchemaForm.isAddField()) {
48
49 try {
50 editSchemaForm.setAddField(false);
51 String tag = editSchemaForm.getNewTag();
52 MetaDataTag mdt = new MetaDataTag();
53 mdt.setName(tag);
54 int num = sMgr.addSchemaTag(mdt, editSchemaForm.getSelectSchema());
55 editSchemaForm.setNewTag("");
56 if (num == 0) {
57 errors = new ActionErrors();
58 errors.add("editSchema", new ActionError("editSchema.tag.already.there"));
59 this.saveErrors(httpServletRequest, errors);
60 }
61 return actionMapping.findForward("reset");
62 }
63 catch (SQLException ex) {
64 log.error("Unexpected SQLException caught in EditSchemaAction :"+ex.getMessage());
65 errors = new ActionErrors();
66 errors.add("editSchema", new ActionError("editSchema.tag.invalid"));
67 this.saveErrors(httpServletRequest, errors);
68 return actionMapping.findForward("reset");
69 }
70 }
71
72 //if delete button is pressed, the chosen MetaDataTag(s) are deleted from
73 //the SchemasTagsTable
74
75 if (editSchemaForm.isDeleteButton()) {
76 editSchemaForm.setDeleteButton(false);
77 for (Enumeration e = httpServletRequest.getParameterNames();
78 e.hasMoreElements(); ) {
79 potentialName = (String) e.nextElement();
80 if (potentialName.indexOf("delete") != -1) {
81 name = potentialName.substring(potentialName.indexOf("delete") + 6);
82 value = httpServletRequest.getParameter(potentialName);
83 if (value.equals("true")) {
84 MetaDataTag tag = new MetaDataTag();
85 tag.setName(name);
86 try {
87 sMgr.deleteSchemaTag(tag, editSchemaForm.getSelectSchema());
88 }
89 catch (SQLException ex1) {
90 log.error("Unexpected SQLException caught in EditSchemaAction :"+ex1.getMessage());
91 errors = new ActionErrors();
92 errors.add("editSchema", new ActionError("editSchema.tag.invalid"));
93 this.saveErrors(httpServletRequest, errors);
94 return actionMapping.findForward("reset");
95 }
96 }
97 }
98 }
99
100 return actionMapping.findForward("reset");
101 }
102
103 //if saveAll button was pressed the new permissionID and groupName are added to the
104 //SchemaTable.
105
106 // Access the vector of the 9 permissions which was initialized in the
107 // addCollection.jsp page.
108 Vector allPerms = (Vector) httpServletRequest.getSession().getAttribute(
109 "allSchemaPermissions");
110
111 // Build a vector of containing 9 "false" strings to correspond to the
112 // 9 permissions in the allPerms vector.
113 Vector allFalse = new Vector();
114 for (int i = 0; i < allPerms.size(); i++) {
115 allFalse.add("false");
116 }
117
118 for (Enumeration e = httpServletRequest.getParameterNames();
119 e.hasMoreElements(); ) {
120 potentialName = (String) e.nextElement();
121
122 //goes through the vector allPerms until it finds the name of the
123 //checkbox in the vector. Once it finds the name it sets the value
124 //to true. All permissions that were not selected remain false.
125 //If the name of the checkbox is not found in the list of permissions
126 //the checkbox will not be counted as checked.
127 int indexOfPerm = 0;
128 boolean found = false;
129 while (!found && indexOfPerm < allPerms.size()) {
130 if (allPerms.elementAt(indexOfPerm).equals(potentialName)) {
131 found = true;
132 allFalse.setElementAt("true", indexOfPerm);
133 }
134 else {
135 indexOfPerm++;
136 }
137 }
138 //reset found and index so that the next checked permission can be found
139 //by searching through the vector of allPerms
140 found = false;
141 indexOfPerm = 0;
142 }
143 AuthMgr amgr = new AuthMgr();
144
145 //get the permissionID that matches the true and false permisssions sent
146 //to getAllPermissions
147 int intPerm = amgr.getAllPermissions(allPerms, allFalse);
148 String perm = Integer.toString(intPerm);
149
150
151 //new schema with all new values to be updated in database.
152 try {
153 Schema oldSchema = sMgr.getSchema(editSchemaForm.getSelectSchema());
154 Vector allTags = oldSchema.getTags();
155 Schema newSchema = new Schema();
156 User user = (User) httpServletRequest.getSession().getAttribute("User");
157 newSchema.setName(editSchemaForm.getSelectSchema());
158 newSchema.setGroupName(editSchemaForm.getSelectGroup());
159 newSchema.setPermissionID(perm);
160 httpServletRequest.getSession().setAttribute("editSchemaPermissionID",
161 perm);
162 newSchema.setOwnerName(user.getUserName());
163 //set all tags
164 for (Iterator i = allTags.iterator(); i.hasNext(); ) {
165 MetaDataTag tag = new MetaDataTag();
166 tag.setName(((MetaDataTag)i.next()).getName());
167 newSchema.addTag(tag);
168 }
169 sMgr.updateSchema(newSchema);
170 return actionMapping.findForward("success");
171 }
172 catch (SchemaException ex2) {
173 log.error("Unexpected SchemaException caught in EditSchemaAction :"+ex2.getMessage());
174 errors = new ActionErrors();
175 errors.add("editSchema", new ActionError("editSchema.error.caught"));
176 this.saveErrors(httpServletRequest, errors);
177 return actionMapping.findForward("reset");
178 }
179 catch (SchemaMgrException ex3) {
180 log.error("Unexpected SchemaMgrException caught in EditSchemaAction :"+ex3.getMessage());
181 errors = new ActionErrors();
182 errors.add("editSchema", new ActionError("editSchema.error.caught"));
183 this.saveErrors(httpServletRequest, errors);
184 return actionMapping.findForward("reset");
185 }
186 }
187 }