SubscriptionRepository.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
com.pentaho.repository.subscribe |
![]() |
![]() |
Pentaho |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Copyright 2005 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*
* @created Oct 7, 2005
* @author James Dixon
*/
package com.pentaho.repository.subscribe;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.pentaho.core.cache.CacheManager;
import org.pentaho.core.repository.IContentItem;
import org.pentaho.core.repository.IContentItemFile;
import org.pentaho.core.repository.IContentLocation;
import org.pentaho.core.repository.IContentRepository;
import org.pentaho.core.session.IPentahoSession;
import org.pentaho.core.subscribe.ISubscriptionScheduler;
import org.pentaho.core.subscribe.SubscriptionHelper;
import org.pentaho.core.subscribe.SubscriptionResultSet;
import org.pentaho.core.system.PentahoSystem;
import org.pentaho.messages.Messages;
import org.pentaho.repository.HibernateUtil;
import org.pentaho.util.UUIDUtil;
public class SubscriptionRepository implements ISubscriptionRepository {
private static byte[] SyncLock = new byte[0];
private static final Log logger = LogFactory.getLog(SubscriptionRepository.class);
private static ISubscriptionScheduler subscriptionScheduler = PentahoSystem.getSubscriptionScheduler();
public Log getLogger() {
return logger;
}
public SubscriptionRepository() {
// TODO make this based on configuration
HibernateUtil.beginTransaction();
}
public Element importSchedules(Document doc) {
Element resultElement = DocumentHelper.createElement("importResults"); //$NON-NLS-1$
if (doc == null) {
Element ele = resultElement
.addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.DOCUMENT_IS_NULL")); //$NON-NLS-1$ //$NON-NLS-2$
ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
return (resultElement);
}
List scheduleNodes = doc.selectNodes("//schedules/*"); //$NON-NLS-1$
if (scheduleNodes.size() == 0) {
Element ele = resultElement
.addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.NO_SCHEDULES_DEFINED")); //$NON-NLS-1$ //$NON-NLS-2$
ele.addAttribute("result", "WARNING"); //$NON-NLS-1$ //$NON-NLS-2$
return (resultElement);
}
synchronized (SyncLock) {
Node scheduleNode = null;
String schedName, schedDesc, schedRef, cronString, schedGroup;
Schedule aSchedule;
// List schedList;
try {
logger.info(Messages.getString(
"PRO_SUBSCRIPTREP.USER_IMPORT_PROCESSING_SCHEDULES", Integer.toString(scheduleNodes.size()))); //$NON-NLS-1$
for (int i = 0; i < scheduleNodes.size(); i++) {
scheduleNode = (Node) scheduleNodes.get(i);
schedRef = scheduleNode.selectSingleNode("@ref").getText(); //$NON-NLS-1$
schedName = scheduleNode.selectSingleNode("@name").getText(); //$NON-NLS-1$
schedDesc = scheduleNode.selectSingleNode("@description").getText(); //$NON-NLS-1$
schedGroup = scheduleNode.selectSingleNode("@group").getText(); //$NON-NLS-1$
cronString = scheduleNode.getText();
try {
aSchedule = getScheduleByScheduleReference(schedRef);
if (aSchedule != null) {
aSchedule.setCronString(cronString);
aSchedule.setDescription(schedDesc);
aSchedule.setGroup(schedGroup);
aSchedule.setTitle(schedName);
resultElement.addElement("modified").addText(schedRef); //$NON-NLS-1$
logger.info(Messages.getString("PRO_SUBSCRIPTREP.MODIFIED_SUBSCRIPTION_SCHEDULE", schedRef)); //$NON-NLS-1$
} else {
aSchedule = addSchedule(schedName, schedRef, schedDesc, cronString, schedGroup);
resultElement.addElement("added").addText(schedRef); //$NON-NLS-1$
logger.info(Messages.getString("PRO_SUBSCRIPTREP.ADDED_SUBSCRIPTION_SCHEDULE", schedRef)); //$NON-NLS-1$
}
subscriptionScheduler.getCronSummary(cronString); // Throws an exception if invalid
} catch (Exception e) {
resultElement
.addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_OCCURRED_WITH_SCHEDULE", schedRef, e.getLocalizedMessage())); //$NON-NLS-1$ //$NON-NLS-2$
logger.warn(Messages.getString("PRO_SUBSCRIPTREP.EXCEPTION_WITH_SCHEDULE", schedRef), e); //$NON-NLS-1$
}
}
} catch (Exception e) {
Element ele = resultElement
.addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_PROCESSING_IMPORTS") + e.getLocalizedMessage()); //$NON-NLS-1$ //$NON-NLS-2$
ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
logger.error(Messages.getString("PRO_SUBSCRIPTREP.EXCEPTION_PROCESSING_IMPORTS"), e); //$NON-NLS-1$
return (resultElement);
}
}
return (resultElement);
}
public Element importContent(Document doc) {
Element resultElement = DocumentHelper.createElement("importResults"); //$NON-NLS-1$
if (doc == null) {
Element ele = resultElement
.addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.DOCUMENT_IS_NULL")); //$NON-NLS-1$ //$NON-NLS-2$
ele.addAttribute("result", "ERROR"); //$NON-NLS-1$//$NON-NLS-2$
return (resultElement);
}
List contentNodes = doc.selectNodes("//subscription-content/*"); //$NON-NLS-1$
if (contentNodes.size() == 0) {
Element ele = resultElement
.addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.NO_SCHEDULES_DEFINED")); //$NON-NLS-1$ //$NON-NLS-2$
ele.addAttribute("result", "WARNING"); //$NON-NLS-1$//$NON-NLS-2$
return (resultElement);
}
synchronized (SyncLock) {
Node contentNode = null;
Node tempNode = null;
String actionRef, contentType, allowAllSchedules;
SubscribeContent subscribeContent = null;
List schedList = getSchedules();
Map scheduleMap = new HashMap();
Map groupMap = new HashMap();
for (int j = 0; j < schedList.size(); ++j) {
Schedule aSchedule = (Schedule) schedList.get(j);
scheduleMap.put(aSchedule.getScheduleReference(), aSchedule);
List groupList = (List) groupMap.get(aSchedule.getGroup());
if (groupList == null) {
groupList = new ArrayList();
groupMap.put(aSchedule.getGroup(), groupList);
}
groupList.add(aSchedule);
}
try {
logger.info(Messages.getString(
"PRO_SUBSCRIPTREP.USER_PROCESSING_CONTENT_NODES", Integer.toString(contentNodes.size()))); //$NON-NLS-1$
for (int i = 0; i < contentNodes.size(); i++) {
contentNode = (Node) contentNodes.get(i);
actionRef = contentNode.selectSingleNode("@action").getText(); //$NON-NLS-1$
contentType = contentNode.selectSingleNode("@type").getText(); //$NON-NLS-1$
tempNode = contentNode.selectSingleNode("@allowAllSchedules"); //$NON-NLS-1$
if (tempNode != null) {
allowAllSchedules = tempNode.getText();
} else {
allowAllSchedules = "false"; //$NON-NLS-1$
}
try {
subscribeContent = getContentByActionReference(actionRef);
if (subscribeContent != null) {
subscribeContent.setType(contentType);
resultElement.addElement("modified").addText(actionRef); //$NON-NLS-1$
logger.info(Messages.getString("PRO_SUBSCRIPTREP.MODIFIED_SUBSCRIPTION_CONTENT", actionRef)); //$NON-NLS-1$
} else {
subscribeContent = addContent(actionRef, contentType);
resultElement.addElement("added").addText(actionRef); //$NON-NLS-1$
logger.info(Messages.getString("PRO_SUBSCRIPTREP.ADDED_SUBSCRIPTION_CONTENT", actionRef)); //$NON-NLS-1$
}
} catch (Exception e) {
resultElement
.addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_WITH_CONTENT", actionRef, e.getLocalizedMessage())); //$NON-NLS-1$ //$NON-NLS-2$
logger.warn(Messages.getString("PRO_SUBSCRIPTREP.ERROR_WITH_CONTENT_LOG", actionRef), e); //$NON-NLS-1$
}
List contentSchedules = new ArrayList();
if ("true".equalsIgnoreCase(allowAllSchedules)) { //$NON-NLS-1$
contentSchedules.addAll(schedList);
} else {
List suppliedSchedules = contentNode.selectNodes("schedule"); //$NON-NLS-1$
if (suppliedSchedules != null) {
for (int j = 0; j < suppliedSchedules.size(); j++) {
tempNode = (Node) suppliedSchedules.get(j);
String schName = tempNode.getText();
if (schName != null) {
Object aSchedule = scheduleMap.get(schName);
if (aSchedule != null) {
contentSchedules.add(aSchedule);
}
}
}
}
List suppliedGroups = contentNode.selectNodes("group"); //$NON-NLS-1$
if (suppliedGroups != null) {
for (int j = 0; j < suppliedGroups.size(); j++) {
tempNode = (Node) suppliedGroups.get(j);
String grpName = tempNode.getText();
if (grpName != null) {
List groupList = (List) groupMap.get(grpName);
if (groupList != null) {
contentSchedules.addAll(groupList);
}
}
}
}
}
HibernateUtil.beginTransaction(); // Need to do this or the schedules don't get saved if the content item is new
subscribeContent.setSchedules(contentSchedules);
HibernateUtil.commitTransaction();
}
} catch (Exception e) {
Element ele = resultElement
.addElement("message").addText(Messages.getString("PRO_SUBSCRIPTREP.ERROR_PROCESSING_IMPORTS") + e.getLocalizedMessage()); //$NON-NLS-1$ //$NON-NLS-2$
ele.addAttribute("result", "ERROR"); //$NON-NLS-1$ //$NON-NLS-2$
logger.error(Messages.getString("PRO_SUBSCRIPTREP.EXCEPTION_PROCESSING_IMPORTS"), e); //$NON-NLS-1$
return (resultElement);
}
}
return (resultElement);
}
public boolean addSubscription(Subscription subscription) {
if (subscription.getId() != null && subscription.getTitle() != null) {
HibernateUtil.beginTransaction();
Session session = HibernateUtil.getSession();
session.save(subscription);
// subscriptionMap.put( subscription.getId(), subscription );
HibernateUtil.commitTransaction();
return true;
} else {
// TODO log an error
return false;
}
}
public List getSubscriptionsForSchedule(Schedule schedule) {
if (schedule == null) {
return new ArrayList();
}
Session session = HibernateUtil.getSession();
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.Schedule.findSubscriptionsBySchedule").setCacheable(true); //$NON-NLS-1$
qry.setEntity("schedule", schedule); //$NON-NLS-1$
return qry.list();
}
public List getSubscriptionsForSchedule(String scheduleId) {
return getSubscriptionsForSchedule(this.getScheduleByScheduleReference(scheduleId));
}
public SubscriptionResultSet getSubscriptions(String scheduleId, IPentahoSession session, String solution,
String path, String action, String parameterNames[]) {
SubscriptionResultSet results = new SubscriptionResultSet(scheduleId, session, parameterNames, solution, path,
action);
return results;
}
public void addContent(SubscribeContent content) {
if (getContentByActionReference(content.getActionReference()) != null) {
throw new SubscriptionRepositoryException(Messages.getString(
"PRO_SUBSCRIPTREP.ACTION_SEQUENCE_ALREADY_EXISTS", content.getActionReference())); //$NON-NLS-1$
}
HibernateUtil.beginTransaction();
Session session = HibernateUtil.getSession();
session.save(content);
HibernateUtil.commitTransaction();
clearSessionCaches();
}
private void clearSessionCaches() {
CacheManager cm = PentahoSystem.getCacheManager();
if (cm != null) {
cm.killSessionCaches();
}
}
public SubscribeContent addContent(String actionRef, String contentType) {
SubscribeContent newContent = new SubscribeContent(UUIDUtil.getUUIDAsString(), actionRef, contentType);
addContent(newContent);
return (newContent);
}
public void setContent(String actionRefs[]) throws Exception {
List keepList = new ArrayList(Arrays.asList(actionRefs)); // Strings
List allContent = getAllContent();
List deleteList = new ArrayList(); // SubscribeContent
for (int i = 0; i < allContent.size(); ++i) {
SubscribeContent content = (SubscribeContent) allContent.get(i);
if (keepList.contains(content.getActionReference())) {
keepList.remove(content.getActionReference());
} else {
deleteList.add(content);
}
}
for (int i = 0; i < keepList.size(); ++i) {
addContent((String) keepList.get(i), ""); //$NON-NLS-1$
}
for (int i = 0; i < deleteList.size(); ++i) {
deleteSubscribeContent((SubscribeContent) deleteList.get(i));
}
}
public SubscribeContent editContent(String id, String actionRef, String contentType) {
try {
SubscribeContent oldCont = getContentById(id);
if (oldCont != null) {
// Any null values keep previous values
if (actionRef != null)
oldCont.setActionReference(actionRef);
if (contentType != null)
oldCont.setType(contentType);
return (oldCont);
}
} catch (HibernateException ex) {
throw new SubscriptionRepositoryException(Messages.getString("PRO_SUBSCRIPTREP.CANNOT_EDIT_CONTENT") + id + ex); //$NON-NLS-1$
}
return (null);
}
public boolean deleteContentForSchedule(String contentId, String schedId) throws Exception {
SubscribeContent content = getContentById(contentId);
Schedule sched = getSchedule(schedId);
return (deleteContentForSchedule(content, sched));
}
public boolean deleteContentForSchedule(SubscribeContent content, Schedule sched) throws Exception {
if (content == null) {
return (false);
}
return (content.removeSchedule(sched));
}
public boolean addContentForSchedule(String contentId, String schedId) throws Exception {
SubscribeContent content = getContentById(contentId);
Schedule sched = getSchedule(schedId);
return (addContentForSchedule(content, sched));
}
public boolean addContentForSchedule(SubscribeContent content, Schedule sched) throws Exception {
if (content == null) {
return (false);
}
content.addSchedule(sched);
return (true);
}
public void setSchedulesForContent(String scheduleId[], String contentId) throws Exception {
SubscribeContent content = getContentById(contentId);
List schedList = new ArrayList();
for (int i = 0; i < scheduleId.length; ++i) {
Schedule sched = getSchedule(scheduleId[i]);
if (sched != null) {
schedList.add(sched);
}
}
content.setSchedules(schedList);
}
public void setContentForSchedule(String contentId[], String schedId) throws Exception {
Schedule sched = getSchedule(schedId);
List allContent = getAllContent();
for (int i = 0; i < contentId.length; ++i) {
SubscribeContent content = getContentById(contentId[i]);
addContentForSchedule(content, sched);
allContent.remove(content);
}
for (int i = 0; i < allContent.size(); ++i) {
SubscribeContent content = (SubscribeContent) allContent.get(i);
deleteContentForSchedule(content, sched);
}
}
public SubscribeContent getContentById(String theId) {
Session session = HibernateUtil.getSession();
try {
return (SubscribeContent) session.get(SubscribeContent.class, theId);
} catch (HibernateException ex) {
throw new SubscriptionRepositoryException(Messages.getErrorString(
"PRO_SUBSCRIPTREP.ERROR_0001_GETTING_SUBSCRIPTION", theId), ex); //$NON-NLS-1$
}
}
public List getAllContent() {
Session session = HibernateUtil.getSession();
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.SubscribeContent.getAllContent").setCacheable(true); //$NON-NLS-1$
return qry.list();
}
public SubscribeContent getContentByActionReference(String actionReference) {
Session session = HibernateUtil.getSession();
Query qry = session.getNamedQuery(
"com.pentaho.repository.subscribe.SubscribeContent.findSubscriptionContentByActionRef").setCacheable(true); //$NON-NLS-1$
qry.setString("searchTerm", actionReference); //$NON-NLS-1$
Object rtn = null;
try {
rtn = qry.uniqueResult();
} catch (Exception ignored) {
}
return (SubscribeContent) rtn;
}
public List getContentBySchedule(Schedule schedule) {
Session session = HibernateUtil.getSession();
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.SubscribeContent.findContentBySchedule").setCacheable(true); //$NON-NLS-1$
qry.setParameter("schedule", schedule); //$NON-NLS-1$
return (qry.list());
}
public Document getUserSubscriptions(String user, String contentId, IPentahoSession session) {
Document document = DocumentHelper.createDocument();
Element subscriptionsNode = document.addElement("subscriptions"); //$NON-NLS-1$
addSubscriptionsToDocument(user, contentId, subscriptionsNode, null, session);
return document;
}
public void deleteUserSubscriptions(String user) {
//
Session session = HibernateUtil.getSession();
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.Subscription.findUserSubscriptionsForDeletion").setCacheable(true); //$NON-NLS-1$
qry.setString("searchUser", user); //$NON-NLS-1$
logger.warn(Messages.getErrorString("PRO_SUBSCRIPTREP.USER_REMOVING_USER", user)); //$NON-NLS-1$
List subscriptions = qry.list();
Subscription subscriptToDelete = null;
for (int i = 0; i < subscriptions.size(); i++) {
subscriptToDelete = (Subscription) subscriptions.get(i);
HibernateUtil.makeTransient(subscriptToDelete);
}
HibernateUtil.flushSession();
}
public List getUserSubscriptionsToContentReference(String user, String contentId) {
Session session = HibernateUtil.getSession();
SubscribeContent content = this.getContentByActionReference(contentId);
if (content == null) {
return new ArrayList();
}
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.Subscription.findUserSubscriptions").setCacheable(true); //$NON-NLS-1$
qry.setString("searchUser", user); //$NON-NLS-1$
qry.setEntity("searchContent", content); //$NON-NLS-1$
return qry.list();
}
public List getUserSubscriptions(String user) {
Session session = HibernateUtil.getSession();
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.Subscription.findAllUserSubscriptions").setCacheable(true); //$NON-NLS-1$
qry.setString("searchUser", user); //$NON-NLS-1$
return qry.list();
}
public List getAllSubscriptions() {
Session session = HibernateUtil.getSession();
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.Subscription.getAllSubscriptions").setCacheable(true); //$NON-NLS-1$
return qry.list();
}
public void addSubscriptionsToDocument(String user, String contentId, Element subscriptionsNode, String editId,
IPentahoSession session) {
List userSubsToContent = getUserSubscriptionsToContentReference(user, contentId);
if (userSubsToContent == null) {
return;
}
for (int i = 0; i < userSubsToContent.size(); i++) {
Subscription subscription = (Subscription) userSubsToContent.get(i);
addSubscriptionToDocument(subscription, subscriptionsNode, editId, session);
}
}
/**
* Returns true if the subscription name is unique for the user/contentId
*
* @param Name
* the schedule name to check for uniqueness
* @param user
* the user that owns the schedules
* @param contentId
* The action sequence separated by slashes
* @return
*/
public boolean checkUniqueSubscriptionName(String name, String user, String contentId) {
if (name == null) {
return true;
}
List userSubsToContent = getUserSubscriptionsToContentReference(user, contentId);
if (userSubsToContent == null) {
return true;
}
for (int i = 0; i < userSubsToContent.size(); i++) {
Subscription subscription = (Subscription) userSubsToContent.get(i);
if (name.equals(subscription.getTitle())) {
return false;
}
}
return true;
}
public void addSchedulesToDocument(String user, String contentId, Element schedulesNode, String editId) {
// now see if there are schedules
SubscribeContent content = this.getContentByActionReference(contentId);
if (content == null) {
return;
}
List schedules = content.getSchedules();
Iterator scheduleIterator = schedules.iterator();
Schedule schedule;
while (scheduleIterator.hasNext()) {
schedule = (Schedule) scheduleIterator.next();
if (schedule != null) {
Element scheduleNode = schedulesNode.addElement("schedule"); //$NON-NLS-1$
scheduleNode.addElement("id").setText(schedule.getId()); //$NON-NLS-1$
scheduleNode.addElement("title").setText(schedule.getTitle()); //$NON-NLS-1$
scheduleNode.addElement("group").setText(schedule.getGroup()); //$NON-NLS-1$
scheduleNode.addElement("schedRef").setText(schedule.getScheduleReference()); //$NON-NLS-1$
// see if this schedule is currently selected
/*
* if( selectedSchedules.contains( schedule.getId() ) ) {
* scheduleNode.addAttribute( "selected", "true" );
* //$NON-NLS-1$//$NON-NLS-2$ }
*/
}
}
}
public void addSubscriptionToDocument(Subscription subscription, Element subscriptionsNode, String editId,
IPentahoSession session) {
Element node = subscriptionsNode.addElement("subscription"); //$NON-NLS-1$
if (subscription.getTitle() == null) {
node.addElement("title").setText("Unknown"); //$NON-NLS-1$ //$NON-NLS-2$
} else {
node.addElement("title").setText(subscription.getTitle()); //$NON-NLS-1$
}
node.addElement("content-id").setText(subscription.getContent().getId()); //$NON-NLS-1$
node.addElement("id").setText(subscription.getId()); //$NON-NLS-1$
node.addElement("user").setText(subscription.getUser()); //$NON-NLS-1$
if (editId != null && editId.equals(subscription.getId())) {
node.addAttribute("edit", "true"); //$NON-NLS-1$//$NON-NLS-2$
}
int type = subscription.getType();
switch (type) {
case Subscription.TYPE_PERSONAL:
node.addElement("type").setText("personal");break; //$NON-NLS-1$ //$NON-NLS-2$
case Subscription.TYPE_GROUP:
node.addElement("type").setText("group");break; //$NON-NLS-1$ //$NON-NLS-2$
case Subscription.TYPE_ROLE:
node.addElement("type").setText("role");break; //$NON-NLS-1$ //$NON-NLS-2$
}
Map parameters = subscription.getParameters();
Iterator parameterIterator = parameters.keySet().iterator();
Element parametersNode = node.addElement("parameters"); //$NON-NLS-1$
while (parameterIterator.hasNext()) {
String name = (String) parameterIterator.next();
Object value = parameters.get(name);
Element parameterNode = parametersNode.addElement("parameter"); //$NON-NLS-1$
parameterNode.addElement("name").setText(name); //$NON-NLS-1$
parameterNode.addElement("value").setText(value.toString()); //$NON-NLS-1$
}
// now add the archives
IContentItem contentItem = getContentItem(subscription.getId(), session);
if (contentItem != null) {
List contentFiles = contentItem.getFileVersions();
if (contentFiles != null && contentFiles.size() > 0) {
Element archivesNode = node.addElement("archives"); //$NON-NLS-1$
Iterator contentFileIterator = contentFiles.iterator();
while (contentFileIterator.hasNext()) {
IContentItemFile file = (IContentItemFile) contentFileIterator.next();
Date date = file.getFileDateTime();
// TODO make this format driven from configuration
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm aa"); //$NON-NLS-1$
String fileId = file.getId();
Element archiveNode = archivesNode.addElement("archive"); //$NON-NLS-1$
archiveNode.addElement("id").setText(fileId); //$NON-NLS-1$
archiveNode.addElement("date").setText(format.format(date)); //$NON-NLS-1$
archiveNode.addElement("size").setText(Long.toString(file.getFileSize())); //$NON-NLS-1$
archiveNode.addElement("mimetype").setText(contentItem.getMimeType()); //$NON-NLS-1$
}
}
}
}
public Subscription getSubscriptionById(String theId) {
Session session = HibernateUtil.getSession();
try {
return (Subscription) session.get(Subscription.class, theId);
} catch (HibernateException ex) {
throw new SubscriptionRepositoryException(Messages.getErrorString(
"PRO_SUBSCRIPTREP.ERROR_0001_GETTING_SUBSCRIPTION", theId), ex); //$NON-NLS-1$
}
}
public Subscription getSubscription(String subscriptionId, IPentahoSession session) {
if (session == null) {
// TODO surface an error
return null;
}
if (!session.isAuthenticated()) {
// TODO surface an error
return null;
}
Subscription subscription = getSubscriptionById(subscriptionId);
if (subscription == null) {
// TODO surface an error
return null;
}
if (subscription.getUser().equals(session.getName())) {
return subscription;
}
// TODO surface an error
return null;
}
public void init(IPentahoSession session) {
HibernateUtil.beginTransaction();
}
public List getSchedules() {
Session session = HibernateUtil.getSession();
Query qry = session.getNamedQuery("com.pentaho.repository.subscribe.Schedule.getAllSchedules").setCacheable(true); //$NON-NLS-1$
return qry.list();
}
public List syncSchedules() throws Exception {
return (subscriptionScheduler.syncSchedule(getSchedules()));
}
public List getSchedulesByTitle(String title) {
Session session = HibernateUtil.getSession();
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.Schedule.findScheduleByTitle").setCacheable(true); //$NON-NLS-1$
qry.setString("searchTerm", title); //$NON-NLS-1$
return qry.list();
}
public Schedule getScheduleByScheduleReference(String scheduleReference) {
Session session = HibernateUtil.getSession();
Query qry = session
.getNamedQuery("com.pentaho.repository.subscribe.Schedule.findSchedulesByReference").setCacheable(true); //$NON-NLS-1$
qry.setString("searchTerm", scheduleReference); //$NON-NLS-1$
return (Schedule) qry.uniqueResult();
}
public boolean deleteSubscription(String subscriptionId, IPentahoSession session) {
if (session == null) {
return false;
}
if (!session.isAuthenticated()) {
return false;
}
Subscription subscription = getSubscription(subscriptionId, session);
if (subscription == null) {
// TODO throw a warning here
return false;
}
if (subscription.getUser().equals(session.getName())) {
HibernateUtil.makeTransient(subscription);
return true;
} else {
// TODO throw an error here
return false;
}
}
public boolean deleteSubscription(String subscriptionId) throws Exception {
Subscription subscription = getSubscriptionById(subscriptionId);
if (subscription == null) {
throw new SubscriptionRepositoryException(Messages.getString(
"PRO_SUBSCRIPTREP.SUBSCRIPTION_ID_NOT_FOUND", subscriptionId)); //$NON-NLS-1$ }
}
HibernateUtil.makeTransient(subscription);
return true;
}
public boolean deleteSchedule(Schedule sched) throws Exception {
if (sched == null) {
return (false);
}
List contentList = getContentBySchedule(sched);
for (int i = 0; i < contentList.size(); ++i) {
SubscribeContent content = (SubscribeContent) contentList.get(i);
deleteContentForSchedule(content, sched);
}
HibernateUtil.makeTransient(sched);
HibernateUtil.flushSession();
subscriptionScheduler.syncSchedule(sched.getScheduleReference(), null);
return true;
}
public boolean deleteScheduleById(String scheduleId) throws Exception {
Schedule sched = getSchedule(scheduleId);
if (sched == null) {
throw new SubscriptionRepositoryException(Messages
.getString("PRO_SUBSCRIPTREP.SCHEDULE_ID_NOT_FOUND", scheduleId)); //$NON-NLS-1$ }
}
return deleteSchedule(sched);
}
public boolean deleteSubscribeContent(SubscribeContent subContent) throws Exception {
HibernateUtil.makeTransient(subContent);
HibernateUtil.flushSession();
subscriptionScheduler.syncSchedule(subContent.getActionReference(), null);
clearSessionCaches();
return true;
}
public boolean deleteSubscribeContentById(String subContentId) throws Exception {
SubscribeContent content = getContentById(subContentId);
return deleteSubscribeContent(content);
}
public void addSchedule(Schedule schedule) throws Exception {
if (getScheduleByScheduleReference(schedule.getScheduleReference()) != null) {
throw new SubscriptionRepositoryException(Messages.getString(
"PRO_SUBSCRIPTREP.SCHEDULE_REF_NOT_UNIQUE_ADD", schedule.getScheduleReference())); //$NON-NLS-1$
}
HibernateUtil.beginTransaction();
Session session = HibernateUtil.getSession();
session.save(schedule);
subscriptionScheduler.syncSchedule(null, schedule);
HibernateUtil.commitTransaction();
}
public Schedule addSchedule(String title, String scheduleRef, String description, String cronString, String group)
throws Exception {
Schedule newSched = new Schedule(UUIDUtil.getUUIDAsString(), title, scheduleRef, description, cronString, group);
addSchedule(newSched);
return (newSched);
}
public Schedule editSchedule(String id, String title, String scheduleRef, String description, String cronString,
String group) throws Exception {
try {
Schedule oldSched = getSchedule(id);
if (oldSched != null) {
String oldScheduleReference = oldSched.getScheduleReference();
// Any null values keep previous values
if (scheduleRef != null) {
if ((!oldScheduleReference.equals(scheduleRef)) && (getScheduleByScheduleReference(scheduleRef) != null)) {
throw new SubscriptionRepositoryException(Messages.getString(
"PRO_SUBSCRIPTREP.SCHEDULE_REF_NOT_UNIQUE_EDIT", scheduleRef)); //$NON-NLS-1$
}
oldSched.setScheduleReference(scheduleRef);
}
if (title != null)
oldSched.setTitle(title);
if (description != null)
oldSched.setDescription(description);
if (cronString != null)
oldSched.setCronString(cronString);
if (group != null)
oldSched.setGroup(group);
subscriptionScheduler.syncSchedule(oldScheduleReference, oldSched);
return (oldSched);
}
} catch (HibernateException ex) {
throw new SubscriptionRepositoryException(Messages.getString("PRO_SUBSCRIPTREP.CANNOT_EDIT_SCHEDULE", id), ex); //$NON-NLS-1$
}
return (null);
}
public Schedule getSchedule(String scheduleId) {
// return (Schedule) scheduleMap.get( scheduleId );
Session session = HibernateUtil.getSession();
try {
return (Schedule) session.get(Schedule.class, scheduleId);
} catch (HibernateException ex) {
throw new SubscriptionRepositoryException(Messages.getErrorString(
"PRO_SUBSCRIPTREP.ERROR_0002_GETTING_SCHEDULE", scheduleId), ex); //$NON-NLS-1$
}
}
public List getSubscriptionArchives(String subscriptionName, IPentahoSession session) {
IContentItem contentItem = getContentItem(subscriptionName, session);
if (contentItem == null) {
// we have no archived versions
return null;
}
return contentItem.getFileVersions();
}
public IContentItem getContentItem(String subscriptionName, IPentahoSession session) {
ISubscriptionRepository subscriptionRepository = PentahoSystem.getSubscriptionRepository(session);
Subscription subscription = subscriptionRepository.getSubscription(subscriptionName, session);
if (subscription == null) {
// TODO surface an error
return null;
}
SubscribeContent content = subscription.getContent();
Map subscriptionParameters = subscription.getParameters();
PentahoSystem.ActionInfo contentInfo = PentahoSystem.parseActionString(content.getActionReference());
// String solutionName = contentInfo[0];
// String actionPath = contentInfo[1];
String actionName = (String) subscriptionParameters.get("action"); //$NON-NLS-1$
if (actionName == null) {
actionName = contentInfo.getActionName();
}
IContentItem contentItem = getContentItem(actionName, subscriptionName, null, null, session, contentInfo
.getSolutionName(), contentInfo.getPath(), false);
if (contentItem == null) {
return null;
}
return contentItem;
}
public IContentItem getContentItem(String contentName, String subscriptionName, String mimeType, String extension,
IPentahoSession session, String solutionName, String solutionPath, boolean allowCreate) {
// get an output stream to hand to the caller
IContentRepository contentRepository = PentahoSystem.getContentRepository(session);
if (contentRepository == null) {
// error(
// Messages.getErrorString("RuntimeContext.ERROR_0024_NO_CONTENT_REPOSITORY")
// ); //$NON-NLS-1$
return null;
}
String contentPath = SubscriptionHelper.getSubscriptionOutputLocation(solutionName, solutionPath, contentName);
// Find the location if it's already there.
IContentLocation contentLocation = null;
try {
contentLocation = contentRepository.getContentLocationByPath(contentPath);
} catch (Exception ex) {
// ignored
}
if (contentLocation == null && allowCreate) {
contentLocation = contentRepository.newContentLocation(contentPath, subscriptionName, subscriptionName,
solutionName, true);
}
if (contentLocation == null) {
// error(
// Messages.getErrorString("RuntimeContext.ERROR_0025_INVALID_CONTENT_LOCATION")
// ); //$NON-NLS-1$
return null;
}
// Get the content item from the location - if it's there.
IContentItem contentItem = null;
try {
contentItem = contentLocation.getContentItemByName(subscriptionName);
} catch (Exception ex) {
// Ignored
}
if (contentItem == null && allowCreate) {
contentItem = contentLocation.newContentItem(subscriptionName, contentName, extension, mimeType, null,
IContentItem.WRITEMODE_KEEPVERSIONS);
}
return contentItem;
}
}
The table below shows all metrics for SubscriptionRepository.java.




