0001: /*
0002: * Copyright 2005 Pentaho Corporation. All rights reserved.
0003: * This software was developed by Pentaho Corporation and is provided under the terms
0004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
0005: * this file except in compliance with the license. If you need a copy of the license,
0006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
0007: * BI Platform. The Initial Developer is Pentaho Corporation.
0008: *
0009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
0010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
0011: * the license for the specific language governing your rights and limitations.
0012: *
0013: * @created Oct 7, 2005
0014: * @author James Dixon
0015: */
0016:
0017: package com.pentaho.repository.subscribe;
0018:
0019: import java.text.SimpleDateFormat;
0020: import java.util.ArrayList;
0021: import java.util.Arrays;
0022: import java.util.Date;
0023: import java.util.HashMap;
0024: import java.util.Iterator;
0025: import java.util.List;
0026: import java.util.Map;
0027:
0028: import org.apache.commons.logging.Log;
0029: import org.apache.commons.logging.LogFactory;
0030: import org.dom4j.Document;
0031: import org.dom4j.DocumentHelper;
0032: import org.dom4j.Element;
0033: import org.dom4j.Node;
0034: import org.hibernate.HibernateException;
0035: import org.hibernate.Query;
0036: import org.hibernate.Session;
0037: import org.pentaho.core.cache.CacheManager;
0038: import org.pentaho.core.repository.IContentItem;
0039: import org.pentaho.core.repository.IContentItemFile;
0040: import org.pentaho.core.repository.IContentLocation;
0041: import org.pentaho.core.repository.IContentRepository;
0042: import org.pentaho.core.session.IPentahoSession;
0043: import org.pentaho.core.subscribe.ISubscriptionScheduler;
0044: import org.pentaho.core.subscribe.SubscriptionHelper;
0045: import org.pentaho.core.subscribe.SubscriptionResultSet;
0046: import org.pentaho.core.system.PentahoSystem;
0047: import org.pentaho.messages.Messages;
0048: import org.pentaho.repository.HibernateUtil;
0049: import org.pentaho.util.UUIDUtil;
0050:
0051: public class SubscriptionRepository implements ISubscriptionRepository {
0052: private static byte[] SyncLock = new byte[0];
0053:
0054: private static final Log logger = LogFactory
0055: .getLog(SubscriptionRepository.class);
0056:
0057: private static ISubscriptionScheduler subscriptionScheduler = PentahoSystem
0058: .getSubscriptionScheduler();
0059:
0060: public Log getLogger() {
0061: return logger;
0062: }
0063:
0064: public SubscriptionRepository() {
0065: // TODO make this based on configuration
0066: HibernateUtil.beginTransaction();
0067: }
0068:
0069: public Element importSchedules(Document doc) {
0070: Element resultElement = DocumentHelper
0071: .createElement("importResults"); //$NON-NLS-1$
0072:
0073: if (doc == null) {
0074: Element ele = resultElement
0075: .addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.DOCUMENT_IS_NULL")); //$NON-NLS-1$ //$NON-NLS-2$
0076: ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
0077: return (resultElement);
0078: }
0079:
0080: List scheduleNodes = doc.selectNodes("//schedules/*"); //$NON-NLS-1$
0081: if (scheduleNodes.size() == 0) {
0082: Element ele = resultElement
0083: .addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.NO_SCHEDULES_DEFINED")); //$NON-NLS-1$ //$NON-NLS-2$
0084: ele.addAttribute("result", "WARNING"); //$NON-NLS-1$ //$NON-NLS-2$
0085: return (resultElement);
0086: }
0087:
0088: synchronized (SyncLock) {
0089:
0090: Node scheduleNode = null;
0091: String schedName, schedDesc, schedRef, cronString, schedGroup;
0092: Schedule aSchedule;
0093: // List schedList;
0094:
0095: try {
0096: logger
0097: .info(Messages
0098: .getString(
0099: "PRO_SUBSCRIPTREP.USER_IMPORT_PROCESSING_SCHEDULES", Integer.toString(scheduleNodes.size()))); //$NON-NLS-1$
0100: for (int i = 0; i < scheduleNodes.size(); i++) {
0101: scheduleNode = (Node) scheduleNodes.get(i);
0102: schedRef = scheduleNode
0103: .selectSingleNode("@ref").getText(); //$NON-NLS-1$
0104: schedName = scheduleNode
0105: .selectSingleNode("@name").getText(); //$NON-NLS-1$
0106: schedDesc = scheduleNode.selectSingleNode(
0107: "@description").getText(); //$NON-NLS-1$
0108: schedGroup = scheduleNode
0109: .selectSingleNode("@group").getText(); //$NON-NLS-1$
0110: cronString = scheduleNode.getText();
0111:
0112: try {
0113: aSchedule = getScheduleByScheduleReference(schedRef);
0114: if (aSchedule != null) {
0115: aSchedule.setCronString(cronString);
0116: aSchedule.setDescription(schedDesc);
0117: aSchedule.setGroup(schedGroup);
0118: aSchedule.setTitle(schedName);
0119: resultElement
0120: .addElement("modified").addText(schedRef); //$NON-NLS-1$
0121: logger
0122: .info(Messages
0123: .getString(
0124: "PRO_SUBSCRIPTREP.MODIFIED_SUBSCRIPTION_SCHEDULE", schedRef)); //$NON-NLS-1$
0125: } else {
0126: aSchedule = addSchedule(schedName,
0127: schedRef, schedDesc, cronString,
0128: schedGroup);
0129: resultElement
0130: .addElement("added").addText(schedRef); //$NON-NLS-1$
0131: logger
0132: .info(Messages
0133: .getString(
0134: "PRO_SUBSCRIPTREP.ADDED_SUBSCRIPTION_SCHEDULE", schedRef)); //$NON-NLS-1$
0135: }
0136: subscriptionScheduler
0137: .getCronSummary(cronString); // Throws an exception if invalid
0138: } catch (Exception e) {
0139: resultElement
0140: .addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_OCCURRED_WITH_SCHEDULE", schedRef, e.getLocalizedMessage())); //$NON-NLS-1$ //$NON-NLS-2$
0141: logger
0142: .warn(
0143: Messages
0144: .getString(
0145: "PRO_SUBSCRIPTREP.EXCEPTION_WITH_SCHEDULE", schedRef), e); //$NON-NLS-1$
0146: }
0147: }
0148:
0149: } catch (Exception e) {
0150: Element ele = resultElement
0151: .addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_PROCESSING_IMPORTS") + e.getLocalizedMessage()); //$NON-NLS-1$ //$NON-NLS-2$
0152: ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
0153: logger
0154: .error(
0155: Messages
0156: .getString("PRO_SUBSCRIPTREP.EXCEPTION_PROCESSING_IMPORTS"), e); //$NON-NLS-1$
0157: return (resultElement);
0158: }
0159: }
0160: return (resultElement);
0161: }
0162:
0163: public Element importContent(Document doc) {
0164: Element resultElement = DocumentHelper
0165: .createElement("importResults"); //$NON-NLS-1$
0166:
0167: if (doc == null) {
0168: Element ele = resultElement
0169: .addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.DOCUMENT_IS_NULL")); //$NON-NLS-1$ //$NON-NLS-2$
0170: ele.addAttribute("result", "ERROR"); //$NON-NLS-1$//$NON-NLS-2$
0171: return (resultElement);
0172: }
0173:
0174: List contentNodes = doc.selectNodes("//subscription-content/*"); //$NON-NLS-1$
0175: if (contentNodes.size() == 0) {
0176: Element ele = resultElement
0177: .addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.NO_SCHEDULES_DEFINED")); //$NON-NLS-1$ //$NON-NLS-2$
0178: ele.addAttribute("result", "WARNING"); //$NON-NLS-1$//$NON-NLS-2$
0179: return (resultElement);
0180: }
0181:
0182: synchronized (SyncLock) {
0183:
0184: Node contentNode = null;
0185: Node tempNode = null;
0186: String actionRef, contentType, allowAllSchedules;
0187: SubscribeContent subscribeContent = null;
0188:
0189: List schedList = getSchedules();
0190: Map scheduleMap = new HashMap();
0191: Map groupMap = new HashMap();
0192:
0193: for (int j = 0; j < schedList.size(); ++j) {
0194: Schedule aSchedule = (Schedule) schedList.get(j);
0195: scheduleMap.put(aSchedule.getScheduleReference(),
0196: aSchedule);
0197: List groupList = (List) groupMap.get(aSchedule
0198: .getGroup());
0199: if (groupList == null) {
0200: groupList = new ArrayList();
0201: groupMap.put(aSchedule.getGroup(), groupList);
0202: }
0203: groupList.add(aSchedule);
0204: }
0205:
0206: try {
0207: logger
0208: .info(Messages
0209: .getString(
0210: "PRO_SUBSCRIPTREP.USER_PROCESSING_CONTENT_NODES", Integer.toString(contentNodes.size()))); //$NON-NLS-1$
0211: for (int i = 0; i < contentNodes.size(); i++) {
0212: contentNode = (Node) contentNodes.get(i);
0213: actionRef = contentNode
0214: .selectSingleNode("@action").getText(); //$NON-NLS-1$
0215: contentType = contentNode
0216: .selectSingleNode("@type").getText(); //$NON-NLS-1$
0217: tempNode = contentNode
0218: .selectSingleNode("@allowAllSchedules"); //$NON-NLS-1$
0219: if (tempNode != null) {
0220: allowAllSchedules = tempNode.getText();
0221: } else {
0222: allowAllSchedules = "false"; //$NON-NLS-1$
0223: }
0224:
0225: try {
0226: subscribeContent = getContentByActionReference(actionRef);
0227: if (subscribeContent != null) {
0228: subscribeContent.setType(contentType);
0229: resultElement
0230: .addElement("modified").addText(actionRef); //$NON-NLS-1$
0231: logger
0232: .info(Messages
0233: .getString(
0234: "PRO_SUBSCRIPTREP.MODIFIED_SUBSCRIPTION_CONTENT", actionRef)); //$NON-NLS-1$
0235: } else {
0236: subscribeContent = addContent(actionRef,
0237: contentType);
0238: resultElement
0239: .addElement("added").addText(actionRef); //$NON-NLS-1$
0240: logger
0241: .info(Messages
0242: .getString(
0243: "PRO_SUBSCRIPTREP.ADDED_SUBSCRIPTION_CONTENT", actionRef)); //$NON-NLS-1$
0244: }
0245:
0246: } catch (Exception e) {
0247: resultElement
0248: .addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_WITH_CONTENT", actionRef, e.getLocalizedMessage())); //$NON-NLS-1$ //$NON-NLS-2$
0249: logger
0250: .warn(
0251: Messages
0252: .getString(
0253: "PRO_SUBSCRIPTREP.ERROR_WITH_CONTENT_LOG", actionRef), e); //$NON-NLS-1$
0254: }
0255:
0256: List contentSchedules = new ArrayList();
0257: if ("true".equalsIgnoreCase(allowAllSchedules)) { //$NON-NLS-1$
0258: contentSchedules.addAll(schedList);
0259: } else {
0260:
0261: List suppliedSchedules = contentNode
0262: .selectNodes("schedule"); //$NON-NLS-1$
0263: if (suppliedSchedules != null) {
0264: for (int j = 0; j < suppliedSchedules
0265: .size(); j++) {
0266: tempNode = (Node) suppliedSchedules
0267: .get(j);
0268: String schName = tempNode.getText();
0269: if (schName != null) {
0270: Object aSchedule = scheduleMap
0271: .get(schName);
0272: if (aSchedule != null) {
0273: contentSchedules.add(aSchedule);
0274: }
0275: }
0276: }
0277: }
0278:
0279: List suppliedGroups = contentNode
0280: .selectNodes("group"); //$NON-NLS-1$
0281: if (suppliedGroups != null) {
0282: for (int j = 0; j < suppliedGroups.size(); j++) {
0283: tempNode = (Node) suppliedGroups.get(j);
0284: String grpName = tempNode.getText();
0285: if (grpName != null) {
0286: List groupList = (List) groupMap
0287: .get(grpName);
0288: if (groupList != null) {
0289: contentSchedules
0290: .addAll(groupList);
0291: }
0292: }
0293: }
0294: }
0295: }
0296: HibernateUtil.beginTransaction(); // Need to do this or the schedules don't get saved if the content item is new
0297: subscribeContent.setSchedules(contentSchedules);
0298: HibernateUtil.commitTransaction();
0299: }
0300: } catch (Exception e) {
0301: Element ele = resultElement
0302: .addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_PROCESSING_IMPORTS") + e.getLocalizedMessage()); //$NON-NLS-1$ //$NON-NLS-2$
0303: ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
0304: logger
0305: .error(
0306: Messages
0307: .getString("PRO_SUBSCRIPTREP.EXCEPTION_PROCESSING_IMPORTS"), e); //$NON-NLS-1$
0308: return (resultElement);
0309: }
0310:
0311: }
0312: return (resultElement);
0313: }
0314:
0315: public boolean addSubscription(Subscription subscription) {
0316: if (subscription.getId() != null
0317: && subscription.getTitle() != null) {
0318: HibernateUtil.beginTransaction();
0319: Session session = HibernateUtil.getSession();
0320: session.save(subscription);
0321: // subscriptionMap.put( subscription.getId(), subscription );
0322: HibernateUtil.commitTransaction();
0323: return true;
0324: } else {
0325: // TODO log an error
0326: return false;
0327: }
0328: }
0329:
0330: public List getSubscriptionsForSchedule(Schedule schedule) {
0331: if (schedule == null) {
0332: return new ArrayList();
0333: }
0334: Session session = HibernateUtil.getSession();
0335: Query qry = session
0336: .getNamedQuery(
0337: "com.pentaho.repository.subscribe.Schedule.findSubscriptionsBySchedule").setCacheable(true); //$NON-NLS-1$
0338: qry.setEntity("schedule", schedule); //$NON-NLS-1$
0339: return qry.list();
0340: }
0341:
0342: public List getSubscriptionsForSchedule(String scheduleId) {
0343: return getSubscriptionsForSchedule(this
0344: .getScheduleByScheduleReference(scheduleId));
0345: }
0346:
0347: public SubscriptionResultSet getSubscriptions(String scheduleId,
0348: IPentahoSession session, String solution, String path,
0349: String action, String parameterNames[]) {
0350: SubscriptionResultSet results = new SubscriptionResultSet(
0351: scheduleId, session, parameterNames, solution, path,
0352: action);
0353: return results;
0354: }
0355:
0356: public void addContent(SubscribeContent content) {
0357: if (getContentByActionReference(content.getActionReference()) != null) {
0358: throw new SubscriptionRepositoryException(
0359: Messages
0360: .getString(
0361: "PRO_SUBSCRIPTREP.ACTION_SEQUENCE_ALREADY_EXISTS", content.getActionReference())); //$NON-NLS-1$
0362: }
0363:
0364: HibernateUtil.beginTransaction();
0365: Session session = HibernateUtil.getSession();
0366: session.save(content);
0367: HibernateUtil.commitTransaction();
0368: clearSessionCaches();
0369: }
0370:
0371: private void clearSessionCaches() {
0372: CacheManager cm = PentahoSystem.getCacheManager();
0373: if (cm != null) {
0374: cm.killSessionCaches();
0375: }
0376: }
0377:
0378: public SubscribeContent addContent(String actionRef,
0379: String contentType) {
0380: SubscribeContent newContent = new SubscribeContent(UUIDUtil
0381: .getUUIDAsString(), actionRef, contentType);
0382: addContent(newContent);
0383: return (newContent);
0384: }
0385:
0386: public void setContent(String actionRefs[]) throws Exception {
0387: List keepList = new ArrayList(Arrays.asList(actionRefs)); // Strings
0388: List allContent = getAllContent();
0389: List deleteList = new ArrayList(); // SubscribeContent
0390: for (int i = 0; i < allContent.size(); ++i) {
0391: SubscribeContent content = (SubscribeContent) allContent
0392: .get(i);
0393: if (keepList.contains(content.getActionReference())) {
0394: keepList.remove(content.getActionReference());
0395: } else {
0396: deleteList.add(content);
0397: }
0398: }
0399:
0400: for (int i = 0; i < keepList.size(); ++i) {
0401: addContent((String) keepList.get(i), ""); //$NON-NLS-1$
0402: }
0403:
0404: for (int i = 0; i < deleteList.size(); ++i) {
0405: deleteSubscribeContent((SubscribeContent) deleteList.get(i));
0406: }
0407: }
0408:
0409: public SubscribeContent editContent(String id, String actionRef,
0410: String contentType) {
0411: try {
0412: SubscribeContent oldCont = getContentById(id);
0413: if (oldCont != null) {
0414: // Any null values keep previous values
0415: if (actionRef != null)
0416: oldCont.setActionReference(actionRef);
0417: if (contentType != null)
0418: oldCont.setType(contentType);
0419: return (oldCont);
0420: }
0421: } catch (HibernateException ex) {
0422: throw new SubscriptionRepositoryException(
0423: Messages
0424: .getString("PRO_SUBSCRIPTREP.CANNOT_EDIT_CONTENT") + id + ex); //$NON-NLS-1$
0425: }
0426:
0427: return (null);
0428: }
0429:
0430: public boolean deleteContentForSchedule(String contentId,
0431: String schedId) throws Exception {
0432: SubscribeContent content = getContentById(contentId);
0433: Schedule sched = getSchedule(schedId);
0434: return (deleteContentForSchedule(content, sched));
0435: }
0436:
0437: public boolean deleteContentForSchedule(SubscribeContent content,
0438: Schedule sched) throws Exception {
0439: if (content == null) {
0440: return (false);
0441: }
0442: return (content.removeSchedule(sched));
0443: }
0444:
0445: public boolean addContentForSchedule(String contentId,
0446: String schedId) throws Exception {
0447: SubscribeContent content = getContentById(contentId);
0448: Schedule sched = getSchedule(schedId);
0449: return (addContentForSchedule(content, sched));
0450: }
0451:
0452: public boolean addContentForSchedule(SubscribeContent content,
0453: Schedule sched) throws Exception {
0454: if (content == null) {
0455: return (false);
0456: }
0457: content.addSchedule(sched);
0458: return (true);
0459: }
0460:
0461: public void setSchedulesForContent(String scheduleId[],
0462: String contentId) throws Exception {
0463: SubscribeContent content = getContentById(contentId);
0464:
0465: List schedList = new ArrayList();
0466: for (int i = 0; i < scheduleId.length; ++i) {
0467: Schedule sched = getSchedule(scheduleId[i]);
0468: if (sched != null) {
0469: schedList.add(sched);
0470: }
0471: }
0472:
0473: content.setSchedules(schedList);
0474: }
0475:
0476: public void setContentForSchedule(String contentId[], String schedId)
0477: throws Exception {
0478: Schedule sched = getSchedule(schedId);
0479:
0480: List allContent = getAllContent();
0481: for (int i = 0; i < contentId.length; ++i) {
0482: SubscribeContent content = getContentById(contentId[i]);
0483: addContentForSchedule(content, sched);
0484: allContent.remove(content);
0485: }
0486:
0487: for (int i = 0; i < allContent.size(); ++i) {
0488: SubscribeContent content = (SubscribeContent) allContent
0489: .get(i);
0490: deleteContentForSchedule(content, sched);
0491: }
0492: }
0493:
0494: public SubscribeContent getContentById(String theId) {
0495: Session session = HibernateUtil.getSession();
0496: try {
0497: return (SubscribeContent) session.get(
0498: SubscribeContent.class, theId);
0499: } catch (HibernateException ex) {
0500: throw new SubscriptionRepositoryException(
0501: Messages
0502: .getErrorString(
0503: "PRO_SUBSCRIPTREP.ERROR_0001_GETTING_SUBSCRIPTION", theId), ex); //$NON-NLS-1$
0504: }
0505: }
0506:
0507: public List getAllContent() {
0508: Session session = HibernateUtil.getSession();
0509: Query qry = session
0510: .getNamedQuery(
0511: "com.pentaho.repository.subscribe.SubscribeContent.getAllContent").setCacheable(true); //$NON-NLS-1$
0512: return qry.list();
0513: }
0514:
0515: public SubscribeContent getContentByActionReference(
0516: String actionReference) {
0517: Session session = HibernateUtil.getSession();
0518: Query qry = session
0519: .getNamedQuery(
0520: "com.pentaho.repository.subscribe.SubscribeContent.findSubscriptionContentByActionRef").setCacheable(true); //$NON-NLS-1$
0521: qry.setString("searchTerm", actionReference); //$NON-NLS-1$
0522: Object rtn = null;
0523: try {
0524: rtn = qry.uniqueResult();
0525: } catch (Exception ignored) {
0526: }
0527: return (SubscribeContent) rtn;
0528: }
0529:
0530: public List getContentBySchedule(Schedule schedule) {
0531: Session session = HibernateUtil.getSession();
0532: Query qry = session
0533: .getNamedQuery(
0534: "com.pentaho.repository.subscribe.SubscribeContent.findContentBySchedule").setCacheable(true); //$NON-NLS-1$
0535: qry.setParameter("schedule", schedule); //$NON-NLS-1$
0536: return (qry.list());
0537: }
0538:
0539: public Document getUserSubscriptions(String user, String contentId,
0540: IPentahoSession session) {
0541: Document document = DocumentHelper.createDocument();
0542: Element subscriptionsNode = document
0543: .addElement("subscriptions"); //$NON-NLS-1$
0544: addSubscriptionsToDocument(user, contentId, subscriptionsNode,
0545: null, session);
0546: return document;
0547: }
0548:
0549: public void deleteUserSubscriptions(String user) {
0550: //
0551: Session session = HibernateUtil.getSession();
0552: Query qry = session
0553: .getNamedQuery(
0554: "com.pentaho.repository.subscribe.Subscription.findUserSubscriptionsForDeletion").setCacheable(true); //$NON-NLS-1$
0555: qry.setString("searchUser", user); //$NON-NLS-1$
0556: logger.warn(Messages.getErrorString(
0557: "PRO_SUBSCRIPTREP.USER_REMOVING_USER", user)); //$NON-NLS-1$
0558: List subscriptions = qry.list();
0559: Subscription subscriptToDelete = null;
0560: for (int i = 0; i < subscriptions.size(); i++) {
0561: subscriptToDelete = (Subscription) subscriptions.get(i);
0562: HibernateUtil.makeTransient(subscriptToDelete);
0563: }
0564: HibernateUtil.flushSession();
0565: }
0566:
0567: public List getUserSubscriptionsToContentReference(String user,
0568: String contentId) {
0569: Session session = HibernateUtil.getSession();
0570: SubscribeContent content = this
0571: .getContentByActionReference(contentId);
0572: if (content == null) {
0573: return new ArrayList();
0574: }
0575: Query qry = session
0576: .getNamedQuery(
0577: "com.pentaho.repository.subscribe.Subscription.findUserSubscriptions").setCacheable(true); //$NON-NLS-1$
0578: qry.setString("searchUser", user); //$NON-NLS-1$
0579: qry.setEntity("searchContent", content); //$NON-NLS-1$
0580: return qry.list();
0581: }
0582:
0583: public List getUserSubscriptions(String user) {
0584: Session session = HibernateUtil.getSession();
0585: Query qry = session
0586: .getNamedQuery(
0587: "com.pentaho.repository.subscribe.Subscription.findAllUserSubscriptions").setCacheable(true); //$NON-NLS-1$
0588: qry.setString("searchUser", user); //$NON-NLS-1$
0589: return qry.list();
0590: }
0591:
0592: public List getAllSubscriptions() {
0593: Session session = HibernateUtil.getSession();
0594: Query qry = session
0595: .getNamedQuery(
0596: "com.pentaho.repository.subscribe.Subscription.getAllSubscriptions").setCacheable(true); //$NON-NLS-1$
0597: return qry.list();
0598: }
0599:
0600: public void addSubscriptionsToDocument(String user,
0601: String contentId, Element subscriptionsNode, String editId,
0602: IPentahoSession session) {
0603: List userSubsToContent = getUserSubscriptionsToContentReference(
0604: user, contentId);
0605: if (userSubsToContent == null) {
0606: return;
0607: }
0608: for (int i = 0; i < userSubsToContent.size(); i++) {
0609: Subscription subscription = (Subscription) userSubsToContent
0610: .get(i);
0611: addSubscriptionToDocument(subscription, subscriptionsNode,
0612: editId, session);
0613: }
0614: }
0615:
0616: /**
0617: * Returns true if the subscription name is unique for the user/contentId
0618: *
0619: * @param Name
0620: * the schedule name to check for uniqueness
0621: * @param user
0622: * the user that owns the schedules
0623: * @param contentId
0624: * The action sequence separated by slashes
0625: * @return
0626: */
0627: public boolean checkUniqueSubscriptionName(String name,
0628: String user, String contentId) {
0629: if (name == null) {
0630: return true;
0631: }
0632: List userSubsToContent = getUserSubscriptionsToContentReference(
0633: user, contentId);
0634: if (userSubsToContent == null) {
0635: return true;
0636: }
0637: for (int i = 0; i < userSubsToContent.size(); i++) {
0638: Subscription subscription = (Subscription) userSubsToContent
0639: .get(i);
0640: if (name.equals(subscription.getTitle())) {
0641: return false;
0642: }
0643: }
0644:
0645: return true;
0646: }
0647:
0648: public void addSchedulesToDocument(String user, String contentId,
0649: Element schedulesNode, String editId) {
0650: // now see if there are schedules
0651: SubscribeContent content = this
0652: .getContentByActionReference(contentId);
0653: if (content == null) {
0654: return;
0655: }
0656: List schedules = content.getSchedules();
0657: Iterator scheduleIterator = schedules.iterator();
0658: Schedule schedule;
0659: while (scheduleIterator.hasNext()) {
0660: schedule = (Schedule) scheduleIterator.next();
0661: if (schedule != null) {
0662: Element scheduleNode = schedulesNode
0663: .addElement("schedule"); //$NON-NLS-1$
0664: scheduleNode.addElement("id").setText(schedule.getId()); //$NON-NLS-1$
0665: scheduleNode
0666: .addElement("title").setText(schedule.getTitle()); //$NON-NLS-1$
0667: scheduleNode
0668: .addElement("group").setText(schedule.getGroup()); //$NON-NLS-1$
0669: scheduleNode
0670: .addElement("schedRef").setText(schedule.getScheduleReference()); //$NON-NLS-1$
0671: // see if this schedule is currently selected
0672: /*
0673: * if( selectedSchedules.contains( schedule.getId() ) ) {
0674: * scheduleNode.addAttribute( "selected", "true" );
0675: * //$NON-NLS-1$//$NON-NLS-2$ }
0676: */
0677: }
0678: }
0679: }
0680:
0681: public void addSubscriptionToDocument(Subscription subscription,
0682: Element subscriptionsNode, String editId,
0683: IPentahoSession session) {
0684:
0685: Element node = subscriptionsNode.addElement("subscription"); //$NON-NLS-1$
0686: if (subscription.getTitle() == null) {
0687: node.addElement("title").setText("Unknown"); //$NON-NLS-1$ //$NON-NLS-2$
0688: } else {
0689: node.addElement("title").setText(subscription.getTitle()); //$NON-NLS-1$
0690: }
0691: node
0692: .addElement("content-id").setText(subscription.getContent().getId()); //$NON-NLS-1$
0693: node.addElement("id").setText(subscription.getId()); //$NON-NLS-1$
0694: node.addElement("user").setText(subscription.getUser()); //$NON-NLS-1$
0695: if (editId != null && editId.equals(subscription.getId())) {
0696: node.addAttribute("edit", "true"); //$NON-NLS-1$//$NON-NLS-2$
0697: }
0698: int type = subscription.getType();
0699: switch (type) {
0700: case Subscription.TYPE_PERSONAL:
0701: node.addElement("type").setText("personal");break; //$NON-NLS-1$ //$NON-NLS-2$
0702: case Subscription.TYPE_GROUP:
0703: node.addElement("type").setText("group");break; //$NON-NLS-1$ //$NON-NLS-2$
0704: case Subscription.TYPE_ROLE:
0705: node.addElement("type").setText("role");break; //$NON-NLS-1$ //$NON-NLS-2$
0706: }
0707: Map parameters = subscription.getParameters();
0708: Iterator parameterIterator = parameters.keySet().iterator();
0709: Element parametersNode = node.addElement("parameters"); //$NON-NLS-1$
0710: while (parameterIterator.hasNext()) {
0711: String name = (String) parameterIterator.next();
0712: Object value = parameters.get(name);
0713: Element parameterNode = parametersNode
0714: .addElement("parameter"); //$NON-NLS-1$
0715: parameterNode.addElement("name").setText(name); //$NON-NLS-1$
0716: parameterNode.addElement("value").setText(value.toString()); //$NON-NLS-1$
0717: }
0718: // now add the archives
0719: IContentItem contentItem = getContentItem(subscription.getId(),
0720: session);
0721: if (contentItem != null) {
0722: List contentFiles = contentItem.getFileVersions();
0723: if (contentFiles != null && contentFiles.size() > 0) {
0724: Element archivesNode = node.addElement("archives"); //$NON-NLS-1$
0725: Iterator contentFileIterator = contentFiles.iterator();
0726: while (contentFileIterator.hasNext()) {
0727: IContentItemFile file = (IContentItemFile) contentFileIterator
0728: .next();
0729: Date date = file.getFileDateTime();
0730: // TODO make this format driven from configuration
0731: SimpleDateFormat format = new SimpleDateFormat(
0732: "MM-dd-yyyy hh:mm aa"); //$NON-NLS-1$
0733: String fileId = file.getId();
0734: Element archiveNode = archivesNode
0735: .addElement("archive"); //$NON-NLS-1$
0736: archiveNode.addElement("id").setText(fileId); //$NON-NLS-1$
0737: archiveNode
0738: .addElement("date").setText(format.format(date)); //$NON-NLS-1$
0739: archiveNode
0740: .addElement("size").setText(Long.toString(file.getFileSize())); //$NON-NLS-1$
0741: archiveNode
0742: .addElement("mimetype").setText(contentItem.getMimeType()); //$NON-NLS-1$
0743: }
0744: }
0745: }
0746: }
0747:
0748: public Subscription getSubscriptionById(String theId) {
0749: Session session = HibernateUtil.getSession();
0750: try {
0751: return (Subscription) session
0752: .get(Subscription.class, theId);
0753: } catch (HibernateException ex) {
0754: throw new SubscriptionRepositoryException(
0755: Messages
0756: .getErrorString(
0757: "PRO_SUBSCRIPTREP.ERROR_0001_GETTING_SUBSCRIPTION", theId), ex); //$NON-NLS-1$
0758: }
0759: }
0760:
0761: public Subscription getSubscription(String subscriptionId,
0762: IPentahoSession session) {
0763: if (session == null) {
0764: // TODO surface an error
0765: return null;
0766: }
0767: if (!session.isAuthenticated()) {
0768: // TODO surface an error
0769: return null;
0770: }
0771: Subscription subscription = getSubscriptionById(subscriptionId);
0772: if (subscription == null) {
0773: // TODO surface an error
0774: return null;
0775: }
0776: if (subscription.getUser().equals(session.getName())) {
0777: return subscription;
0778: }
0779: // TODO surface an error
0780: return null;
0781: }
0782:
0783: public void init(IPentahoSession session) {
0784: HibernateUtil.beginTransaction();
0785: }
0786:
0787: public List getSchedules() {
0788: Session session = HibernateUtil.getSession();
0789: Query qry = session
0790: .getNamedQuery(
0791: "com.pentaho.repository.subscribe.Schedule.getAllSchedules").setCacheable(true); //$NON-NLS-1$
0792: return qry.list();
0793: }
0794:
0795: public List syncSchedules() throws Exception {
0796: return (subscriptionScheduler.syncSchedule(getSchedules()));
0797: }
0798:
0799: public List getSchedulesByTitle(String title) {
0800: Session session = HibernateUtil.getSession();
0801: Query qry = session
0802: .getNamedQuery(
0803: "com.pentaho.repository.subscribe.Schedule.findScheduleByTitle").setCacheable(true); //$NON-NLS-1$
0804: qry.setString("searchTerm", title); //$NON-NLS-1$
0805: return qry.list();
0806: }
0807:
0808: public Schedule getScheduleByScheduleReference(
0809: String scheduleReference) {
0810: Session session = HibernateUtil.getSession();
0811: Query qry = session
0812: .getNamedQuery(
0813: "com.pentaho.repository.subscribe.Schedule.findSchedulesByReference").setCacheable(true); //$NON-NLS-1$
0814: qry.setString("searchTerm", scheduleReference); //$NON-NLS-1$
0815: return (Schedule) qry.uniqueResult();
0816: }
0817:
0818: public boolean deleteSubscription(String subscriptionId,
0819: IPentahoSession session) {
0820: if (session == null) {
0821: return false;
0822: }
0823: if (!session.isAuthenticated()) {
0824: return false;
0825: }
0826: Subscription subscription = getSubscription(subscriptionId,
0827: session);
0828: if (subscription == null) {
0829: // TODO throw a warning here
0830: return false;
0831: }
0832: if (subscription.getUser().equals(session.getName())) {
0833:
0834: HibernateUtil.makeTransient(subscription);
0835: return true;
0836: } else {
0837: // TODO throw an error here
0838: return false;
0839: }
0840: }
0841:
0842: public boolean deleteSubscription(String subscriptionId)
0843: throws Exception {
0844: Subscription subscription = getSubscriptionById(subscriptionId);
0845: if (subscription == null) {
0846: throw new SubscriptionRepositoryException(
0847: Messages
0848: .getString(
0849: "PRO_SUBSCRIPTREP.SUBSCRIPTION_ID_NOT_FOUND", subscriptionId)); //$NON-NLS-1$ }
0850: }
0851: HibernateUtil.makeTransient(subscription);
0852: return true;
0853: }
0854:
0855: public boolean deleteSchedule(Schedule sched) throws Exception {
0856: if (sched == null) {
0857: return (false);
0858: }
0859:
0860: List contentList = getContentBySchedule(sched);
0861: for (int i = 0; i < contentList.size(); ++i) {
0862: SubscribeContent content = (SubscribeContent) contentList
0863: .get(i);
0864: deleteContentForSchedule(content, sched);
0865: }
0866:
0867: HibernateUtil.makeTransient(sched);
0868: HibernateUtil.flushSession();
0869: subscriptionScheduler.syncSchedule(
0870: sched.getScheduleReference(), null);
0871: return true;
0872: }
0873:
0874: public boolean deleteScheduleById(String scheduleId)
0875: throws Exception {
0876: Schedule sched = getSchedule(scheduleId);
0877: if (sched == null) {
0878: throw new SubscriptionRepositoryException(
0879: Messages
0880: .getString(
0881: "PRO_SUBSCRIPTREP.SCHEDULE_ID_NOT_FOUND", scheduleId)); //$NON-NLS-1$ }
0882: }
0883: return deleteSchedule(sched);
0884: }
0885:
0886: public boolean deleteSubscribeContent(SubscribeContent subContent)
0887: throws Exception {
0888: HibernateUtil.makeTransient(subContent);
0889: HibernateUtil.flushSession();
0890: subscriptionScheduler.syncSchedule(subContent
0891: .getActionReference(), null);
0892: clearSessionCaches();
0893: return true;
0894: }
0895:
0896: public boolean deleteSubscribeContentById(String subContentId)
0897: throws Exception {
0898: SubscribeContent content = getContentById(subContentId);
0899: return deleteSubscribeContent(content);
0900: }
0901:
0902: public void addSchedule(Schedule schedule) throws Exception {
0903: if (getScheduleByScheduleReference(schedule
0904: .getScheduleReference()) != null) {
0905: throw new SubscriptionRepositoryException(
0906: Messages
0907: .getString(
0908: "PRO_SUBSCRIPTREP.SCHEDULE_REF_NOT_UNIQUE_ADD", schedule.getScheduleReference())); //$NON-NLS-1$
0909: }
0910: HibernateUtil.beginTransaction();
0911: Session session = HibernateUtil.getSession();
0912: session.save(schedule);
0913: subscriptionScheduler.syncSchedule(null, schedule);
0914: HibernateUtil.commitTransaction();
0915: }
0916:
0917: public Schedule addSchedule(String title, String scheduleRef,
0918: String description, String cronString, String group)
0919: throws Exception {
0920: Schedule newSched = new Schedule(UUIDUtil.getUUIDAsString(),
0921: title, scheduleRef, description, cronString, group);
0922: addSchedule(newSched);
0923: return (newSched);
0924: }
0925:
0926: public Schedule editSchedule(String id, String title,
0927: String scheduleRef, String description, String cronString,
0928: String group) throws Exception {
0929:
0930: try {
0931: Schedule oldSched = getSchedule(id);
0932: if (oldSched != null) {
0933: String oldScheduleReference = oldSched
0934: .getScheduleReference();
0935:
0936: // Any null values keep previous values
0937: if (scheduleRef != null) {
0938: if ((!oldScheduleReference.equals(scheduleRef))
0939: && (getScheduleByScheduleReference(scheduleRef) != null)) {
0940: throw new SubscriptionRepositoryException(
0941: Messages
0942: .getString(
0943: "PRO_SUBSCRIPTREP.SCHEDULE_REF_NOT_UNIQUE_EDIT", scheduleRef)); //$NON-NLS-1$
0944: }
0945: oldSched.setScheduleReference(scheduleRef);
0946: }
0947:
0948: if (title != null)
0949: oldSched.setTitle(title);
0950: if (description != null)
0951: oldSched.setDescription(description);
0952: if (cronString != null)
0953: oldSched.setCronString(cronString);
0954: if (group != null)
0955: oldSched.setGroup(group);
0956:
0957: subscriptionScheduler.syncSchedule(
0958: oldScheduleReference, oldSched);
0959: return (oldSched);
0960: }
0961: } catch (HibernateException ex) {
0962: throw new SubscriptionRepositoryException(
0963: Messages
0964: .getString(
0965: "PRO_SUBSCRIPTREP.CANNOT_EDIT_SCHEDULE", id), ex); //$NON-NLS-1$
0966: }
0967: return (null);
0968: }
0969:
0970: public Schedule getSchedule(String scheduleId) {
0971: // return (Schedule) scheduleMap.get( scheduleId );
0972: Session session = HibernateUtil.getSession();
0973: try {
0974: return (Schedule) session.get(Schedule.class, scheduleId);
0975: } catch (HibernateException ex) {
0976: throw new SubscriptionRepositoryException(
0977: Messages
0978: .getErrorString(
0979: "PRO_SUBSCRIPTREP.ERROR_0002_GETTING_SCHEDULE", scheduleId), ex); //$NON-NLS-1$
0980: }
0981: }
0982:
0983: public List getSubscriptionArchives(String subscriptionName,
0984: IPentahoSession session) {
0985:
0986: IContentItem contentItem = getContentItem(subscriptionName,
0987: session);
0988: if (contentItem == null) {
0989: // we have no archived versions
0990: return null;
0991: }
0992: return contentItem.getFileVersions();
0993:
0994: }
0995:
0996: public IContentItem getContentItem(String subscriptionName,
0997: IPentahoSession session) {
0998: ISubscriptionRepository subscriptionRepository = PentahoSystem
0999: .getSubscriptionRepository(session);
1000: Subscription subscription = subscriptionRepository
1001: .getSubscription(subscriptionName, session);
1002: if (subscription == null) {
1003: // TODO surface an error
1004: return null;
1005: }
1006: SubscribeContent content = subscription.getContent();
1007: Map subscriptionParameters = subscription.getParameters();
1008: PentahoSystem.ActionInfo contentInfo = PentahoSystem
1009: .parseActionString(content.getActionReference());
1010: // String solutionName = contentInfo[0];
1011: // String actionPath = contentInfo[1];
1012: String actionName = (String) subscriptionParameters
1013: .get("action"); //$NON-NLS-1$
1014: if (actionName == null) {
1015: actionName = contentInfo.getActionName();
1016: }
1017:
1018: IContentItem contentItem = getContentItem(actionName,
1019: subscriptionName, null, null, session, contentInfo
1020: .getSolutionName(), contentInfo.getPath(),
1021: false);
1022: if (contentItem == null) {
1023: return null;
1024: }
1025: return contentItem;
1026: }
1027:
1028: public IContentItem getContentItem(String contentName,
1029: String subscriptionName, String mimeType, String extension,
1030: IPentahoSession session, String solutionName,
1031: String solutionPath, boolean allowCreate) {
1032:
1033: // get an output stream to hand to the caller
1034: IContentRepository contentRepository = PentahoSystem
1035: .getContentRepository(session);
1036: if (contentRepository == null) {
1037: // error(
1038: // Messages.getErrorString("RuntimeContext.ERROR_0024_NO_CONTENT_REPOSITORY")
1039: // ); //$NON-NLS-1$
1040: return null;
1041: }
1042: String contentPath = SubscriptionHelper
1043: .getSubscriptionOutputLocation(solutionName,
1044: solutionPath, contentName);
1045: // Find the location if it's already there.
1046: IContentLocation contentLocation = null;
1047: try {
1048: contentLocation = contentRepository
1049: .getContentLocationByPath(contentPath);
1050: } catch (Exception ex) {
1051: // ignored
1052: }
1053: if (contentLocation == null && allowCreate) {
1054: contentLocation = contentRepository.newContentLocation(
1055: contentPath, subscriptionName, subscriptionName,
1056: solutionName, true);
1057: }
1058: if (contentLocation == null) {
1059: // error(
1060: // Messages.getErrorString("RuntimeContext.ERROR_0025_INVALID_CONTENT_LOCATION")
1061: // ); //$NON-NLS-1$
1062: return null;
1063: }
1064:
1065: // Get the content item from the location - if it's there.
1066: IContentItem contentItem = null;
1067: try {
1068: contentItem = contentLocation
1069: .getContentItemByName(subscriptionName);
1070: } catch (Exception ex) {
1071: // Ignored
1072: }
1073: if (contentItem == null && allowCreate) {
1074: contentItem = contentLocation.newContentItem(
1075: subscriptionName, contentName, extension, mimeType,
1076: null, IContentItem.WRITEMODE_KEEPVERSIONS);
1077: }
1078:
1079: return contentItem;
1080:
1081: }
1082:
1083: }
|