1 21 package com.jaspersoft.jasperserver.api.engine.scheduling.hibernate; 22 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import org.springframework.orm.hibernate3.HibernateTemplate; 27 28 import com.jaspersoft.jasperserver.api.JSException; 29 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJob; 30 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobCalendarTrigger; 31 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobMailNotification; 32 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSimpleTrigger; 33 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSummary; 34 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobTrigger; 35 36 40 public class PersistentReportJob { 41 42 private long id; 43 private int version; 44 private String username; 45 private String label; 46 private String description; 47 private PersistentReportJobSource source; 48 private PersistentReportJobTrigger trigger; 49 private String baseOutputFilename; 50 private Set outputFormats; 51 private String outputLocale; 52 private PersistentReportJobMailNotification mailNotification; 53 private PersistentReportJobRepositoryDestination contentRepositoryDestination; 54 55 public PersistentReportJob() { 56 version = ReportJob.VERSION_NEW; 57 outputFormats = new HashSet (); 58 } 59 60 public long getId() { 61 return id; 62 } 63 64 public void setId(long id) { 65 this.id = id; 66 } 67 68 public int getVersion() { 69 return version; 70 } 71 72 public void setVersion(int version) { 73 this.version = version; 74 } 75 76 public PersistentReportJobSource getSource() { 77 return source; 78 } 79 80 public void setSource(PersistentReportJobSource source) { 81 this.source = source; 82 } 83 84 public PersistentReportJobTrigger getTrigger() { 85 return trigger; 86 } 87 88 public void setTrigger(PersistentReportJobTrigger trigger) { 89 this.trigger = trigger; 90 } 91 92 public PersistentReportJobMailNotification getMailNotification() { 93 return mailNotification; 94 } 95 96 public void setMailNotification(PersistentReportJobMailNotification mailNotification) { 97 this.mailNotification = mailNotification; 98 } 99 100 public void copyFrom(ReportJob job, HibernateTemplate hibernateTemplate) { 101 if (getVersion() != job.getVersion()) { 102 throw new JSException("The client version (" + job.getVersion() + ") does not match the latest version (" + getVersion() + ")."); 103 } 104 105 setLabel(job.getLabel()); 106 setDescription(job.getDescription()); 107 copySource(job); 108 copyTrigger(job, hibernateTemplate); 109 setBaseOutputFilename(job.getBaseOutputFilename()); 110 setOutputFormats(job.getOutputFormats() == null ? null : new HashSet (job.getOutputFormats())); 111 setOutputLocale(job.getOutputLocale()); 112 copyContentRepostoryDestination(job); 113 copyMailNotification(job, hibernateTemplate); 114 } 115 116 protected void copySource(ReportJob job) { 117 if (getSource() == null) { 118 setSource(new PersistentReportJobSource()); 119 } 120 getSource().copyFrom(job.getSource()); 121 } 122 123 protected void copyTrigger(ReportJob job, HibernateTemplate hibernateTemplate) { 124 ReportJobTrigger jobTrigger = job.getTrigger(); 125 PersistentReportJobTrigger persistentTrigger = getTrigger(); 126 if (persistentTrigger != null && !persistentTrigger.supports(jobTrigger.getClass())) { 127 hibernateTemplate.delete(persistentTrigger); 128 persistentTrigger = null; 129 } 130 131 if (persistentTrigger == null) { 132 if (jobTrigger instanceof ReportJobSimpleTrigger) { 133 persistentTrigger = new PersistentReportJobSimpleTrigger(); 134 } else if (jobTrigger instanceof ReportJobCalendarTrigger) { 135 persistentTrigger = new PersistentReportJobCalendarTrigger(); 136 } else { 137 throw new JSException("Unknow report job trigger type \"" + jobTrigger.getClass().getName() + "\""); 138 } 139 setTrigger(persistentTrigger); 140 } 141 142 persistentTrigger.copyFrom(jobTrigger); 143 } 144 145 protected void copyContentRepostoryDestination(ReportJob job) { 146 if (getContentRepositoryDestination() == null) { 147 setContentRepositoryDestination(new PersistentReportJobRepositoryDestination()); 148 } 149 getContentRepositoryDestination().copyFrom(job.getContentRepositoryDestination()); 150 } 151 152 protected void copyMailNotification(ReportJob job, HibernateTemplate hibernateTemplate) { 153 ReportJobMailNotification jobMail = job.getMailNotification(); 154 PersistentReportJobMailNotification persistentMail = getMailNotification(); 155 if (jobMail == null) { 156 if (persistentMail != null) { 157 hibernateTemplate.delete(persistentMail); 158 setContentRepositoryDestination(null); 159 } 160 } else { 161 if (persistentMail == null) { 162 persistentMail = new PersistentReportJobMailNotification(); 163 setMailNotification(persistentMail); 164 } 165 persistentMail.copyFrom(jobMail); 166 } 167 } 168 169 public ReportJob toClient() { 170 ReportJob job = new ReportJob(); 171 job.setId(getId()); 172 job.setVersion(getVersion()); 173 job.setUsername(getUsername()); 174 job.setLabel(getLabel()); 175 job.setDescription(getDescription()); 176 job.setSource(getSource().toClient()); 177 job.setTrigger(getTrigger().toClient()); 178 job.setBaseOutputFilename(getBaseOutputFilename()); 179 job.setOutputFormats(getOutputFormats() == null ? null : new HashSet (getOutputFormats())); 180 job.setOutputLocale(getOutputLocale()); 181 job.setContentRepositoryDestination(getContentRepositoryDestination().toClient()); 182 job.setMailNotification(getMailNotification() == null ? null : getMailNotification().toClient()); 183 return job; 184 } 185 186 public ReportJobSummary toSummary() { 187 ReportJobSummary job = new ReportJobSummary(); 188 job.setId(getId()); 189 job.setVersion(getVersion()); 190 job.setUsername(getUsername()); 191 job.setLabel(getLabel()); 192 return job; 193 } 194 195 public PersistentReportJobRepositoryDestination getContentRepositoryDestination() { 196 return contentRepositoryDestination; 197 } 198 199 public void setContentRepositoryDestination( 200 PersistentReportJobRepositoryDestination contentRepositoryDestination) { 201 this.contentRepositoryDestination = contentRepositoryDestination; 202 } 203 204 public String getDescription() { 205 return description; 206 } 207 208 public void setDescription(String description) { 209 this.description = description; 210 } 211 212 public String getLabel() { 213 return label; 214 } 215 216 public void setLabel(String label) { 217 this.label = label; 218 } 219 220 public String getBaseOutputFilename() { 221 return baseOutputFilename; 222 } 223 224 public void setBaseOutputFilename(String baseOutputFilename) { 225 this.baseOutputFilename = baseOutputFilename; 226 } 227 228 public Set getOutputFormats() { 229 return outputFormats; 230 } 231 232 public void setOutputFormats(Set outputFormats) { 233 this.outputFormats = outputFormats; 234 } 235 236 public boolean isNew() { 237 return getVersion() == ReportJob.VERSION_NEW; 238 } 239 240 public void cascadeSave(HibernateTemplate hibernateTemplate) { 241 if (getTrigger().isNew()) { 242 hibernateTemplate.save(getTrigger()); 243 } 244 if (getContentRepositoryDestination() != null && getContentRepositoryDestination().isNew()) { 245 hibernateTemplate.save(getContentRepositoryDestination()); 246 } 247 if (getMailNotification() != null && getMailNotification().isNew()) { 248 hibernateTemplate.save(getMailNotification()); 249 } 250 } 251 252 public void delete(HibernateTemplate hibernateTemplate) { 253 hibernateTemplate.delete(this); 254 hibernateTemplate.delete(getTrigger()); 255 if (getContentRepositoryDestination() != null) { 256 hibernateTemplate.delete(getContentRepositoryDestination()); 257 } 258 if (getMailNotification() != null) { 259 hibernateTemplate.delete(getMailNotification()); 260 } 261 } 262 263 public String getUsername() { 264 return username; 265 } 266 267 public void setUsername(String username) { 268 this.username = username; 269 } 270 271 public String getOutputLocale() { 272 return outputLocale; 273 } 274 275 public void setOutputLocale(String outputLocale) { 276 this.outputLocale = outputLocale; 277 } 278 279 } 280 | Popular Tags |