KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > component > UserFilesComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Aug 18, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.ui.component;
18
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.dom4j.Document;
30 import org.dom4j.DocumentHelper;
31 import org.dom4j.Element;
32 import org.pentaho.messages.Messages;
33 import org.pentaho.core.runtime.IBackgroundExecution;
34 import org.pentaho.plugin.quartz.QuartzSystemListener;
35 import org.pentaho.core.repository.IContentItem;
36 import org.pentaho.core.session.IPentahoSession;
37 import org.pentaho.core.system.PentahoSystem;
38 import org.pentaho.core.ui.IPentahoUrlFactory;
39 import org.pentaho.ui.XmlComponent;
40 import org.quartz.JobDataMap;
41 import org.quartz.JobDetail;
42 import org.quartz.Scheduler;
43
44 public class UserFilesComponent extends XmlComponent {
45
46     /**
47      *
48      */

49     private static final long serialVersionUID = -7404173000559758744L;
50     
51     protected final static String JavaDoc FILE = "file"; //$NON-NLS-1$
52

53     protected final static String JavaDoc NAME = "name"; //$NON-NLS-1$
54
protected final static String JavaDoc TIMESTAMP = "timestamp"; //$NON-NLS-1$
55
protected final static String JavaDoc ACTIONS = "actions"; //$NON-NLS-1$
56
protected final static String JavaDoc ACTION = "action"; //$NON-NLS-1$
57
protected final static String JavaDoc TITLE = "title"; //$NON-NLS-1$
58
protected final static String JavaDoc PARAMS = "params"; //$NON-NLS-1$
59
protected final static String JavaDoc PARAM = "param"; //$NON-NLS-1$
60
protected final static String JavaDoc PARAM_NAME = "param-name"; //$NON-NLS-1$
61
protected final static String JavaDoc PARAM_VALUE = "param-value"; //$NON-NLS-1$
62
protected final static String JavaDoc USER_FILES = "user-files"; //$NON-NLS-1$
63
protected final static String JavaDoc MIMETYPE = "mimetype"; //$NON-NLS-1$
64
protected final static String JavaDoc SIZE = "size"; //$NON-NLS-1$
65

66     protected HttpServletRequest JavaDoc request;
67     protected HttpServletResponse JavaDoc response;
68     private static final Log logger = LogFactory.getLog(UserFilesComponent.class);
69
70     public Log getLogger() {
71         return logger;
72     }
73
74     public UserFilesComponent( ) {
75         super(null, null, null);
76         setXsl( "text/html", "UserFiles.xsl" ); //$NON-NLS-1$ //$NON-NLS-2$
77
}
78
79     public UserFilesComponent( IPentahoUrlFactory urlFactory, HttpServletRequest JavaDoc request,
80             HttpServletResponse JavaDoc response, List JavaDoc messages) {
81         super(urlFactory, messages, null);
82         this.request = request;
83         this.response = response;
84         setXsl( "text/html", "UserFiles.xsl" ); //$NON-NLS-1$ //$NON-NLS-2$
85
}
86
87     public void setRequest( HttpServletRequest JavaDoc request ) {
88             this.request = request;
89     }
90     
91     public void setResponse( HttpServletResponse JavaDoc response ) {
92             this.response = response;
93     }
94     
95     public boolean validate() {
96         return true;
97     }
98
99     public Document getXmlContent() {
100      
101         Document result = DocumentHelper.createDocument();
102         Element root = result.addElement( USER_FILES );
103         
104         addFiles( root );
105         return result;
106     }
107
108     protected void addFiles( Element root ) {
109         
110             addScheduledAndExecuting( root );
111             
112             addExecutedlist( root );
113                     
114     }
115     
116     protected void addScheduledAndExecuting( Element root ) {
117             Element jobs = root.addElement( "scheduled" ); //$NON-NLS-1$
118
IPentahoSession session = getSession();
119         IBackgroundExecution backgroundExecution = PentahoSystem.getBackgroundExecutionHandler(session);
120         List JavaDoc jobsList = null;
121             if (backgroundExecution != null) {
122           jobsList = backgroundExecution.getScheduledAndExecutingBackgroundJobs( session );
123         } else {
124           jobsList = new ArrayList JavaDoc();
125           Element errorRoot = root.addElement("error"); //$NON-NLS-1$
126
errorRoot.addElement("error-message").setText(Messages.getErrorString("UI.USER_ERROR_0003_NO_BACKGROUND_EXECUTION")); //$NON-NLS-1$//$NON-NLS-2$
127
}
128             if( jobsList != null && jobsList.size() > 0) {
129                 for (int i=0; i<jobsList.size(); i++) {
130
131                     Element job = jobs.addElement( FILE );
132                     
133                     JobDetail jobDetail = (JobDetail) jobsList.get(i);
134                     JobDataMap jobDataMap = jobDetail.getJobDataMap();
135
136                     job.addElement( NAME ).setText( jobDataMap.getString( IBackgroundExecution.BACKGROUND_ACTION_NAME_STR ) );
137                     job.addElement( TIMESTAMP ).setText( jobDataMap.getString( IBackgroundExecution.BACKGROUND_SUBMITTED ) );
138                     Element actions = job.addElement( ACTIONS );
139                     Element action = actions.addElement( ACTION );
140                     action.addElement( TITLE ).setText( Messages.getString( "UI.USER_CANCEL" ) ); //$NON-NLS-1$
141
Element params = action.addElement( PARAMS );
142                     Element param = params.addElement( PARAM );
143                     param.addElement( PARAM_NAME ).setText( "del-job-name" ); //$NON-NLS-1$
144
param.addElement( PARAM_VALUE ).setText( jobDetail.getName() );
145                     param = params.addElement( PARAM );
146                     param.addElement( PARAM_NAME ).setText( "del-job-group" ); //$NON-NLS-1$
147
param.addElement( PARAM_VALUE ).setText( jobDetail.getGroup() );
148                     param = params.addElement( PARAM );
149                     param.addElement( PARAM_NAME ).setText( "action" ); //$NON-NLS-1$
150
param.addElement( PARAM_VALUE ).setText( "cancel-job" ); //$NON-NLS-1$
151
}
152             }
153     }
154
155     protected void addExecutedlist( Element root ) {
156         Element jobs = root.addElement( "executed" ); //$NON-NLS-1$
157
IPentahoSession session = getSession();
158       IBackgroundExecution backgroundExecution = PentahoSystem.getBackgroundExecutionHandler(session);
159       
160         List JavaDoc pastExecutionList = null;
161       if (backgroundExecution != null) {
162         pastExecutionList = backgroundExecution.getBackgroundExecutedContentList(session);
163       } else {
164         pastExecutionList = new ArrayList JavaDoc();
165       }
166         SimpleDateFormat JavaDoc fmt = new SimpleDateFormat JavaDoc();
167   
168         for (int i=0; i<pastExecutionList.size(); i++) {
169             IContentItem item = (IContentItem) pastExecutionList.get(i);
170             Element job = jobs.addElement( FILE );
171   
172             job.addElement( NAME ).setText( item.getTitle() );
173             String JavaDoc dateStr = ""; //$NON-NLS-1$
174
Date JavaDoc time = item.getFileDateTime();
175             if( time != null ) {
176                 dateStr = fmt.format( time );
177             }
178             job.addElement( TIMESTAMP ).setText( dateStr );
179             job.addElement( MIMETYPE ).setText( item.getMimeType() );
180             job.addElement( SIZE ).setText( Long.toString( item.getFileSize() ) );
181   
182             Element actions = job.addElement( ACTIONS );
183             Element action = actions.addElement( ACTION );
184             action.addElement( TITLE ).setText( Messages.getString( "UI.USER_VIEW" ) ); //$NON-NLS-1$
185
Element params = action.addElement( PARAMS );
186             Element param = params.addElement( PARAM );
187             param.addElement( PARAM_NAME ).setText( "action" ); //$NON-NLS-1$
188
param.addElement( PARAM_VALUE ).setText( "view" ); //$NON-NLS-1$
189

190             param = params.addElement( PARAM );
191             param.addElement( PARAM_NAME ).setText( "id" ); //$NON-NLS-1$
192
param.addElement( PARAM_VALUE ).setText( item.getId() );
193   
194             action = actions.addElement( ACTION );
195             action.addElement( TITLE ).setText( Messages.getString( "UI.USER_DELETE" ) ); //$NON-NLS-1$
196
params = action.addElement( PARAMS );
197             param = params.addElement( PARAM );
198             param.addElement( PARAM_NAME ).setText( "action" ); //$NON-NLS-1$
199
param.addElement( PARAM_VALUE ).setText( "delete" ); //$NON-NLS-1$
200

201             param = params.addElement( PARAM );
202             param.addElement( PARAM_NAME ).setText( "content-id" ); //$NON-NLS-1$
203
param.addElement( PARAM_VALUE ).setText( item.getId() );
204             
205         }
206     }
207
208     public boolean cancelJob( String JavaDoc jobName, String JavaDoc jobGroup ) {
209         
210         try {
211             Scheduler sched = QuartzSystemListener.getSchedulerInstance();
212             sched.deleteJob(jobName, jobGroup);
213             return true;
214         } catch (Throwable JavaDoc t) {
215                 error( Messages.getErrorString( "Scheduler.ERROR_0001_SCHEDULER_CANNOT_CANCEL", t.getMessage() ), t ); //$NON-NLS-1$
216
}
217         return false;
218     }
219     
220     public boolean deleteContent( String JavaDoc contentId ) {
221             try {
222           IPentahoSession session = getSession();
223           IBackgroundExecution backgroundExecution = PentahoSystem.getBackgroundExecutionHandler(session);
224           if (backgroundExecution != null) {
225             backgroundExecution.removeBackgroundExecutedContentForID( contentId, session );
226             return true;
227           } else {
228             return false;
229           }
230             } catch (Throwable JavaDoc t) {
231                 error( Messages.getErrorString( "Scheduler.ERROR_0001_SCHEDULER_CANNOT_CANCEL", t.getMessage() ), t ); //$NON-NLS-1$
232
}
233             return false;
234     }
235     
236     
237 }
238
Popular Tags