001: /**
002: *
003: */package net.sf.crispy.sample.jira;
004:
005: import java.lang.reflect.Array;
006: import java.text.SimpleDateFormat;
007: import java.util.Date;
008: import java.util.HashSet;
009: import java.util.Hashtable;
010: import java.util.Iterator;
011: import java.util.Map;
012: import java.util.Vector;
013:
014: import net.sf.crispy.InterceptorContext;
015: import net.sf.crispy.Modifier;
016: import net.sf.crispy.util.Converter;
017: import net.sf.crispy.util.Util;
018:
019: import com.atlassian.jira.rpc.soap.beans.RemoteComment;
020: import com.atlassian.jira.rpc.soap.beans.RemoteIssue;
021: import com.atlassian.jira.rpc.soap.beans.RemoteIssueType;
022: import com.atlassian.jira.rpc.soap.beans.RemoteProject;
023: import com.atlassian.jira.rpc.soap.beans.RemoteStatus;
024: import com.atlassian.jira.rpc.soap.beans.RemoteUser;
025:
026: /**
027: * @author Linke
028: *
029: */
030: public class TokenAndResultModifier implements Modifier {
031:
032: private String token = null;
033: private int kindOfService = -1;
034:
035: public void setKindOfService(int pvKindOfService) {
036: kindOfService = pvKindOfService;
037: }
038:
039: public InterceptorContext modifyBeforeInvocation(
040: InterceptorContext pvInterceptorContext) {
041: InterceptorContext lvInterceptorContext = pvInterceptorContext;
042: String lvMethodName = pvInterceptorContext.getMethod()
043: .getName();
044: if (lvMethodName.equals("login") == false) {
045: Object args[] = (pvInterceptorContext.getArgs() == null ? new Object[0]
046: : pvInterceptorContext.getArgs());
047: int s = args.length;
048: Object lvNewArgs[] = new Object[s + 1];
049: lvNewArgs[0] = token;
050: for (int i = 0; i < args.length; i++) {
051: lvNewArgs[i + 1] = args[i];
052: }
053: lvInterceptorContext.setArgs(lvNewArgs);
054: }
055: return lvInterceptorContext;
056: }
057:
058: public InterceptorContext modifyAfterInvocation(
059: InterceptorContext pvInterceptorContext) {
060: InterceptorContext lvInterceptorContext = pvInterceptorContext;
061: if (token == null) {
062: String lvMethodName = pvInterceptorContext.getMethod()
063: .getName();
064: if (lvMethodName.equals("login") == true) {
065: Object lvToken = lvInterceptorContext.getResult();
066: if (lvToken != null) {
067: token = lvToken.toString();
068: }
069: }
070: }
071:
072: // convert vector or hashtable for XML-RPC
073: try {
074: if (kindOfService == JiraServiceManager.XMLRPC_SERVICE) {
075: String lvMethodName = pvInterceptorContext.getMethod()
076: .getName();
077: // Object lvResultObjects[] = null;
078: if (lvMethodName.equals("getComments")) {
079: makeComplexObjects(lvInterceptorContext,
080: RemoteComment.class,
081: new String[] { "timePerformed" });
082: }
083: // else if (lvMethodName.equals("getIssue")) {
084: // makeComplexObjects(lvInterceptorContext, RemoteIssue.class);
085: // }
086: else if (lvMethodName.equals("getStatuses")) {
087: makeComplexObjects(lvInterceptorContext,
088: RemoteStatus.class);
089: } else if (lvMethodName.equals("getUser")) {
090: RemoteUser ru = (RemoteUser) makeComplexOneObject(
091: lvInterceptorContext.getResult(),
092: RemoteUser.class);
093: lvInterceptorContext.setResult(ru);
094: } else if (lvMethodName.equals("getIssueTypes")) {
095: makeComplexObjects(lvInterceptorContext,
096: RemoteIssueType.class);
097: } else if (lvMethodName.equals("getProjects")) {
098: makeComplexObjects(lvInterceptorContext,
099: RemoteProject.class);
100: } else if (lvMethodName.equals("getIssue")) {
101: RemoteIssue issue = (RemoteIssue) makeComplexOneObject(
102: lvInterceptorContext.getResult(),
103: RemoteIssue.class, new String[] {
104: "created", "updated", "duedate" });
105: lvInterceptorContext.setResult(issue);
106: }
107: }
108: } catch (Exception e) {
109: e.printStackTrace();
110: }
111:
112: return lvInterceptorContext;
113: }
114:
115: private void makeComplexObjects(
116: InterceptorContext pvInterceptorContext, Class pvClass)
117: throws Exception {
118: makeComplexObjects(pvInterceptorContext, pvClass, new String[0]);
119: }
120:
121: private void makeComplexObjects(
122: InterceptorContext pvInterceptorContext, Class pvClass,
123: String pvDateNames[]) throws Exception {
124: Vector v = (Vector) pvInterceptorContext.getResult();
125: Object lvObjects[] = (Object[]) Array.newInstance(pvClass, v
126: .size());
127: for (int i = 0; i < v.size(); i++) {
128: Hashtable h = (Hashtable) v.get(i);
129: Util.printMapInfo(h);
130: h.put("class", pvClass.getName());
131:
132: for (int j = 0; j < pvDateNames.length; j++) {
133: String lvTime = (String) h.get(pvDateNames[j]);
134: SimpleDateFormat lvDateFormat = new SimpleDateFormat(
135: "yyyy-MM-dd HH:mm:ss.SSS");
136: Date d = lvDateFormat.parse(lvTime);
137: h.put(pvDateNames[j], d);
138: }
139:
140: Object o = new Converter().makeComplex(h);
141: lvObjects[i] = o;
142: }
143: pvInterceptorContext.setResult(lvObjects);
144: }
145:
146: private Object makeComplexOneObject(Object pvObject, Class pvClass)
147: throws Exception {
148: return makeComplexOneObject(pvObject, pvClass, new String[0]);
149: }
150:
151: private Object makeComplexOneObject(Object pvObject, Class pvClass,
152: String pvDateNames[]) throws Exception {
153: Hashtable h = (Hashtable) pvObject;
154: // Util.printMapInfo(h);
155: h.put("class", pvClass.getName());
156:
157: for (int j = 0; j < pvDateNames.length; j++) {
158: String lvTime = (String) h.get(pvDateNames[j]);
159: if (lvTime != null) {
160: SimpleDateFormat lvDateFormat = new SimpleDateFormat(
161: "yyyy-MM-dd HH:mm:ss.SSS");
162: Date d = lvDateFormat.parse(lvTime);
163: h.put(pvDateNames[j], d);
164: }
165: }
166:
167: Object o = new Converter().makeComplex(findAndDelFromMap(h));
168: return o;
169: }
170:
171: private Hashtable findAndDelFromMap(Map pvMap) {
172: Hashtable lvMap = new Hashtable(pvMap);
173: Iterator it = new HashSet(lvMap.keySet()).iterator();
174: while (it.hasNext()) {
175: Object key = (Object) it.next();
176: Object value = lvMap.get(key);
177: if (value instanceof Vector) {
178: lvMap.remove(key);
179: }
180: }
181: return lvMap;
182: }
183: }
|