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 org.kuali.rice.core.framework.persistence.dao.GenericDao;
019import org.kuali.rice.ken.bo.NotificationBo;
020import org.kuali.rice.ken.bo.NotificationContentTypeBo;
021import org.kuali.rice.ken.service.NotificationContentTypeService;
022
023import java.util.Collection;
024import java.util.HashMap;
025import java.util.Map;
026
027//import org.apache.ojb.broker.query.QueryByCriteria;
028//import org.apache.ojb.broker.query.QueryFactory;
029//import org.kuali.rice.core.jpa.criteria.Criteria;
030
031
032/**
033 * NotificationContentTypeService implementation - uses the businessObjectDao 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    private GenericDao businessObjectDao;
038
039    /**
040     * Constructs a NotificationContentTypeServiceImpl.java.
041     * @param businessObjectDao
042     */
043    public NotificationContentTypeServiceImpl(GenericDao businessObjectDao) {
044        this.businessObjectDao = businessObjectDao;
045    }
046
047    /**
048     * @see org.kuali.rice.ken.service.NotificationContentTypeService#getNotificationContentType(java.lang.String)
049     */
050    //this is the one need to tweek on criteria
051    public NotificationContentTypeBo getNotificationContentType(String name) {
052//        Criteria c = new Criteria();
053//        c.addEqualTo("name", name);
054//        c.addEqualTo("current", true);        
055//      Criteria c = new Criteria(NotificationContentType.class.getName());
056//      c.eq("name", name);
057//      c.eq("current", true);
058        Map<String, Object> c = new HashMap<String, Object>();
059        c.put("name", name);
060        c.put("current", new Boolean(true));
061        
062        Collection<NotificationContentTypeBo> coll = businessObjectDao.findMatching(NotificationContentTypeBo.class, c);
063        if (coll.size() == 0) {
064            return null;
065        } else {
066            return coll.iterator().next();
067        }
068    }
069
070    protected int findHighestContentTypeVersion(String name) {
071        // there's probably a better way...'report'? or direct SQL
072        Map<String, Object> fields = new HashMap<String, Object>(2);
073        fields.put("name", name);
074        Collection<NotificationContentTypeBo> types = businessObjectDao.findMatchingOrderBy(NotificationContentTypeBo.class, fields, "version", false);
075        if (types.size() > 0) {
076            return types.iterator().next().getVersion();
077        }
078        return -1;
079    }
080
081    /**
082     * @see org.kuali.rice.ken.service.NotificationContentTypeService#saveNotificationContentType(org.kuali.rice.ken.bo.NotificationContentTypeBo)
083     */
084    public void saveNotificationContentType(NotificationContentTypeBo contentType) {
085        NotificationContentTypeBo previous = getNotificationContentType(contentType.getName());
086        if (previous != null) {
087            previous.setCurrent(false);
088            businessObjectDao.save(previous);
089        }
090        int lastVersion = findHighestContentTypeVersion(contentType.getName());
091        NotificationContentTypeBo next;
092        if (contentType.getId() == null) {
093            next = contentType; 
094        } else {
095            next = new NotificationContentTypeBo();
096            next.setName(contentType.getName());
097            next.setDescription(contentType.getDescription());
098            next.setNamespace(contentType.getNamespace());
099            next.setXsd(contentType.getXsd());
100            next.setXsl(contentType.getXsl());
101        }
102
103        next.setVersion(lastVersion + 1);
104        next.setCurrent(true);
105        businessObjectDao.save(next);
106        
107        // update all the old references
108        if (previous != null) {
109            Collection<NotificationBo> ns = getNotificationsOfContentType(previous);
110            for (NotificationBo n: ns) {
111                n.setContentType(next);
112                businessObjectDao.save(n);
113            }
114        }
115    }
116
117    protected Collection<NotificationBo> getNotificationsOfContentType(NotificationContentTypeBo ct) {
118        Map<String, Object> fields = new HashMap<String, Object>(1);
119        fields.put("contentType", ct.getId());
120        return businessObjectDao.findMatching(NotificationBo.class, fields);
121    }
122    /**
123     * @see org.kuali.rice.ken.service.NotificationContentTypeService#getAllCurrentContentTypes()
124     */
125    public Collection<NotificationContentTypeBo> getAllCurrentContentTypes() {
126//        Criteria c = new Criteria();
127//        c.addEqualTo("current", true);
128////            Criteria c = new Criteria(NotificationContentType.class.getName());
129////            c.eq("current", true);
130        
131        Map<String, Boolean> c = new HashMap<String, Boolean>();
132        c.put("current", new Boolean(true));
133   
134        return businessObjectDao.findMatching(NotificationContentTypeBo.class, c);
135    }
136    
137    /**
138     * @see org.kuali.rice.ken.service.NotificationContentTypeService#getAllContentTypes()
139     */
140    public Collection<NotificationContentTypeBo> getAllContentTypes() {
141        return businessObjectDao.findAll(NotificationContentTypeBo.class);
142    }
143}