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.services.console;
009:
010: //base classes
011: import java.io.IOException;
012: import java.math.BigDecimal;
013: import java.sql.Connection;
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.HashMap;
017: import javax.xml.parsers.ParserConfigurationException;
018: import org.w3c.dom.Document;
019: import org.xml.sax.SAXException;
020:
021: //project specific classes
022: import org.jfolder.common.UnexpectedSystemException;
023: import org.jfolder.common.entity.SystemEntityUpdatesContext;
024: import org.jfolder.common.files.VirtualFileSystemHolder;
025: import org.jfolder.common.tagging.ConceptTagCharacteristic;
026: import org.jfolder.common.tagging.ConceptTagPreferencesHelper;
027: import org.jfolder.common.tagging.RootConceptTagHolder;
028: import org.jfolder.common.utils.misc.MiscHelper;
029: import org.jfolder.common.utils.xml.XMLHelper;
030: import org.jfolder.console.config.ConsoleConfigTagHelper;
031: import org.jfolder.platforms.stores.base.CreateStatement;
032: import org.jfolder.platforms.stores.base.DeleteStatement;
033: import org.jfolder.platforms.stores.base.InsertStatement;
034: import org.jfolder.platforms.stores.base.SelectStatement;
035: import org.jfolder.platforms.stores.base.SystemDatabase;
036: import org.jfolder.platforms.stores.base.SystemResultSet;
037: import org.jfolder.platforms.stores.base.SystemStatement;
038: import org.jfolder.platforms.stores.base.SystemStore;
039: import org.jfolder.platforms.stores.base.SystemStoreFactory;
040: import org.jfolder.platforms.stores.base.UpdateStatement;
041: import org.jfolder.platforms.stores.base.WhereStatement;
042: import org.jfolder.security.audit.SecurityAudit;
043: import org.jfolder.security.audit.SecurityAuditHelper;
044: import org.jfolder.security.audit.SecurityAuditSet;
045: import org.jfolder.security.model.UserIdentity;
046: import org.jfolder.services.base.AbstractBaseServiceBean;
047:
048: //other classes
049:
050: class ConsoleServiceBean extends AbstractBaseServiceBean implements
051: ConsoleService {
052:
053: protected ConsoleServiceBean() {
054: }
055:
056: public RootConceptTagHolder getConsoleConfig(UserIdentity inUi,
057: String inPreferencesHandle) {
058: //
059: Connection conn = null;
060: try {
061: RootConceptTagHolder outValue = null;
062:
063: //
064: SystemStore ss = SystemStoreFactory.getSystemStore();
065: SystemDatabase sd = SystemDatabase.newInstance();
066: //
067: conn = SystemStore.getConnection(ss);
068:
069: BigDecimal userId = saveUser(inUi, conn, ss);
070:
071: CreateStatement userStudioTable = sd
072: .createUserStudioTable();
073:
074: if (isConsoleConfigPresent(userId, inPreferencesHandle, ss,
075: sd, conn)) {
076: //
077:
078: SelectStatement userStudioSelect = SelectStatement
079: .createOrderById(userStudioTable, null);
080: //
081: userStudioSelect.addSelectColumn(
082: SystemDatabase.C_CONFIG_T_USER_STUDIO,
083: userStudioTable, null);
084: //
085: userStudioSelect.addWhereColumn(
086: SystemDatabase.C_USER_ID_T_USER_STUDIO,
087: SystemStatement.NORMAL_COLUMN, userStudioTable,
088: userId, SelectStatement.EQUALS, null);
089: userStudioSelect.addWhereColumn(
090: SystemDatabase.C_TYPE_T_USER_STUDIO,
091: SystemStatement.NORMAL_COLUMN, userStudioTable,
092: inPreferencesHandle, SelectStatement.EQUALS,
093: null);
094: //
095: //
096: ss.execute(userStudioSelect, conn);
097: SystemResultSet userStudioSrs = userStudioSelect
098: .getResults();
099: //
100: String s = userStudioSrs.getLString(0, 1);
101: //
102: Document doc = XMLHelper.loadDocument(s);
103: //
104: outValue = ConsoleConfigTagHelper.getRootHolder(doc);
105: } else {
106: if (inPreferencesHandle
107: .equals(ConceptTagPreferencesHelper.CTP_PREF_HANDLE__WEB_PAGE)) {
108: //
109: outValue = ConsoleConfigTagHelper
110: .getDefaultConsoleConfigForWebPage();
111: } else {
112: throw new UnexpectedSystemException(
113: "There is no default ConsoleConfig for handle '"
114: + inPreferencesHandle + "'");
115: }
116: }
117:
118: return outValue;
119: } catch (SAXException saxe) {
120: throw new UnexpectedSystemException(saxe);
121: } catch (IOException ioe) {
122: throw new UnexpectedSystemException(ioe);
123: } catch (ParserConfigurationException pce) {
124: throw new UnexpectedSystemException(pce);
125: } finally {
126: SystemStore.closeSQLObjects(conn, null, null);
127: }
128: }
129:
130: public void saveConsoleConfig(UserIdentity inUi,
131: String inPreferencesHandle, RootConceptTagHolder inRcth) {
132: //
133: Connection conn = null;
134: try {
135: //
136: SystemStore ss = SystemStoreFactory.getSystemStore();
137: SystemDatabase sd = SystemDatabase.newInstance();
138: //
139: conn = SystemStore.getConnection(ss);
140:
141: BigDecimal userId = saveUser(inUi, conn, ss);
142:
143: CreateStatement userStudioTable = sd
144: .createUserStudioTable();
145:
146: if (isConsoleConfigPresent(userId, inPreferencesHandle, ss,
147: sd, conn)) {
148: //
149: UpdateStatement userStudioUpdate = SystemStatement
150: .newUpdateStatement(userStudioTable);
151: //
152: userStudioUpdate.addWhereColumn(
153: SystemDatabase.C_USER_ID_T_USER_STUDIO,
154: SystemStatement.NORMAL_COLUMN, userStudioTable,
155: userId, WhereStatement.EQUALS, null);
156: userStudioUpdate.addWhereColumn(
157: SystemDatabase.C_TYPE_T_USER_STUDIO,
158: SystemStatement.NORMAL_COLUMN, userStudioTable,
159: inPreferencesHandle, WhereStatement.EQUALS,
160: null);
161: //
162: userStudioUpdate.addUpdateColumn(
163: SystemDatabase.C_CONFIG_T_USER_STUDIO, inRcth
164: .getXMLRepresentation());
165: //
166: ss.execute(userStudioUpdate, conn);
167: ss.commit(conn);
168: } else {
169: InsertStatement userStudioInsert = SystemStatement
170: .newInsertStatement(userStudioTable);
171: //
172: userStudioInsert.setDecimalColumn(
173: SystemDatabase.C_USER_ID_T_USER_STUDIO, userId);
174: userStudioInsert.setSStringColumn(
175: SystemDatabase.C_TYPE_T_USER_STUDIO,
176: inPreferencesHandle);
177: userStudioInsert.setLStringColumn(
178: SystemDatabase.C_CONFIG_T_USER_STUDIO, inRcth
179: .getXMLRepresentation());
180: //
181: ss.execute(userStudioInsert, conn);
182: ss.commit(conn);
183: }
184: } finally {
185: SystemStore.closeSQLObjects(conn, null, null);
186: }
187: }
188:
189: public void resetConsoleConfig(UserIdentity inUi,
190: String inPreferencesHandle) {
191: //
192: //
193: Connection conn = null;
194: try {
195: //
196: SystemStore ss = SystemStoreFactory.getSystemStore();
197: SystemDatabase sd = SystemDatabase.newInstance();
198: //
199: conn = SystemStore.getConnection(ss);
200:
201: BigDecimal userId = saveUser(inUi, conn, ss);
202:
203: CreateStatement userStudioTable = sd
204: .createUserStudioTable();
205:
206: if (isConsoleConfigPresent(userId, inPreferencesHandle, ss,
207: sd, conn)) {
208: //
209: DeleteStatement userStudioDelete = SystemStatement
210: .newDeleteStatement(userStudioTable);
211: //
212: userStudioDelete.addWhereColumn(
213: SystemDatabase.C_USER_ID_T_USER_STUDIO,
214: SystemStatement.NORMAL_COLUMN, userStudioTable,
215: userId, WhereStatement.EQUALS, null);
216: userStudioDelete.addWhereColumn(
217: SystemDatabase.C_TYPE_T_USER_STUDIO,
218: SystemStatement.NORMAL_COLUMN, userStudioTable,
219: inPreferencesHandle, WhereStatement.EQUALS,
220: null);
221: //
222: //
223: ss.execute(userStudioDelete, conn);
224: ss.commit(conn);
225: }
226: } finally {
227: SystemStore.closeSQLObjects(conn, null, null);
228: }
229: }
230:
231: //
232: //
233: //
234: //
235: //
236: //
237: //
238: //
239: //
240: //
241: //
242: private boolean isConsoleConfigPresent(BigDecimal inUserId,
243: String inPreferencesHandle, SystemStore inSs,
244: SystemDatabase inSd, Connection inConn) {
245: //
246: boolean outValue = false;
247:
248: CreateStatement userStudioTable = inSd.createUserStudioTable();
249:
250: SelectStatement userStudioSelect = SelectStatement
251: .createOrderById(userStudioTable, null);
252: //
253: userStudioSelect.addWhereColumn(
254: SystemDatabase.C_USER_ID_T_USER_STUDIO,
255: SystemStatement.NORMAL_COLUMN, userStudioTable,
256: inUserId, SelectStatement.EQUALS, null);
257: userStudioSelect.addWhereColumn(
258: SystemDatabase.C_TYPE_T_USER_STUDIO,
259: SystemStatement.NORMAL_COLUMN, userStudioTable,
260: inPreferencesHandle, SelectStatement.EQUALS, null);
261: //
262: //
263: inSs.execute(userStudioSelect, inConn);
264: SystemResultSet userStudioSrs = userStudioSelect.getResults();
265: //
266: outValue = (userStudioSrs.getRowCount() > 0);
267:
268: return outValue;
269: }
270:
271: //
272: //
273: //
274: //
275: //
276: //
277: //
278: //
279: //
280: //
281: public void updateAudit(BigDecimal inId, UserIdentity inUi,
282: long inUtcDateTime, String inAnnotation, Boolean inStatus) {
283: //
284: Connection conn = null;
285: try {
286:
287: SystemStore ss = SystemStoreFactory.getSystemStore();
288: conn = SystemStore.getConnection(ss);
289: //
290: BigDecimal userId = saveUser(inUi, conn, ss);
291: //
292: SystemDatabase sd = SystemDatabase.newInstance();
293: CreateStatement auditTable = sd.createAuditTable();
294: if (inStatus != null) {
295: UpdateStatement auditUpdate = SystemStatement
296: .newUpdateStatement(auditTable);
297: //
298: auditUpdate.addWhereColumn(null,
299: SystemStatement.ID_COLUMN, auditTable, inId,
300: WhereStatement.EQUALS, null);
301: //
302: auditUpdate.addUpdateColumn(
303: SystemDatabase.C_OPENED_T_AUDIT, inStatus);
304: ss.execute(auditUpdate, conn);
305: }
306: //
307: //
308: //
309: //
310: CreateStatement auditAnnotationTable = sd
311: .createAuditAnnotationTable();
312: //
313: InsertStatement auditAnnotationInsert = SystemStatement
314: .newInsertStatement(auditAnnotationTable);
315: //
316: auditAnnotationInsert.setDecimalColumn(
317: SystemDatabase.C_AUDIT_ID_T_AUDIT_ANNOTATION, inId);
318: auditAnnotationInsert
319: .setDecimalColumn(
320: SystemDatabase.C_USER_ID_T_AUDIT_ANNOTATION,
321: userId);
322: auditAnnotationInsert.setDecimalColumn(
323: SystemDatabase.C_DATE_TIME_T_AUDIT_ANNOTATION,
324: new BigDecimal(inUtcDateTime));
325: auditAnnotationInsert.setBooleanColumn(
326: SystemDatabase.C_OPENED_T_AUDIT_ANNOTATION,
327: inStatus);
328: auditAnnotationInsert.setLStringColumn(
329: SystemDatabase.C_AUDIT_NOTE_T_AUDIT_ANNOTATION,
330: inAnnotation);
331: ss.execute(auditAnnotationInsert, conn);
332: //
333: //
334: //
335: //
336: //
337: ss.commit(conn);
338: } finally {
339: SystemStore.closeSQLObjects(conn, null, null);
340: }
341: }
342:
343: public void saveAudit(SecurityAudit inSa) {
344: //
345: Connection conn = null;
346: try {
347:
348: SystemStore ss = SystemStoreFactory.getSystemStore();
349: conn = SystemStore.getConnection(ss);
350: saveAudit(inSa, conn, ss);
351: ss.commit(conn);
352: } finally {
353: SystemStore.closeSQLObjects(conn, null, null);
354: }
355: }
356:
357: public SecurityAuditSet getSecurityAudits(long inStartDate,
358: long inEndDate, BigDecimal inSeverityLevels[],
359: String inCategoryTypes[], String inSourceTypes[],
360: BigDecimal inCustomCodes[], String inCustomCategoryTypes[],
361: String inCustomSourceTypes[], Boolean inOpened) {
362: //
363: Connection conn = null;
364: try {
365: SecurityAuditSet outValue = SecurityAuditSet.newInstance();
366:
367: //
368: SystemStore ss = SystemStoreFactory.getSystemStore();
369: SystemDatabase sd = SystemDatabase.newInstance();
370: //
371: CreateStatement auditTable = sd.createAuditTable();
372: //
373: conn = SystemStore.getConnection(ss);
374:
375: //
376: SelectStatement auditSelect = getSecurityAuditSelectWithoutWhereClause();
377: //
378: auditSelect.addWhereColumn(
379: SystemDatabase.C_DATE_TIME_T_AUDIT,
380: SystemStatement.NORMAL_COLUMN, auditTable,
381: new BigDecimal(inStartDate),
382: SelectStatement.GREATER_THAN_OR_EQUAL, null);
383: auditSelect.addWhereColumn(
384: SystemDatabase.C_DATE_TIME_T_AUDIT,
385: SystemStatement.NORMAL_COLUMN, auditTable,
386: new BigDecimal(inEndDate),
387: SelectStatement.LESS_THAN, null);
388:
389: //
390: if (inSeverityLevels.length > 0) {
391: for (int i = 0; i < inSeverityLevels.length; i++) {
392: //
393: Integer paren = getParenthesis(i,
394: inSeverityLevels.length);
395: //
396: auditSelect.addWhereColumn(
397: SystemDatabase.C_SEVERITY_LEVEL_T_AUDIT,
398: SystemStatement.NORMAL_COLUMN, auditTable,
399: inSeverityLevels[i],
400: SelectStatement.EQUALS, paren);
401: //
402: auditSelect.useOrConjunction();
403: }
404: }
405: auditSelect.useAndConjunction();
406:
407: //
408: if (inCategoryTypes.length > 0) {
409: for (int i = 0; i < inCategoryTypes.length; i++) {
410: //
411: Integer paren = getParenthesis(i,
412: inCategoryTypes.length);
413: //
414: auditSelect.addWhereColumn(
415: SystemDatabase.C_CATEGORY_TYPE_T_AUDIT,
416: SystemStatement.NORMAL_COLUMN, auditTable,
417: getLikeRepresentation(inCategoryTypes[i]),
418: SelectStatement.LIKE, paren);
419: //
420: auditSelect.useOrConjunction();
421: }
422: }
423: auditSelect.useAndConjunction();
424:
425: //
426: if (inSourceTypes.length > 0) {
427: for (int i = 0; i < inSourceTypes.length; i++) {
428: //
429: Integer paren = getParenthesis(i,
430: inSourceTypes.length);
431: //
432: auditSelect.addWhereColumn(
433: SystemDatabase.C_SOURCE_TYPE_T_AUDIT,
434: SystemStatement.NORMAL_COLUMN, auditTable,
435: getLikeRepresentation(inSourceTypes[i]),
436: SelectStatement.LIKE, paren);
437: //
438: auditSelect.useOrConjunction();
439: }
440: }
441: auditSelect.useAndConjunction();
442:
443: //
444: if (inCustomCodes.length > 0) {
445: for (int i = 0; i < inCustomCodes.length; i++) {
446: //
447: Integer paren = getParenthesis(i,
448: inCustomCodes.length);
449: //
450: auditSelect.addWhereColumn(
451: SystemDatabase.C_CUSTOM_CODE_T_AUDIT,
452: SystemStatement.NORMAL_COLUMN, auditTable,
453: inCustomCodes[i], SelectStatement.EQUALS,
454: paren);
455: //
456: auditSelect.useOrConjunction();
457: }
458: }
459: auditSelect.useAndConjunction();
460:
461: //
462: if (inCustomCategoryTypes.length > 0) {
463: for (int i = 0; i < inCustomCategoryTypes.length; i++) {
464: //
465: Integer paren = getParenthesis(i,
466: inCustomCategoryTypes.length);
467: //
468: //
469: auditSelect
470: .addWhereColumn(
471: SystemDatabase.C_CUSTOM_CAT_TYPE_T_AUDIT,
472: SystemStatement.NORMAL_COLUMN,
473: auditTable,
474: getLikeRepresentation(inCustomCategoryTypes[i]),
475: SelectStatement.LIKE, paren);
476: //
477: auditSelect.useOrConjunction();
478: }
479: }
480: auditSelect.useAndConjunction();
481:
482: //
483: if (inCustomSourceTypes.length > 0) {
484: for (int i = 0; i < inCustomSourceTypes.length; i++) {
485: //
486: Integer paren = getParenthesis(i,
487: inCustomSourceTypes.length);
488: //
489: //
490: auditSelect
491: .addWhereColumn(
492: SystemDatabase.C_CUSTOM_SRC_TYPE_T_AUDIT,
493: SystemStatement.NORMAL_COLUMN,
494: auditTable,
495: getLikeRepresentation(inCustomSourceTypes[i]),
496: SelectStatement.LIKE, paren);
497: //
498: auditSelect.useOrConjunction();
499: }
500: }
501: auditSelect.useAndConjunction();
502:
503: //
504: if (inOpened != null) {
505: auditSelect.addWhereColumn(
506: SystemDatabase.C_OPENED_T_AUDIT,
507: SystemStatement.NORMAL_COLUMN, auditTable,
508: inOpened, SelectStatement.EQUALS, null);
509: }
510: auditSelect.useAndConjunction();
511:
512: //
513: //
514: ss.execute(auditSelect, conn);
515: SystemResultSet auditSrs = auditSelect.getResults();
516: for (int i = 0; i < auditSrs.getRowCount(); i++) {
517: SecurityAudit nextSa = getSecurityAuditFromResults(
518: conn, ss, auditSrs, i);
519: BigDecimal nextId = getSecurityAuditIdFromResults(
520: auditSrs, i);
521: outValue.addSecurityAudit(nextId, nextSa);
522: }
523:
524: return outValue;
525: } finally {
526: SystemStore.closeSQLObjects(conn, null, null);
527: }
528: }
529:
530: private final static String getLikeRepresentation(String inValue) {
531: //
532: String outValue = null;
533:
534: outValue = "%"
535: + SecurityAuditHelper.SECURITY_AUDIT_TYPE_START
536: + inValue
537: + SecurityAuditHelper.SECURITY_AUDIT_TYPE_NAME_VALUE_JUNCTION
538: + "%";
539:
540: return outValue;
541: }
542:
543: private final static Integer getParenthesis(int inIndex, int inTotal) {
544:
545: Integer outValue = null;
546:
547: if (inIndex == 0 && inIndex == (inTotal - 1)) {
548: //
549: } else if (inIndex == 0) {
550: outValue = new Integer(1);
551: } else if (inIndex == (inTotal - 1)) {
552: outValue = new Integer(-1);
553: }
554:
555: return outValue;
556: }
557: }
|