001: package com.xoetrope.xbb.appmgr;
002:
003: import com.xoetrope.service.http.XHttpRequestProxy;
004: import com.xoetrope.swing.painter.XFlarePainter;
005:
006: import java.awt.Color;
007: import java.awt.SystemColor;
008: import java.io.IOException;
009: import java.io.StringReader;
010: import java.text.SimpleDateFormat;
011: import java.util.Calendar;
012: import java.util.Date;
013: import java.util.Properties;
014: import javax.swing.JPanel;
015: import javax.swing.SwingUtilities;
016: import net.xoetrope.debug.DebugLogger;
017: import net.xoetrope.optional.data.XOptionalDataSource;
018: import net.xoetrope.optional.http.XHttpResponse;
019: import net.xoetrope.optional.http.XHttpResponseHandler;
020: import net.xoetrope.optional.http.XHttpServer;
021: import net.xoetrope.optional.service.ServiceContext;
022: import net.xoetrope.optional.service.ServiceProxyArgs;
023: import net.xoetrope.optional.service.XServiceModelNode;
024: import net.xoetrope.swing.deploy.XSystemTrayManager;
025: import net.xoetrope.swing.painter.XGradientBackground;
026: import net.xoetrope.swing.app.XDockingApp;
027: import net.xoetrope.xml.XmlElement;
028: import net.xoetrope.xml.nanoxml.NanoXmlParser;
029: import net.xoetrope.xui.PageSupport;
030: import net.xoetrope.xui.XLifeCycleListener;
031: import net.xoetrope.xui.XProject;
032: import net.xoetrope.xui.data.XBaseModel;
033: import net.xoetrope.xui.data.XModel;
034:
035: import sun.misc.BASE64Encoder;
036:
037: /**
038: * Initialize the project's connection
039: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
040: * the GNU Public License (GPL), please see license.txt for more details. If
041: * you make commercial use of this software you must purchase a commercial
042: * license from Xoetrope.</p>
043: */
044: public class ApplicationManager implements XLifeCycleListener {
045: // public static final int MAIN_CLASS = 0;
046: public static final int STARTUP_PROPERTIES = 0;
047: public static final int WIDGE_SET = 1;
048: public static final int URL = 2;
049: // public static final int USER_NAME = 3;
050: // public static final int PASSWORD = 4;
051: // public static final int SESSION_ID = 5;
052: // public static final int EVENT_TYPE_ARG = 6;
053: // public static final int EVENT_TYPE_ID = 7;
054: public static final int SESSION_ID = 3;
055: public static final int EVENT_TYPE_ARG = 4;
056: public static final int EVENT_TYPE_ID = 5;
057:
058: public static String userName = null;//"admin";
059: public static String password = null;//"admin";
060: public static String sessionId = "";
061: public static String url = "http://localhost:8080/jobs/";
062: public static String jobRequestPage = "jobs.html";
063: private static Logger logger;
064: private static int queryCount = 0;
065:
066: private static XSystemTrayManager sysTray;
067: private static XHttpServer httpServer;
068:
069: private static Date nextStartDate, nextEndDate;
070:
071: /**
072: * Called when the application/applet has been created and initialized.
073: * @param project the owner project
074: */
075: public void initialize(XProject proj) {
076: final XProject project = proj;
077: if (sysTray == null) {
078: try {
079: sysTray = XSystemTrayManager.getInstance(project);
080: sysTray.setStripSplashArgument(true);
081:
082: httpServer = new XHttpServer(12190,
083: new XHttpResponseHandler() {
084: public XHttpResponse getResponse(
085: String uri, String method,
086: Properties headers,
087: Properties params) {
088: if (params.get("action") != null) {
089: sysTray.actionPerformed(null);
090:
091: // If the startup properties/event has changed invoke the activation object
092: // sysTray.showActivationEvent();
093: }
094: return new XHttpResponse();
095: }
096: });
097: } catch (Throwable ex) {
098: DebugLogger
099: .logError("Unable to start the internal http server");
100: DebugLogger.logError(ex.getMessage());
101: }
102:
103: XBaseModel jobModel = (XBaseModel) proj.getModel().get(
104: "requests/job");
105: jobRequestPage = jobModel
106: .getAttribValueAsString(XBaseModel.VALUE_ATTRIBUTE);
107:
108: XDockingApp app = (XDockingApp) project.getAppWindow();
109: XGradientBackground gp = new XFlarePainter();
110: JPanel p = (JPanel) app.getContentPaneEx();
111: p.setForeground(new Color(255, 251, 200));
112: p.setBackground(SystemColor.control);
113: app.setBackgroundPainter(gp);
114:
115: String[] args = (String[]) project.getObject("StartupArgs");
116: if (args.length > URL) {
117: url = args[URL];
118: // userName = args[ USER_NAME ];
119: // password = args[ PASSWORD ];
120: if (args.length > SESSION_ID)
121: sessionId = args[SESSION_ID];
122:
123: final String startupEventType = args.length > EVENT_TYPE_ARG ? args[EVENT_TYPE_ARG]
124: : null;
125: //final String startupEventID = args.length > EVENT_TYPE_ID ? args[ EVENT_TYPE_ID ] : null;
126:
127: project.getPageManager().mapPageName("START_PAGE",
128: startupEventType);
129:
130: Thread t = new Thread() {
131: public void run() {
132: queryEvents(project, null, null);
133: }
134: };
135: t.start();
136: }
137: }
138: }
139:
140: public static int queryEvents(XProject proj,
141: String startupEventType, String startupEventID) {
142: final XProject project = proj;
143: final XModel rootModel = project.getModel();
144: XBaseModel model = (XBaseModel) rootModel.get("jobs");
145: XServiceModelNode node = (XServiceModelNode) model.get();
146: ((XHttpRequestProxy) node.getServiceProxy()).setupSession(
147: false, "__ac", sessionId);
148:
149: ServiceContext context = new ServiceContext();
150: ServiceProxyArgs args = context.getArgs();
151: if (userName != null) {
152: String token = userName + ":" + password;
153: String authorization = new BASE64Encoder().encode(token
154: .getBytes());
155: args.setConfigParam(XHttpRequestProxy.PARAM_AUTHORIZATION,
156: authorization);
157: }
158: if ((url == null) || (url.length() == 0))
159: return -1;
160:
161: boolean singleEventQuery = false;
162: String queryUrl = url;
163: if ((startupEventID != null) && (startupEventID.length() > 0)) {
164: queryUrl += "/" + startupEventID + "/xmlcalendar.html";
165: singleEventQuery = true;
166: } else if (queryUrl.indexOf(".htm") < 0)
167: queryUrl += "/" + jobRequestPage;
168:
169: args.setConfigParam("url", queryUrl);
170:
171: if (!singleEventQuery) {
172: if (nextStartDate == null)
173: nextStartDate = new Date();
174: SimpleDateFormat df = new SimpleDateFormat("yyyy/M/d");
175: args.setPassParam("begindate", df.format(nextStartDate));
176:
177: Calendar endDate = Calendar.getInstance();
178: if (nextEndDate != null)
179: endDate.setTime(nextEndDate);
180: if (endDate.get(Calendar.MONTH) == 11)
181: endDate.roll(Calendar.YEAR, true);
182: endDate.roll(Calendar.MONTH, true);
183: args.setPassParam("enddate", df.format(endDate.getTime()));
184: }
185:
186: // Clear the search data
187: nextStartDate = nextEndDate = null;
188:
189: String result = (String) node.get(context);
190: if (node.getStatus() >= 300)
191: return -1;
192:
193: //System.out.println( result );
194: StringReader sr = new StringReader(result);
195: NanoXmlParser parser = new NanoXmlParser();
196: XmlElement httpElement = parser.parse(sr);
197: XOptionalDataSource modelDataSource = (XOptionalDataSource) project
198: .getObject("ModelDataSource");
199: httpElement.setAttribute("id", "job");
200:
201: XBaseModel jobsModel = (XBaseModel) rootModel.get("job");
202: if (queryCount == 0)
203: jobsModel.removeChildren();
204:
205: queryCount++;
206: // if ( singleEventQuery ) {
207: // // Add the enclosing events node!
208: // XmlElement httpParent = new NanoXmlElement( "Events" );
209: // XmlElement child = httpElement.getFirstChildNamed( "Data" );
210: // httpElement.setAttribute( "id", child.getAttribute( "id" ));
211: // httpParent.addChild( httpElement );
212: // httpElement = httpParent;
213: // }
214: modelDataSource.loadTable(httpElement, jobsModel);
215: int newPendingCount = 0;
216:
217: // Now filter the events for pending and sent status and fill the tree's structure'
218: XBaseModel pendingModel = (XBaseModel) rootModel
219: .get("myJobs/job/1");
220: int pending = pendingModel.getNumChildren();
221: XBaseModel issuedModel = (XBaseModel) rootModel
222: .get("myJobs/job/2");
223: int issued = issuedModel.getNumChildren();
224: int numEvents = jobsModel.getNumChildren();
225: for (int i = 0; i < numEvents; i++) {
226: XModel eventModel = jobsModel.get(i);
227: String id = eventModel.getId();
228:
229: /** @todo fix this - the bindings seem to be saving to this node's parent erroneously */
230: String name = null;
231: Object titleModel = ((XModel) eventModel.get("title"))
232: .get();
233: if (titleModel != null)
234: name = titleModel.toString();
235: else
236: continue;
237:
238: XModel sentModel = (XModel) eventModel.get("letterSent");
239: if (sentModel != null) {
240: String sentStr = sentModel.get().toString()
241: .toLowerCase();
242: if ("true".equals(sentStr) || "tes".equals(sentStr))
243: issuedModel.set(id, name);
244: else {
245: pendingModel.set(id, name);
246: newPendingCount++;
247: }
248: } else
249: pendingModel.set(id, name);
250: }
251:
252: if (!singleEventQuery) {
253: SwingUtilities.invokeLater(new Runnable() {
254: public void run() {
255: PageSupport leftPage = project.getPageManager()
256: .getPage("left");
257: if (leftPage != null) {
258: Object tree = leftPage
259: .findComponent("taskList");
260: leftPage.getBinding(tree).get();
261: }
262:
263: if (logger == null)
264: logger = (Logger) ((XBaseModel) rootModel
265: .get("Logger")).get();
266: if (logger != null)
267: logger
268: .addMessage("Event list retrieved from server");
269: }
270: });
271: }
272:
273: return newPendingCount;
274: }
275:
276: /**
277: * Called when the application/applet has been shutdown and is about to exit
278: */
279: public void shutdown() {
280: }
281:
282: /**
283: * Can the current project close?
284: * @param project the project initiating the shutdown
285: * @return true if the application can close
286: */
287: public boolean canClose(XProject project) {
288: return true;
289: }
290:
291: public static void setNextStartDate(Date d) {
292: nextStartDate = d;
293: }
294:
295: public static void setNextEndDate(Date d) {
296: nextEndDate = d;
297: }
298: }
|