001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.console.web.admin.audit;
009:
010: //base classes
011: import java.io.IOException;
012: import java.math.BigDecimal;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.StringTokenizer;
016: import javax.servlet.ServletException;
017:
018: //project specific classes
019: import org.jfolder.common.utils.misc.MiscHelper;
020: import org.jfolder.common.utils.web.ParameterSet;
021: import org.jfolder.common.web.template.ConsoleParameterContext;
022: import org.jfolder.common.web.template.SubmitActionContext;
023: import org.jfolder.common.web.template.SubmitActionParameters;
024: import org.jfolder.console.base.BaseConsoleServlet;
025: import org.jfolder.console.base.ConsolePageContext;
026: import org.jfolder.console.base.ConsolePageMessageHolder;
027: import org.jfolder.console.base.ConsolePageSession;
028: import org.jfolder.platforms.stores.base.SystemStore;
029: import org.jfolder.security.audit.SecurityAudit;
030: import org.jfolder.security.audit.SecurityAuditHelper;
031: import org.jfolder.security.audit.SimpleSecurityAudit;
032: import org.jfolder.security.model.UserHolder;
033: import org.jfolder.services.console.ConsoleService;
034: import org.jfolder.services.console.ConsoleServiceCallerFactory;
035:
036: //other classes
037:
038: public class AuditCreateServlet extends BaseConsoleServlet {
039:
040: public AuditCreateServlet() {
041: }
042:
043: protected boolean checkAccess(ConsolePageSession inCps)
044: throws IOException, ServletException {
045:
046: boolean outValue = false;
047:
048: outValue = (inCps.isAdministrator());
049:
050: return outValue;
051: }
052:
053: public ConsolePageContext process(ConsolePageSession inCps,
054: UserHolder inUser, ParameterSet inPs,
055: SubmitActionContext inSac) throws IOException,
056: ServletException {
057:
058: //
059: ConsolePageContext outValue = null;
060:
061: if (inSac.isAction(SubmitActionParameters.OPEN)) {
062: }
063: //else if (inSac.isAction(SubmitActionParameters.DO_NOTHING)) {
064: //}
065: else if (inSac.isAction(SubmitActionParameters.EXECUTE)) {
066: //
067: //
068: //
069: String firstParam = inSac
070: .getParameter(ConsoleParameterContext.FIRST_INPUT);
071: MiscHelper.println("AuQuServ Param01 = " + firstParam);
072: //
073: String secondParam = inSac
074: .getParameter(ConsoleParameterContext.SECOND_INPUT);
075: MiscHelper.println("AuQuServ Param02 = " + secondParam);
076: //
077: String thirdParam = inSac
078: .getParameter(ConsoleParameterContext.THIRD_INPUT);
079: MiscHelper.println("AuQuServ Param03 = " + thirdParam);
080: //
081: String fourthParam = inSac
082: .getParameter(ConsoleParameterContext.FOURTH_INPUT);
083: MiscHelper.println("AuQuServ Param04 = " + fourthParam);
084: //
085: String fifthParam = inSac
086: .getParameter(ConsoleParameterContext.FIFTH_INPUT);
087: MiscHelper.println("AuQuServ Param05 = " + fifthParam);
088: //
089: String sixthParam = inSac
090: .getParameter(ConsoleParameterContext.SIXTH_INPUT);
091: MiscHelper.println("AuQuServ Param06 = " + sixthParam);
092: //
093: String seventhParam = inSac
094: .getParameter(ConsoleParameterContext.SEVENTH_INPUT);
095: MiscHelper.println("AuQuServ Param07 = " + seventhParam);
096: //
097: String eighthParam = inSac
098: .getParameter(ConsoleParameterContext.EIGHTH_INPUT);
099: MiscHelper.println("AuQuServ Param08 = " + eighthParam);
100: //
101: String ninthParam = inSac
102: .getParameter(ConsoleParameterContext.NINTH_INPUT);
103: MiscHelper.println("AuQuServ Param09 = " + ninthParam);
104: //
105: //String tenthParam = inSac.getParameter(inSac.TENTH_INPUT);
106: //MiscHelper.println("AuQuServ Param10 = " + tenthParam);
107:
108: ArrayList errors = new ArrayList();
109:
110: //
111: //
112: //
113: boolean currentStatus = (new Boolean(inPs
114: .getParameter(firstParam))).booleanValue();
115: String sequenceCodeText = inPs.getParameter(secondParam);
116: BigDecimal sequenceCode = null;
117: if (MiscHelper.isStringNonNegativeNumber(sequenceCodeText
118: .trim())) {
119: sequenceCode = new BigDecimal(sequenceCodeText);
120: } else {
121: errors.add("Sequence Code '" + sequenceCodeText
122: + "' is not a non-negative whole number");
123: }
124: String severityLevel = inPs.getParameter(thirdParam);
125: HashMap categoryTypes = new HashMap();
126: processMulti(categoryTypes, errors, "Category Type",
127: fourthParam, inPs);
128: HashMap sourceTypes = new HashMap();
129: processMulti(sourceTypes, errors, "Source Type",
130: fifthParam, inPs);
131: String customCodeText = inPs.getParameter(sixthParam);
132: BigDecimal customCode = null;
133: if (MiscHelper.isStringNonNegativeNumber(customCodeText
134: .trim())) {
135: customCode = new BigDecimal(customCodeText);
136: } else {
137: errors.add("Custom Code '" + customCodeText
138: + "' is not a non-negative whole number");
139: }
140: HashMap customCategoryTypes = new HashMap();
141: processMulti(customCategoryTypes, errors,
142: "Custom Category Type", seventhParam, inPs);
143: HashMap customSourceTypes = new HashMap();
144: processMulti(customSourceTypes, errors,
145: "Custom Source Type", eighthParam, inPs);
146: String customMessage = inPs.getParameter(ninthParam);
147: //
148: MiscHelper.println("AuQuServ currentStatus = "
149: + currentStatus);
150: MiscHelper.println("AuQuServ sequenceCode = "
151: + sequenceCode);
152: MiscHelper.println("AuQuServ severityLevel = "
153: + severityLevel);
154: MiscHelper.println("AuQuServ categoryTypes = "
155: + categoryTypes);
156: MiscHelper.println("AuQuServ sourceTypes = " + sourceTypes);
157: MiscHelper.println("AuQuServ customCode = " + customCode);
158: MiscHelper.println("AuQuServ customCategoryTypes = "
159: + customCategoryTypes);
160: MiscHelper.println("AuQuServ customSourceTypes = "
161: + customSourceTypes);
162: MiscHelper.println("AuQuServ customMessage = "
163: + customMessage);
164: //
165: if (errors.size() == 0) {
166: long utcTime = MiscHelper.currentUTC();
167: SecurityAudit sa = SimpleSecurityAudit
168: .newInstance(inUser.getUserIdentity(),
169: sequenceCode, SystemStore
170: .getMachineOfOrigin(),
171: MiscHelper.fromTimeToString(utcTime),
172: severityLevel, categoryTypes,
173: sourceTypes,
174: SystemStore.CURRENT_VERSION.toString(),
175: customCode, customCategoryTypes,
176: customSourceTypes, customMessage,
177: currentStatus);
178: ConsoleService cs = ConsoleServiceCallerFactory
179: .getConsoleService();
180: cs.saveAudit(sa);
181: } else {
182: ConsolePageMessageHolder cpmh = inCps
183: .getMessageHolder();
184: cpmh.setErrorHeader("One or more entries is incorrect");
185: for (int i = 0; i < errors.size(); i++) {
186: ConsolePageMessageHolder subCpmh = cpmh
187: .createSubMessage();
188: subCpmh.setErrorHeader(errors.get(i).toString());
189: }
190: }
191: } else {
192: inSac.unknownAction();
193: }
194:
195: outValue = AuditCreateContext.newInstance(inCps);
196:
197: return outValue;
198:
199: }
200:
201: private void processMulti(HashMap inValues, ArrayList inErrors,
202: String inLabel, String inParam, ParameterSet inPs) {
203: //
204: ArrayList params = new ArrayList();
205: StringTokenizer st = new StringTokenizer(inParam,
206: ConsolePageContext.HANDLE_SEPARATOR);
207: while (st.hasMoreTokens()) {
208: params.add(st.nextToken());
209: }
210: //
211: if ((params.size() % 2) == 0) {
212: for (int i = 0; i < params.size(); i = i + 2) {
213: String nextName = inPs.getParameter(((String) params
214: .get(i)));
215: String nextValue = inPs.getParameter((String) params
216: .get(i + 1));
217: nextName = nextName.trim().toUpperCase();
218: //
219: ArrayList nextErrors = new ArrayList();
220: SecurityAuditHelper.validateType(nextName, nextValue,
221: nextErrors, "For " + inLabel + ", ");
222: //
223: if (nextName.length() == 0) {
224: } else if (nextErrors.size() > 0) {
225: inErrors.addAll(nextErrors);
226: } else if (!inValues.containsKey(nextName)) {
227: inValues.put(nextName, nextValue);
228: } else {
229: inErrors.add("'" + inLabel
230: + "' already contains key '" + nextName
231: + "' (may be of different case)");
232: }
233: }
234: } else {
235: inErrors.add("'" + inLabel
236: + "' contains uneven names and values");
237: }
238: }
239: }
|