NewScheduler.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /**
  2. Licensed Materials - Property of IBM
  3. IBM Cognos Products: DOCS
  4. (C) Copyright IBM Corp. 2005, 2008
  5. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  6. IBM Corp.
  7. */
  8. /**
  9. * NewScheduler.java
  10. *
  11. * Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  12. * Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  13. *
  14. */
  15. import java.math.BigInteger;
  16. import java.util.Calendar;
  17. import com.cognos.developer.schemas.bibus._3.Account;
  18. import com.cognos.developer.schemas.bibus._3.AddOptions;
  19. import com.cognos.developer.schemas.bibus._3.AddressSMTP;
  20. import com.cognos.developer.schemas.bibus._3.BaseClass;
  21. import com.cognos.developer.schemas.bibus._3.BaseClassArrayProp;
  22. import com.cognos.developer.schemas.bibus._3.BaseParameter;
  23. import com.cognos.developer.schemas.bibus._3.BooleanProp;
  24. import com.cognos.developer.schemas.bibus._3.Credential;
  25. import com.cognos.developer.schemas.bibus._3.DateTimeProp;
  26. import com.cognos.developer.schemas.bibus._3.DeleteOptions;
  27. import com.cognos.developer.schemas.bibus._3.DeliveryOptionAddressSMTPArray;
  28. import com.cognos.developer.schemas.bibus._3.DeliveryOptionEnum;
  29. import com.cognos.developer.schemas.bibus._3.DeliveryOptionMemoPart;
  30. import com.cognos.developer.schemas.bibus._3.DeliveryOptionString;
  31. import com.cognos.developer.schemas.bibus._3.MemoPartString;
  32. import com.cognos.developer.schemas.bibus._3.MultilingualToken;
  33. import com.cognos.developer.schemas.bibus._3.NmtokenProp;
  34. import com.cognos.developer.schemas.bibus._3.Option;
  35. import com.cognos.developer.schemas.bibus._3.OptionArrayProp;
  36. import com.cognos.developer.schemas.bibus._3.ParameterValue;
  37. import com.cognos.developer.schemas.bibus._3.ParameterValueArrayProp;
  38. import com.cognos.developer.schemas.bibus._3.PositiveIntegerProp;
  39. import com.cognos.developer.schemas.bibus._3.PropEnum;
  40. import com.cognos.developer.schemas.bibus._3.QueryOptions;
  41. import com.cognos.developer.schemas.bibus._3.ReportSaveAsEnum;
  42. import com.cognos.developer.schemas.bibus._3.RunOptionBoolean;
  43. import com.cognos.developer.schemas.bibus._3.RunOptionEnum;
  44. import com.cognos.developer.schemas.bibus._3.RunOptionSaveAs;
  45. import com.cognos.developer.schemas.bibus._3.RunOptionString;
  46. import com.cognos.developer.schemas.bibus._3.RunOptionStringArray;
  47. import com.cognos.developer.schemas.bibus._3.Schedule;
  48. import com.cognos.developer.schemas.bibus._3.SearchPathMultipleObject;
  49. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  50. import com.cognos.developer.schemas.bibus._3.SmtpContentDispositionEnum;
  51. import com.cognos.developer.schemas.bibus._3.Sort;
  52. import com.cognos.developer.schemas.bibus._3.StringProp;
  53. import com.cognos.developer.schemas.bibus._3.UpdateActionEnum;
  54. import com.cognos.developer.schemas.bibus._3.UpdateOptions;
  55. public class NewScheduler extends BaseClass
  56. {
  57. private Schedule newSchedule = new Schedule();
  58. NmtokenProp howOften = new NmtokenProp();
  59. /**
  60. * Set and start a schedule for a report.
  61. *
  62. *@param connection Connection to Server
  63. *@param reportPath Search path to a report.
  64. *@param outFormat CSV,HTML,PDF,HTMLFragment,XHTML,XML. Default is HTML
  65. *@param pageOrientation landscape or portrait. Default is portrait
  66. *@param paper Letter, Legal, 11x17, A3, A4, B4 JIS, B5,JIS (all pageDefinition objects).
  67. *@param delivery save, saveas, print, email
  68. *@param printerName as "CAMID(\":\")/printer[@name='default_printer']"
  69. *@param saveasName
  70. *@param emails An AddressSMTP array of all the e-mail addresses
  71. *@param weekDay weekdays in small letters sunday
  72. *@param monthlyStartDate The start date in the month, as a String
  73. *@param startMonth month with small letters (i.e. june)
  74. *@param relativeWeekValue
  75. *@param relativeDayValue
  76. *@param runPeriod minute, hour, day if dailyMonthlyYearly is daily
  77. *@param runFreq every n periods
  78. *@param dailyMonthlyYearly ByDay, ByWeek, ByMonth, ByYear
  79. *@param startOnDate If start date not passed set to today/now
  80. *@param endOnDate If end date not passed endOnTime must be indefinite
  81. *@param endOnTime Parameter endOnTime cannot be null! Options: indefinite or onDate
  82. */
  83. public String setSchedule(
  84. CRNConnect connection,
  85. BaseClassWrapper report,
  86. String outFormat,
  87. String pageOrientation,
  88. String paper,
  89. String delivery,
  90. String printerName,
  91. String saveasName,
  92. AddressSMTP[] emails,
  93. int weekDay,
  94. String monthlyStartDate,
  95. String startMonth,
  96. String relativeWeekValue,
  97. String relativeDayValue,
  98. String runPeriod,
  99. String runFreq,
  100. String dailyMonthlyYearly,
  101. Calendar startOnDate,
  102. Calendar endOnDate,
  103. String endOnTime)
  104. throws java.rmi.RemoteException
  105. {
  106. // sn_dg_prm_smpl_schedulereport_P5_start_0
  107. OptionArrayProp roap = new OptionArrayProp();
  108. String reportPath = report.getBaseClassObject().getSearchPath().getValue();
  109. //Set options
  110. roap.setValue(
  111. this.setSchedulerOptions(
  112. outFormat,
  113. delivery,
  114. reportPath,
  115. printerName,
  116. saveasName,
  117. pageOrientation,
  118. paper,
  119. emails,
  120. connection));
  121. // sn_dg_prm_smpl_schedulereport_P5_end_0
  122. BooleanProp isActive = new BooleanProp();
  123. isActive.setValue(true);
  124. setFrequency(runFreq);
  125. if (dailyMonthlyYearly.trim() == "ByDay")
  126. {
  127. setFrequencyByDay(runPeriod, runFreq);
  128. }
  129. else if (dailyMonthlyYearly.trim() == "ByWeek")
  130. {
  131. setFrequencyByWeek(weekDay);
  132. }
  133. else if (dailyMonthlyYearly.trim() == "ByMonth")
  134. {
  135. if (monthlyStartDate != null)
  136. {
  137. setFrequencyByAbsoluteMonth(monthlyStartDate);
  138. }
  139. else
  140. {
  141. setFrequencyByRelativeMonth(
  142. relativeWeekValue,
  143. relativeDayValue);
  144. }
  145. }
  146. else if (dailyMonthlyYearly.trim() == "ByYear")
  147. {
  148. if (monthlyStartDate != null)
  149. {
  150. setFrequencyByAbsoluteYear(monthlyStartDate, startMonth);
  151. }
  152. else
  153. {
  154. setFrequencyByRelativeYear(
  155. relativeWeekValue,
  156. relativeDayValue,
  157. startMonth);
  158. }
  159. }
  160. else
  161. {
  162. System.out.println("Period is not correct.");
  163. }
  164. //Assign current user as the owner
  165. // sn_dg_prm_smpl_schedulereport_P4_start_0
  166. BaseClass[] owners = new BaseClass[] { Logon.getLogonAccount(connection) };
  167. BaseClassArrayProp ownersProp = new BaseClassArrayProp();
  168. ownersProp.setValue(owners);
  169. // sn_dg_prm_smpl_schedulereport_P4_end_0
  170. //
  171. //Make sure the account that is currently logged in has a credential,
  172. //then get a reference to the account credential
  173. // sn_dg_prm_smpl_schedulereport_P1_start_0
  174. if (! Credentials.hasCredential(connection))
  175. {
  176. Credentials newCred = new Credentials();
  177. newCred.addCredential(connection);
  178. }
  179. Account logonInfo = Logon.getLogonAccount(connection);
  180. Credential credential = new Credential();
  181. StringProp credentialPath = new StringProp();
  182. String credentialPathString = logonInfo.getSearchPath().getValue();
  183. credentialPathString = credentialPathString + "/credential[@name='Credential']";
  184. credentialPath.setValue(credentialPathString);
  185. credential.setSearchPath(credentialPath);
  186. BaseClassArrayProp credentials = new BaseClassArrayProp();
  187. credentials.setValue(new BaseClass[] {credential} );
  188. // sn_dg_prm_smpl_schedulereport_P1_end_0
  189. //
  190. //Set parameter values required for the report, if any
  191. ParameterValueArrayProp pv = new ParameterValueArrayProp();
  192. ParameterValue reportParameters[] = new ParameterValue[] {};
  193. ReportParameters repParms = new ReportParameters();
  194. BaseParameter[] prm = repParms.getReportParameters(
  195. report,
  196. connection);
  197. if (prm != null && prm.length > 0)
  198. {
  199. reportParameters =
  200. ReportParameters.setReportParameters(prm);
  201. }
  202. pv.setValue(reportParameters);
  203. //Set the remaining properties of the new Schedule object
  204. // sn_dg_prm_smpl_schedulereport_P6_start_0
  205. newSchedule.setActive(isActive);
  206. newSchedule.setCredential(credentials);
  207. newSchedule.setEndDate(this.setScheduleEndDate(endOnDate, endOnTime));
  208. newSchedule.setEndType(this.setScheduleEndTime(endOnTime));
  209. newSchedule.setOwner(ownersProp);
  210. newSchedule.setParameters(pv);
  211. newSchedule.setOptions(roap);
  212. newSchedule.setStartDate(this.setScheduleStartDate(startOnDate));
  213. newSchedule.setType(howOften);
  214. // add the schedule to the report
  215. AddOptions ao = new AddOptions();
  216. ao.setUpdateAction(UpdateActionEnum.replace);
  217. BaseClass newBc = connection.getCMService().add(
  218. new SearchPathSingleObject(reportPath),
  219. new BaseClass[] { newSchedule },
  220. ao)[0];
  221. // sn_dg_prm_smpl_schedulereport_P6_end_0
  222. if (newBc != null)
  223. {
  224. return ("Schedule Created");
  225. }
  226. else
  227. {
  228. return ("Schedule NOT Created");
  229. }
  230. }
  231. public void setFrequency(String runFreq)
  232. {
  233. PositiveIntegerProp freq = new PositiveIntegerProp();
  234. freq.setValue(new BigInteger(runFreq));
  235. newSchedule.setEveryNPeriods(freq);
  236. }
  237. public void setFrequencyByDay(String runPeriod, String runFreq)
  238. {
  239. howOften.setValue("daily");
  240. NmtokenProp period = new NmtokenProp();
  241. period.setValue(runPeriod);
  242. PositiveIntegerProp runFreqProp = new PositiveIntegerProp();
  243. runFreqProp.setValue(new BigInteger(runFreq));
  244. newSchedule.setDailyPeriod(period);
  245. newSchedule.setEveryNPeriods(runFreqProp);
  246. }
  247. public void setFrequencyByWeek(int weekDay)
  248. {
  249. howOften.setValue("weekly");
  250. BooleanProp dayOfTheWeek = new BooleanProp();
  251. dayOfTheWeek.setValue(true);
  252. //This will set only one day of the week
  253. //If you want more than one, use if statement
  254. switch (weekDay)
  255. {
  256. case 1 :
  257. newSchedule.setWeeklySunday(dayOfTheWeek);
  258. break;
  259. case 2 :
  260. newSchedule.setWeeklyMonday(dayOfTheWeek);
  261. break;
  262. case 3 :
  263. newSchedule.setWeeklyTuesday(dayOfTheWeek);
  264. break;
  265. case 4 :
  266. newSchedule.setWeeklyWednesday(dayOfTheWeek);
  267. break;
  268. case 5 :
  269. newSchedule.setWeeklyThursday(dayOfTheWeek);
  270. break;
  271. case 6 :
  272. newSchedule.setWeeklyFriday(dayOfTheWeek);
  273. break;
  274. case 7 :
  275. newSchedule.setWeeklySaturday(dayOfTheWeek);
  276. break;
  277. }
  278. }
  279. public void setFrequencyByAbsoluteMonth(String monthlyStartDate)
  280. {
  281. howOften.setValue("monthlyAbsolute");
  282. this.m_setMonthlyAbsoluteDay(monthlyStartDate);
  283. }
  284. public void setFrequencyByRelativeMonth(
  285. String relativeWeekValue,
  286. String relativeDayValue)
  287. {
  288. howOften.setValue("monthlyRelative");
  289. this.m_setMonthlyRelativeDay(relativeDayValue);
  290. this.m_setMonthlyRelativeWeek(relativeWeekValue);
  291. }
  292. public void setFrequencyByAbsoluteYear(
  293. String monthlyStartDate,
  294. String startMonth)
  295. {
  296. howOften.setValue("yearlyAbsolute");
  297. this.m_setYearlyAbsoluteMonth(startMonth);
  298. this.m_setYearlyAbsoluteDay(monthlyStartDate);
  299. }
  300. public void setFrequencyByRelativeYear(
  301. String relativeWeekValue,
  302. String relativeDayValue,
  303. String startMonth)
  304. {
  305. howOften.setValue("yearlyRelative");
  306. this.m_setYearlyRelativeDay(relativeDayValue);
  307. this.m_setYearlyRelativeWeek(relativeWeekValue);
  308. this.m_setYearlyRelativeMonth(startMonth);
  309. }
  310. public Option[] setSchedulerOptions(
  311. String outFormat,
  312. String delivery,
  313. String reportPath,
  314. String printerName,
  315. String saveasName,
  316. String pageOrientation,
  317. String paper,
  318. AddressSMTP[] emails,
  319. CRNConnect connection)
  320. {
  321. //Declare run options
  322. RunOptionStringArray format = new RunOptionStringArray();
  323. RunOptionBoolean prompt = new RunOptionBoolean();
  324. int numberRunOptions = 3; //default run options
  325. if (outFormat.compareToIgnoreCase("PDF") == 0)
  326. {
  327. //one more each for orientation and paper size
  328. numberRunOptions = numberRunOptions + 2;
  329. }
  330. if (delivery.compareToIgnoreCase("email") == 0)
  331. {
  332. //one more each for e-mail addresses, subject and body
  333. numberRunOptions = numberRunOptions + 4;
  334. }
  335. else if (delivery.compareToIgnoreCase("print") == 0)
  336. {
  337. //this one is for the printer
  338. numberRunOptions++;
  339. }
  340. Option ro[] = new Option[numberRunOptions];
  341. //Set option to replace any existing output
  342. //Don't prompt for values
  343. prompt.setName(RunOptionEnum.prompt);
  344. prompt.setValue(false);
  345. ro[--numberRunOptions] = prompt;
  346. if (delivery.compareToIgnoreCase("saveas") == 0)
  347. {
  348. //If saveAs name not provided a default will be used
  349. ro[--numberRunOptions] =
  350. setDeliveryMethodSaveAs(connection, saveasName, reportPath);
  351. }
  352. else if (delivery.compareToIgnoreCase("save") == 0)
  353. {
  354. ro[--numberRunOptions] = setDeliveryMethodSave();
  355. }
  356. else if (delivery.compareToIgnoreCase("print") == 0)
  357. {
  358. ro[--numberRunOptions] = setDeliveryMethodPrint();
  359. ro[--numberRunOptions] = setPrinter(printerName);
  360. }
  361. else if (delivery.compareToIgnoreCase("email") == 0)
  362. {
  363. ro[--numberRunOptions] = setDeliveryMethodEmail();
  364. ro[--numberRunOptions] = setEmailAddresses(emails);
  365. ro[--numberRunOptions] = setEmailBody();
  366. ro[--numberRunOptions] = setEmailSubject();
  367. ro[--numberRunOptions] = setEmailAttach();
  368. }
  369. //set the output format to outFormat
  370. format.setName(RunOptionEnum.outputFormat);
  371. format.setValue(new String[] { outFormat });
  372. ro[--numberRunOptions] = format;
  373. //If outFormat is PDF set page orientation and paper size
  374. if (outFormat.compareToIgnoreCase("PDF") == 0)
  375. {
  376. ro[--numberRunOptions] = setOrientation(pageOrientation);
  377. ro[--numberRunOptions] = setPaperSize(paper);
  378. }
  379. return ro;
  380. }
  381. // public NmtokenProp m_setMonthlyRelativeDay(String relativeDayValue)
  382. public void m_setMonthlyRelativeDay(String relativeDayValue)
  383. {
  384. NmtokenProp relativeDay = new NmtokenProp();
  385. relativeDay.setValue(relativeDayValue); //"sunday"
  386. newSchedule.setMonthlyRelativeDay(relativeDay);
  387. }
  388. public void m_setMonthlyRelativeWeek(String relativeWeekValue)
  389. {
  390. NmtokenProp relativeWeek = new NmtokenProp();
  391. relativeWeek.setValue(relativeWeekValue);
  392. newSchedule.setMonthlyRelativeWeek(relativeWeek);
  393. }
  394. public void m_setMonthlyAbsoluteDay(String monthlyStartDate)
  395. {
  396. PositiveIntegerProp absoluteDay = new PositiveIntegerProp();
  397. absoluteDay.setValue(new BigInteger(monthlyStartDate));
  398. //This method should be called ONLY for "ByMonth"
  399. newSchedule.setMonthlyAbsoluteDay(absoluteDay);
  400. }
  401. public void m_setYearlyAbsoluteDay(String monthlyStartDate)
  402. {
  403. PositiveIntegerProp absoluteDay = new PositiveIntegerProp();
  404. absoluteDay.setValue(new BigInteger(monthlyStartDate));
  405. //This method should be called ONLY for "ByYear"
  406. newSchedule.setYearlyAbsoluteDay(absoluteDay);
  407. }
  408. public void m_setYearlyAbsoluteMonth(String startMonth)
  409. {
  410. NmtokenProp absoluteMonth = new NmtokenProp();
  411. absoluteMonth.setValue(startMonth.toLowerCase().trim());
  412. newSchedule.setYearlyAbsoluteMonth(absoluteMonth);
  413. }
  414. public void m_setYearlyRelativeMonth(String startMonth)
  415. {
  416. NmtokenProp relativeMonth = new NmtokenProp();
  417. relativeMonth.setValue(startMonth.toLowerCase().trim());
  418. newSchedule.setYearlyRelativeMonth(relativeMonth);
  419. }
  420. public void m_setYearlyRelativeDay(String relativeDayValue)
  421. {
  422. NmtokenProp relativeDay = new NmtokenProp();
  423. relativeDay.setValue(relativeDayValue); //"sunday"
  424. newSchedule.setYearlyRelativeDay(relativeDay);
  425. }
  426. public void m_setYearlyRelativeWeek(String relativeWeekValue)
  427. {
  428. NmtokenProp relativeWeek = new NmtokenProp();
  429. relativeWeek.setValue(relativeWeekValue); //"first"
  430. newSchedule.setYearlyRelativeWeek(relativeWeek);
  431. }
  432. // sn_dg_prm_smpl_schedulereport_P2_start_0
  433. public DateTimeProp setScheduleStartDate(Calendar startOnDate)
  434. {
  435. DateTimeProp startDate = new DateTimeProp();
  436. //If start date not passed set to today/now
  437. if (startOnDate != null)
  438. startDate.setValue(startOnDate);
  439. else
  440. startDate.setValue(Calendar.getInstance());
  441. return startDate;
  442. }
  443. // sn_dg_prm_smpl_schedulereport_P2_end_0
  444. // sn_dg_prm_smpl_schedulereport_P3_start_0
  445. public DateTimeProp setScheduleEndDate(
  446. Calendar endOnDate,
  447. String endOnTime)
  448. {
  449. DateTimeProp endDate = new DateTimeProp();
  450. if (endOnTime.compareToIgnoreCase("onDate") == 0)
  451. if (endOnDate != null)
  452. {
  453. endDate.setValue(endOnDate);
  454. }
  455. else
  456. System.out.println(
  457. "Parameter endOnTime cannot be onDate if no enddate provided");
  458. return endDate;
  459. }
  460. public NmtokenProp setScheduleEndTime(String endOnTime)
  461. {
  462. NmtokenProp endTime = new NmtokenProp();
  463. if (endOnTime != null)
  464. {
  465. endTime.setValue(endOnTime);
  466. }
  467. else
  468. System.out.println(
  469. "Parameter endOnTime cannot be null! Options: indefinite or onDate");
  470. return endTime;
  471. }
  472. // sn_dg_prm_smpl_schedulereport_P3_end_0
  473. public RunOptionBoolean setDeliveryMethodPrint()
  474. {
  475. RunOptionBoolean printOption = new RunOptionBoolean();
  476. printOption.setName(RunOptionEnum.print);
  477. //?saveOutput, e-mail,print,burst?
  478. printOption.setValue(true);
  479. return printOption;
  480. }
  481. public RunOptionBoolean setDeliveryMethodSave()
  482. {
  483. RunOptionBoolean saveOutput = new RunOptionBoolean();
  484. saveOutput.setName(RunOptionEnum.saveOutput);
  485. saveOutput.setValue(true);
  486. return saveOutput;
  487. }
  488. public DeliveryOptionAddressSMTPArray setEmailAddresses(AddressSMTP[] emails)
  489. {
  490. //The emails can also be taken from CM as group, user, etc.(see Email class)
  491. DeliveryOptionAddressSMTPArray emailAddress = new DeliveryOptionAddressSMTPArray();
  492. emailAddress.setName(DeliveryOptionEnum.toAddress);
  493. emailAddress.setValue(emails);
  494. return emailAddress;
  495. }
  496. public DeliveryOptionMemoPart setEmailBody()
  497. {
  498. DeliveryOptionMemoPart bodyText = new DeliveryOptionMemoPart();
  499. MemoPartString memoText = new MemoPartString();
  500. memoText.setName("Body");
  501. memoText.setText("This is a scheduled report");
  502. memoText.setContentDisposition(SmtpContentDispositionEnum.inline);
  503. bodyText.setName(DeliveryOptionEnum.memoPart);
  504. bodyText.setValue(memoText);
  505. return bodyText;
  506. }
  507. public DeliveryOptionString setEmailSubject()
  508. {
  509. DeliveryOptionString subjectText = new DeliveryOptionString();
  510. subjectText.setName(DeliveryOptionEnum.subject);
  511. subjectText.setValue("Sample Scheduled Report");
  512. return subjectText;
  513. }
  514. public RunOptionBoolean setEmailAttach()
  515. {
  516. RunOptionBoolean attach = new RunOptionBoolean();
  517. attach.setName(RunOptionEnum.emailAsAttachment);
  518. attach.setValue(true);
  519. return attach;
  520. }
  521. public RunOptionBoolean setDeliveryMethodEmail()
  522. {
  523. RunOptionBoolean sendEmail = new RunOptionBoolean();
  524. sendEmail.setName(RunOptionEnum.email);
  525. sendEmail.setValue(true);
  526. return sendEmail;
  527. }
  528. public RunOptionString setPrinter(String printerName)
  529. {
  530. RunOptionString printerOption = new RunOptionString();
  531. //if ptinterOption not set, it will print to the default one
  532. printerOption.setName(RunOptionEnum.printer);
  533. printerOption.setValue(printerName);
  534. return printerOption;
  535. }
  536. public RunOptionString setOrientation(String pageOrientation)
  537. {
  538. RunOptionString orientation = new RunOptionString();
  539. orientation.setName(RunOptionEnum.outputPageOrientation);
  540. orientation.setValue(pageOrientation);
  541. return orientation;
  542. }
  543. public RunOptionString setPaperSize(String paper)
  544. {
  545. String quotChar = "\'";
  546. if (paper.indexOf(quotChar) >= 0)
  547. {
  548. quotChar = "\"";
  549. }
  550. RunOptionString paperSize = new RunOptionString();
  551. paperSize.setName(RunOptionEnum.outputPageDefinition);
  552. paperSize.setValue(
  553. "/configuration/pageDefinition[@name="
  554. + quotChar
  555. + paper
  556. + quotChar
  557. + "]");
  558. return paperSize;
  559. }
  560. public Option setDeliveryMethodSaveAs(
  561. CRNConnect connection,
  562. String saveasName,
  563. String reportPath)
  564. {
  565. CSHandlers csh = new CSHandlers();
  566. // sn_dg_prm_smpl_schedulereport_P5_start_1
  567. RunOptionSaveAs saveAs = new RunOptionSaveAs();
  568. MultilingualToken[] obj = new MultilingualToken[1];
  569. //Set the name of the reportView
  570. obj[0] = new MultilingualToken();
  571. obj[0].setLocale("en-us");
  572. //If no name provided use default
  573. if (saveasName != null)
  574. {
  575. obj[0].setValue(saveasName);
  576. }
  577. else
  578. {
  579. obj[0].setValue("View of Report " + reportPath);
  580. }
  581. //Save the object as report view with name saveasName
  582. saveAs.setName(RunOptionEnum.saveAs);
  583. saveAs.setObjectClass(ReportSaveAsEnum.reportView);
  584. saveAs.setObjectName(obj);
  585. saveAs.setParentSearchPath(csh.getParentPath(connection, reportPath));
  586. // sn_dg_prm_smpl_schedulereport_P5_end_1
  587. //add saveAs option to RunOptions
  588. return saveAs;
  589. }
  590. /**
  591. * Return 1 if the report has a schedule and get the schedule .
  592. *
  593. *@param connection Connection to Server
  594. *@param path Search path to a report.
  595. */
  596. public int getSchedule(CRNConnect connection, String path)
  597. {
  598. PropEnum props[] =
  599. new PropEnum[] {
  600. PropEnum.searchPath,
  601. PropEnum.defaultName,
  602. PropEnum.displaySequence,
  603. PropEnum.weeklyWednesday,
  604. PropEnum.everyNPeriods };
  605. String schedulePath = path + "/schedule";
  606. try
  607. {
  608. connection.getCMService().query(
  609. new SearchPathMultipleObject(schedulePath),
  610. props,
  611. new Sort[] {},
  612. new QueryOptions());
  613. return 1;
  614. }
  615. catch (Exception e)
  616. {
  617. System.out.println(e);
  618. return 0;
  619. }
  620. }
  621. public void setSchedule(Schedule sch)
  622. {
  623. newSchedule = sch;
  624. }
  625. public BaseClass getSchedule()
  626. {
  627. return newSchedule;
  628. }
  629. public void setScheduleSearchPath(String path)
  630. {
  631. StringProp schPath = new StringProp();
  632. schPath.setValue(path);
  633. newSchedule.setSearchPath(schPath);
  634. }
  635. /**
  636. * If schedule exists delete it from the Content store.
  637. *
  638. *@param connection Connection to Server
  639. *@param reportPath Search path to a report.
  640. */
  641. public void deleteSchedule(CRNConnect connection, String path)
  642. {
  643. PropEnum props[] =
  644. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  645. try
  646. {
  647. //this.getSchedule(connect, path);
  648. this.getSchedule();
  649. //Define delete options
  650. DeleteOptions del = new DeleteOptions();
  651. del.setForce(true);
  652. String schedulePath = path + "/schedule";
  653. BaseClass bc[] =
  654. connection.getCMService().query(
  655. new SearchPathMultipleObject(schedulePath),
  656. props,
  657. new Sort[] {},
  658. new QueryOptions());
  659. if (bc != null)
  660. {
  661. if (bc.length > 0)
  662. {
  663. int i = connection.getCMService().delete(bc, del);
  664. if (i > 0)
  665. {
  666. System.out.println(
  667. "The schedule was deleted successfully");
  668. }
  669. else
  670. System.out.println(
  671. "Error occurred while deleting the schedule");
  672. }
  673. }
  674. }
  675. catch (Exception e)
  676. {
  677. System.out.println(e);
  678. }
  679. }
  680. /**
  681. * Searches the content store for the schedule path
  682. * and returns a Schedule object
  683. *@param connection Connection to Server
  684. *@param path Search path to a report.
  685. */
  686. public Schedule getScheduleFromCM(
  687. CRNConnect connection,
  688. String path)
  689. {
  690. PropEnum props[] =
  691. new PropEnum[] {
  692. PropEnum.searchPath,
  693. PropEnum.defaultName,
  694. PropEnum.options};
  695. try
  696. {
  697. BaseClass[] bc =
  698. connection.getCMService().query(
  699. new SearchPathMultipleObject(path),
  700. props,
  701. new Sort[] {},
  702. new QueryOptions());
  703. if (bc.length > 0)
  704. {
  705. return (Schedule)bc[0];
  706. }
  707. }
  708. catch (Exception e)
  709. {
  710. System.out.println(e);
  711. }
  712. return null;
  713. }
  714. /** If the report has a schedule, disable it.
  715. *
  716. *@param connection Connection to Server
  717. *@param path Search path to a report.
  718. */
  719. public int updateSchedule(CRNConnect connection, String path)
  720. {
  721. PropEnum props[] =
  722. new PropEnum[] {
  723. PropEnum.searchPath,
  724. PropEnum.defaultName,
  725. PropEnum.options };
  726. try
  727. {
  728. BaseClass[] bc =
  729. connection.getCMService().query(
  730. new SearchPathMultipleObject(path),
  731. props, new Sort[] {},
  732. new QueryOptions());
  733. if (bc.length > 0)
  734. {
  735. newSchedule = (Schedule)bc[0];
  736. connection.getCMService().update(bc, new UpdateOptions());
  737. }
  738. return 1;
  739. }
  740. catch (Exception e)
  741. {
  742. System.out.println(e);
  743. return 0;
  744. }
  745. }
  746. /**
  747. * If the report has a schedule, disable it.
  748. *
  749. *@param connection Connection to Server
  750. *@param path Search path to a report.
  751. */
  752. public int disableSchedule(CRNConnect connection, String path)
  753. {
  754. PropEnum props[] =
  755. new PropEnum[] { PropEnum.searchPath, PropEnum.defaultName };
  756. try
  757. {
  758. Schedule sch = new Schedule();
  759. if (this.getSchedule(connection, path) != 0)
  760. {
  761. BooleanProp isActive = new BooleanProp();
  762. if (path.indexOf("/schedule") <= 0)
  763. path += "/schedule";
  764. BaseClass[] bc =
  765. connection.getCMService().query(
  766. new SearchPathMultipleObject(path),
  767. props,
  768. new Sort[] {},
  769. new QueryOptions());
  770. if (bc.length > 0)
  771. {
  772. sch = (Schedule)bc[0];
  773. isActive.setValue(false);
  774. sch.setActive(isActive);
  775. connection.getCMService().update(bc, new UpdateOptions());
  776. }
  777. }
  778. return 1;
  779. }
  780. catch (Exception e)
  781. {
  782. System.out.println(e);
  783. return 0;
  784. }
  785. }
  786. }