001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.ken.service.impl; 017 018import static org.kuali.rice.core.api.criteria.PredicateFactory.equal; 019import static org.kuali.rice.core.api.criteria.PredicateFactory.greaterThanOrEqual; 020 021import java.util.Collection; 022import java.util.List; 023 024import org.apache.commons.lang.StringUtils; 025import org.kuali.rice.core.api.criteria.QueryByCriteria; 026import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 027import org.kuali.rice.ken.bo.NotificationBo; 028import org.kuali.rice.ken.bo.NotificationContentTypeBo; 029import org.kuali.rice.ken.service.NotificationContentTypeService; 030import org.kuali.rice.krad.data.DataObjectService; 031 032/** 033 * NotificationContentTypeService implementation - uses the dataObjectService to get at the underlying data in the stock DBMS. 034 * @author Kuali Rice Team (rice.collab@kuali.org) 035 */ 036public class NotificationContentTypeServiceImpl implements NotificationContentTypeService { 037 038 /** Service to persist data to and from the datasource. */ 039 private DataObjectService dataObjectService; 040 041 /** 042 * Constructs a NotificationContentTypeServiceImpl.java. 043 * @param dataObjectService Service to persist data to and from the datasource. 044 */ 045 public NotificationContentTypeServiceImpl(DataObjectService dataObjectService) { 046 this.dataObjectService = dataObjectService; 047 } 048 049 /** 050 * @see org.kuali.rice.ken.service.NotificationContentTypeService#getNotificationContentType(java.lang.String) 051 */ 052 //this is the one need to tweek on criteria 053 @Override 054 public NotificationContentTypeBo getNotificationContentType(String name) { 055 if (StringUtils.isBlank(name)) { 056 throw new RiceIllegalArgumentException("name is blank"); 057 } 058 059 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create(); 060 criteria.setPredicates(equal("name", name), equal("current", new Boolean(true))); 061 List<NotificationContentTypeBo> coll= dataObjectService.findMatching(NotificationContentTypeBo.class, criteria.build()).getResults(); 062 if (coll.isEmpty()) { 063 return null; 064 } else { 065 return coll.get(0); 066 } 067 } 068 069 /** 070 * Returns the highest version found for the given name or negative one if the name is not found. 071 * @param name the name to query for 072 * @return the highest version number found or negative one if the name is not found. 073 */ 074 protected int findHighestContentTypeVersion(String name) { 075 076 int highestVersion = -1; 077 078 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create(); 079 criteria.setPredicates(equal("name", name)); 080 List<NotificationContentTypeBo> ntfyCntTypes = dataObjectService.findMatching(NotificationContentTypeBo.class, criteria.build()).getResults(); 081 082 for (NotificationContentTypeBo ntfyCntType : ntfyCntTypes) { 083 if (ntfyCntType.getVersion() > highestVersion) { 084 highestVersion = ntfyCntType.getVersion(); 085 } 086 } 087 088 return highestVersion; 089 } 090 091 /** 092 * @see org.kuali.rice.ken.service.NotificationContentTypeService#saveNotificationContentType(org.kuali.rice.ken.bo.NotificationContentTypeBo) 093 */ 094 @Override 095 public void saveNotificationContentType(NotificationContentTypeBo contentType) { 096 NotificationContentTypeBo previous = getNotificationContentType(contentType.getName()); 097 if (previous != null) { 098 previous.setCurrent(false); 099 previous = dataObjectService.save(previous); 100 } 101 int lastVersion = findHighestContentTypeVersion(contentType.getName()); 102 NotificationContentTypeBo next; 103 if (contentType.getId() == null) { 104 next = contentType; 105 } else { 106 next = new NotificationContentTypeBo(); 107 next.setName(contentType.getName()); 108 next.setDescription(contentType.getDescription()); 109 next.setNamespace(contentType.getNamespace()); 110 next.setXsd(contentType.getXsd()); 111 next.setXsl(contentType.getXsl()); 112 } 113 114 next.setVersion(lastVersion + 1); 115 next.setCurrent(true); 116 next = dataObjectService.save(next); 117 118 // update all the old references 119 if (previous != null) { 120 Collection<NotificationBo> ns = getNotificationsOfContentType(previous); 121 for (NotificationBo n: ns) { 122 n.setContentType(next); 123 dataObjectService.save(n); 124 } 125 } 126 } 127 128 /** 129 * Get notifications based on content type. 130 * @param ct Notification content type 131 * @return a collection of {@link NotificationBo} for the given content type 132 */ 133 protected Collection<NotificationBo> getNotificationsOfContentType(NotificationContentTypeBo ct) { 134 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create(); 135 criteria.setPredicates(equal("contentType.id", ct.getId())); 136 return dataObjectService.findMatching(NotificationBo.class, criteria.build()).getResults(); 137 } 138 139 /** 140 * @see org.kuali.rice.ken.service.NotificationContentTypeService#getAllCurrentContentTypes() 141 */ 142 @Override 143 public Collection<NotificationContentTypeBo> getAllCurrentContentTypes() { 144 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create(); 145 criteria.setPredicates(equal("current", new Boolean(true))); 146 147 return dataObjectService.findMatching(NotificationContentTypeBo.class, criteria.build()).getResults(); 148 } 149 150 /** 151 * @see org.kuali.rice.ken.service.NotificationContentTypeService#getAllContentTypes() 152 */ 153 @Override 154 public Collection<NotificationContentTypeBo> getAllContentTypes() { 155 QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create(); 156 criteria.setPredicates(greaterThanOrEqual("version", 0)); 157 158 return dataObjectService.findMatching(NotificationContentTypeBo.class, criteria.build()).getResults(); 159 } 160 161 /** 162 * Sets the data object service 163 * @param dataObjectService {@link DataObjectService} 164 */ 165 public void setDataObjectService(DataObjectService dataObjectService) { 166 this.dataObjectService = dataObjectService; 167 } 168}