1 13 package org.pentaho.plugin.quartz; 14 15 import java.text.SimpleDateFormat ; 16 import java.util.ArrayList ; 17 import java.util.Date ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.pentaho.core.repository.IContentItem; 22 import org.pentaho.core.repository.IContentRepository; 23 import org.pentaho.core.repository.ISolutionRepository; 24 import org.pentaho.core.repository.content.ContentRepositoryOutputHandler; 25 import org.pentaho.core.runtime.IBackgroundExecution; 26 import org.pentaho.core.session.IPentahoSession; 27 import org.pentaho.core.session.UserSession; 28 import org.pentaho.core.solution.IActionSequence; 29 import org.pentaho.core.solution.IOutputHandler; 30 import org.pentaho.core.solution.IParameterProvider; 31 import org.pentaho.core.system.PentahoSystem; 32 import org.pentaho.messages.Messages; 33 import org.pentaho.repository.HibernateUtil; 34 import org.pentaho.repository.content.ContentRepository; 35 import org.pentaho.util.UUIDUtil; 36 import org.pentaho.util.logging.Logger; 37 import org.quartz.JobDataMap; 38 import org.quartz.JobDetail; 39 import org.quartz.JobExecutionContext; 40 import org.quartz.JobExecutionException; 41 import org.quartz.JobListener; 42 import org.quartz.Scheduler; 43 import org.quartz.SimpleTrigger; 44 import org.quartz.Trigger; 45 46 public class QuartzBackgroundExecutionHelper implements IBackgroundExecution { 47 48 public static final String DEFAULT_JOB_NAME = "bgExecution"; public static final String DEFAULT_TRIGGER_NAME = "bgTrigger"; public static final String DEFAULT_BACKGROUND_LOCATION = "background"; public static final String BACKGROUND_USER_NAME_STR = "background_user_name"; public static final String BACKGROUND_CONTENT_GUID_STR = "background_output_content_guid"; public static final String BACKGROUND_CONTENT_LOCATION_STR = "background_output_location"; public static final String BACKGROUND_CONTENT_COOKIE_PREFIX = "pentaho_background_content"; public static final String BACKGROUND_EXECUTION_FLAG = "backgroundExecution"; 57 58 63 64 public String backgroundExecuteAction(IPentahoSession userSession, IParameterProvider parameterProvider) { 65 try { 66 Scheduler sched = QuartzSystemListener.getSchedulerInstance(); 67 68 String solutionName = parameterProvider.getStringParameter("solution", null); String actionPath = parameterProvider.getStringParameter("path", null); String actionName = parameterProvider.getStringParameter("action", null); String userName = userSession.isAuthenticated() ? userSession.getName() : IBackgroundExecution.DEFAULT_USER_NAME; 72 String jobGroup = userName; 73 74 String outputContentGUID = UUIDUtil.getUUIDAsString(); 75 76 JobDetail jobDetail = createDetailFromParameterProvider(parameterProvider, 77 userSession, 78 outputContentGUID, 79 solutionName, 80 actionPath, 81 actionName, 82 jobGroup); 83 84 trackBackgroundExecution(userSession, outputContentGUID); 85 86 BackgroundExecuteListener listener = new BackgroundExecuteListener(userSession, outputContentGUID, sched, jobDetail.getName()); 87 sched.addJobListener(listener); 88 jobDetail.addJobListener(listener.getName()); 89 90 Trigger bgTrigger = new SimpleTrigger(outputContentGUID, jobGroup, new Date ()); 91 93 sched.scheduleJob(jobDetail, bgTrigger); 94 return Messages.getString("BackgroundExecuteHelper.USER_JOB_SUBMITTED", "UserContent", "if(window.opener) {window.opener.location.href='UserContent'; window.close() } else { return true; }" ); } catch (Exception ex) { 97 return ex.getLocalizedMessage(); 98 } 99 } 100 101 public void trackBackgroundExecution(IPentahoSession userSession, String GUID) { 102 IContentRepository repo = ContentRepository.getInstance(userSession); 103 repo.newBackgroundExecutedContentId(userSession, GUID); 104 } 105 106 public IContentItem getBackgroundContent(String contentGUID, IPentahoSession userSession) { 107 IContentRepository repo = ContentRepository.getInstance(userSession); 108 try { 109 IContentItem item = repo.getContentItemById(contentGUID); 110 return item; 111 } catch (Exception ex) { 112 Logger.error(this.getClass().getName(), ex.getLocalizedMessage(), ex); 113 } 114 return null; 115 } 116 117 public List getScheduledAndExecutingBackgroundJobs(IPentahoSession userSession) { 118 try { 119 Scheduler sched = QuartzSystemListener.getSchedulerInstance(); 120 String userName = userSession.isAuthenticated() ? userSession.getName() : IBackgroundExecution.DEFAULT_USER_NAME; 121 String [] jobNames = sched.getJobNames(userName); 122 List rtn = new ArrayList (); 123 if (jobNames != null) { 124 for (int i=0; i<jobNames.length; i++) { 125 JobDetail jobDetail = sched.getJobDetail( jobNames[i], userName ); 126 rtn.add(jobDetail); 127 } 128 } 129 return rtn; 130 } catch (Exception ex) { 131 ex.printStackTrace(); 133 } 134 return new ArrayList (); 135 } 136 137 138 public void removeBackgroundExecutedContentForID(String contentGUID, IPentahoSession userSession) { 139 140 IContentRepository repo = ContentRepository.getInstance(userSession); 142 try { 143 IContentItem item = repo.getContentItemById(contentGUID); 144 if (item != null) { 145 item.makeTransient(); 146 } else { 147 return; 148 } 149 } finally { 150 HibernateUtil.commitTransaction(); 151 } 152 repo.removeBackgroundExecutedContentId(userSession, contentGUID); 153 } 154 155 public List getBackgroundExecutedContentList(IPentahoSession userSession) { 156 IContentRepository repo = ContentRepository.getInstance(userSession); 157 ArrayList idList = new ArrayList (); 158 List idObjectList = repo.getBackgroundExecutedContentItemsForUser(userSession); 159 if (idObjectList != null) { 160 IContentItem contentItem = null; 161 for (int i=0; i<idObjectList.size(); i++) { 162 contentItem = (IContentItem)idObjectList.get(i); 163 idList.add(contentItem); 164 } 165 } 166 return idList; 167 } 168 169 protected JobDetail createDetailFromParameterProvider(IParameterProvider parameterProvider, 171 IPentahoSession userSession, 172 String outputContentGUID, 173 String solutionName, 174 String actionPath, 175 String actionName, 176 String jobGroup) { 177 String userName = userSession.isAuthenticated() ? userSession.getName() : IBackgroundExecution.DEFAULT_USER_NAME; 178 179 SimpleDateFormat fmt = new SimpleDateFormat (); 180 String completeAction = solutionName + "/" + actionPath + "/" + actionName; String jobName = completeAction + "/" + outputContentGUID; JobDetail jobDetail = new JobDetail(jobName, jobGroup, QuartzExecute.class); 183 JobDataMap data = jobDetail.getJobDataMap(); 184 Iterator inputNamesIterator = parameterProvider.getParameterNames(); 185 String outputLocationGUID = UUIDUtil.getUUIDAsString(); 186 while (inputNamesIterator.hasNext()) { 187 String inputName = (String ) inputNamesIterator.next(); 188 Object inputValue = parameterProvider.getParameter(inputName); 189 data.put(inputName, inputValue); 190 } 191 ISolutionRepository repo = PentahoSystem.getSolutionRepository( userSession ); 192 IActionSequence action = repo.getActionSequence( solutionName, actionPath, actionName, repo.getLoggingLevel(), ISolutionRepository.ACTION_EXECUTE ); 193 data.put( BACKGROUND_ACTION_NAME_STR, action.getTitle() ); 194 data.put("processId", this.getClass().getName()); data.put(BACKGROUND_USER_NAME_STR, userName); 196 data.put(BACKGROUND_CONTENT_GUID_STR, outputContentGUID); 197 data.put(BACKGROUND_CONTENT_LOCATION_STR, DEFAULT_BACKGROUND_LOCATION + "/" + outputLocationGUID); data.put(BACKGROUND_SUBMITTED, fmt.format( new Date () ) ); 199 200 data.put( BACKGROUND_EXECUTION_FLAG, "true" ); 204 return jobDetail; 205 206 } 207 208 public IPentahoSession getEffectiveUserSession(String user) { 209 UserSession us = new UserSession(user, null, true); 210 return us; 211 } 212 213 public static class BackgroundExecuteListener implements JobListener { 214 215 private IPentahoSession userSession; 216 private String contentGUID; 217 private Scheduler sched; 218 private String jobName; 219 220 public BackgroundExecuteListener(IPentahoSession session, String contentGUID, Scheduler scheduler, String jobName) { 221 userSession = session; 222 this.contentGUID = contentGUID; 223 this.jobName = jobName; 224 } 225 226 public String getName() { 227 return contentGUID; 228 } 229 230 public void jobExecutionVetoed(JobExecutionContext context) { 231 233 } 234 235 public void jobToBeExecuted(JobExecutionContext context) { 236 238 } 239 240 public void jobWasExecuted(JobExecutionContext context, JobExecutionException exception) { 241 JobDetail ctxDetail = context.getJobDetail(); 244 if ( (ctxDetail != null) && (ctxDetail.getName().equals(this.jobName) ) ) { Object contentItemGUID = context.get(BACKGROUND_CONTENT_GUID_STR); 246 if (contentItemGUID != null && userSession != null) { 247 userSession.setBackgroundExecutionAlert(); } else { 249 Logger.warn(this.getClass().getName(), Messages.getString("BackgroundExecuteHelper.WARN_CONTENT_ITEM_NOT_CREATED" )); } 251 this.userSession = null; try { 253 sched.removeJobListener(this.getName()); 254 } catch (Exception ex) { 255 } 257 } 258 } 259 260 } 261 262 public IOutputHandler getContentOutputHandler(String location, String fileName, String solutionName, IPentahoSession userSession, IParameterProvider parameterProvider) { 263 return new ContentRepositoryOutputHandler(location, fileName, solutionName, userSession); 264 } 265 266 } 267 | Popular Tags |