001: package org.manentia.kasai.ui.audit;
002:
003: import java.io.IOException;
004: import java.text.DateFormat;
005: import java.text.SimpleDateFormat;
006: import java.util.ArrayList;
007: import java.util.Calendar;
008: import java.util.Collection;
009: import java.util.Collections;
010: import java.util.Comparator;
011: import java.util.Date;
012: import java.util.GregorianCalendar;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Map;
017:
018: import javax.servlet.http.HttpServletRequest;
019:
020: import org.apache.commons.beanutils.BeanComparator;
021: import org.apache.commons.collections.comparators.ReverseComparator;
022: import org.apache.commons.lang.StringUtils;
023: import org.manentia.kasai.KasaiFacade;
024: import org.manentia.kasai.audit.AuditHandler;
025: import org.manentia.kasai.exceptions.CannotAuditException;
026: import org.manentia.kasai.exceptions.DataAccessException;
027: import org.manentia.kasai.ui.UserView;
028: import org.manentia.kasai.ui.actions.BaseAction;
029: import org.manentia.kasai.ui.exceptions.SessionExpiredException;
030:
031: import uk.ltd.getahead.dwr.WebContext;
032: import uk.ltd.getahead.dwr.WebContextFactory;
033:
034: import com.manentia.commons.NonCriticalException;
035: import com.manentia.commons.audit.AuditBean;
036: import com.manentia.commons.log.Log;
037: import com.manentia.commons.xml.XMLException;
038:
039: public class AuditFacade {
040: private String lastSortOrder = null;
041:
042: public Map refresh(int page, String dateFrom, int dateFromHour,
043: int dateFromMinute, String dateTo, int dateToHour,
044: int dateToMinute, String user, String operation)
045: throws SessionExpiredException, DataAccessException,
046: XMLException, CannotAuditException, NonCriticalException {
047:
048: WebContext ctx = WebContextFactory.get();
049: HttpServletRequest request = ctx.getHttpServletRequest();
050:
051: UserView userView = new BaseAction().validateSession(request);
052: DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
053:
054: Date dateFromDate = null;
055: try {
056: if (StringUtils.isNotEmpty(dateFrom)) {
057: dateFromDate = dateFormat.parse(dateFrom);
058: GregorianCalendar dateFromCal = new GregorianCalendar();
059: dateFromCal.setTime(dateFromDate);
060: dateFromCal.set(Calendar.HOUR_OF_DAY, 0);
061: dateFromCal.set(Calendar.MINUTE, 0);
062: dateFromCal.set(Calendar.SECOND, 0);
063: dateFromCal.set(Calendar.MILLISECOND, 0);
064:
065: if (dateFromHour >= 0) {
066:
067: dateFromCal.set(Calendar.HOUR_OF_DAY, dateFromHour);
068: if (dateFromMinute >= 0) {
069: dateFromCal
070: .set(Calendar.MINUTE, dateFromMinute);
071: }
072: }
073: dateFromDate = dateFromCal.getTime();
074:
075: }
076: } catch (Exception e) {
077: }
078:
079: Date dateToDate = null;
080: try {
081: if (StringUtils.isNotEmpty(dateTo)) {
082: dateToDate = dateFormat.parse(dateTo);
083: GregorianCalendar dateToCal = new GregorianCalendar();
084: dateToCal.setTime(dateToDate);
085: dateToCal.set(Calendar.HOUR_OF_DAY, 23);
086: dateToCal.set(Calendar.MINUTE, 59);
087: dateToCal.set(Calendar.SECOND, 59);
088: dateToCal.set(Calendar.MILLISECOND, 999);
089:
090: if (dateToHour >= 0) {
091: dateToCal.set(Calendar.HOUR_OF_DAY, dateToHour);
092: if (dateToMinute >= 0) {
093: dateToCal.set(Calendar.MINUTE, dateToMinute);
094: }
095: }
096:
097: dateToDate = dateToCal.getTime();
098: }
099: } catch (Exception e) {
100: }
101:
102: Collection<AuditBean> entries = KasaiFacade.getInstance()
103: .listAuditEntries(userView.getUser(), dateFromDate,
104: dateToDate, user, operation,
105: request.getRemoteAddr());
106:
107: request.getSession().setAttribute("entries", entries);
108:
109: return goToPageEntries(page);
110: }
111:
112: public String getEntryDetails(int entryId) throws XMLException,
113: IOException {
114: WebContext ctx = WebContextFactory.get();
115: HttpServletRequest request = ctx.getHttpServletRequest();
116:
117: String result = null;
118:
119: AuditBean entry = null;
120: Collection<AuditBean> entries = (Collection<AuditBean>) request
121: .getSession().getAttribute("entries");
122:
123: for (AuditBean currentEntry : entries) {
124: if (currentEntry.getId() == entryId) {
125: entry = currentEntry;
126: break;
127: }
128: }
129:
130: if (entry != null) {
131: result = (String) entry.getProperty("transaction_data");
132: }
133:
134: return result;
135: }
136:
137: public Map goToPageEntries(int page) {
138: ArrayList result = null;
139: WebContext ctx = WebContextFactory.get();
140: HttpServletRequest request = ctx.getHttpServletRequest();
141:
142: Map map = new HashMap();
143:
144: Collection entries = (List) request.getSession().getAttribute(
145: "entries");
146: int rowsPerPage = ((Integer) request.getSession().getAttribute(
147: "rowsPerPage")).intValue();
148:
149: result = new ArrayList(rowsPerPage);
150: int index = 0;
151: for (Iterator iter = entries.iterator(); iter.hasNext()
152: && index < rowsPerPage * page;) {
153: AuditBean entry = (AuditBean) iter.next();
154: if (index >= rowsPerPage * (page - 1)
155: && index < rowsPerPage * page) {
156: result.add(entry);
157: }
158: index++;
159: }
160:
161: map.put("list", result);
162: map.put("totalSize", new Integer(entries.size()));
163:
164: return map;
165: }
166:
167: public Map sortEntries(String sortOrder) {
168: WebContext ctx = WebContextFactory.get();
169: HttpServletRequest request = ctx.getHttpServletRequest();
170:
171: if (StringUtils.isEmpty(sortOrder)) {
172: sortOrder = "dateFrom";
173: }
174:
175: boolean reverse = sortOrder.equalsIgnoreCase(lastSortOrder);
176: Comparator comparator = new BeanComparator(sortOrder);
177:
178: if (reverse) {
179: comparator = new ReverseComparator(comparator);
180: }
181:
182: List entries = (List) request.getSession().getAttribute(
183: "entries");
184: Collections.sort(entries, comparator);
185:
186: lastSortOrder = reverse ? "__CHANGE__" : sortOrder;
187:
188: return goToPageEntries(1);
189: }
190:
191: }
|