Coverage Report - org.kuali.rice.ken.service.impl.NotificationContentTypeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
NotificationContentTypeServiceImpl
0%
0/45
0%
0/12
2.143
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.ken.service.impl;
 17  
 
 18  
 import org.kuali.rice.core.framework.persistence.dao.GenericDao;
 19  
 import org.kuali.rice.ken.bo.Notification;
 20  
 import org.kuali.rice.ken.bo.NotificationContentType;
 21  
 import org.kuali.rice.ken.service.NotificationContentTypeService;
 22  
 
 23  
 import java.util.Collection;
 24  
 import java.util.HashMap;
 25  
 import java.util.Map;
 26  
 
 27  
 //import org.apache.ojb.broker.query.QueryByCriteria;
 28  
 //import org.apache.ojb.broker.query.QueryFactory;
 29  
 //import org.kuali.rice.core.jpa.criteria.Criteria;
 30  
 
 31  
 
 32  
 /**
 33  
  * NotificationContentTypeService implementation - uses the businessObjectDao to get at the underlying data in the stock DBMS.
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  */
 36  
 public class NotificationContentTypeServiceImpl implements NotificationContentTypeService {
 37  
     private GenericDao businessObjectDao;
 38  
 
 39  
     /**
 40  
      * Constructs a NotificationContentTypeServiceImpl.java.
 41  
      * @param businessObjectDao
 42  
      */
 43  0
     public NotificationContentTypeServiceImpl(GenericDao businessObjectDao) {
 44  0
         this.businessObjectDao = businessObjectDao;
 45  0
     }
 46  
 
 47  
     /**
 48  
      * @see org.kuali.rice.ken.service.NotificationContentTypeService#getNotificationContentType(java.lang.String)
 49  
      */
 50  
     //this is the one need to tweek on criteria
 51  
     public NotificationContentType getNotificationContentType(String name) {
 52  
 //        Criteria c = new Criteria();
 53  
 //        c.addEqualTo("name", name);
 54  
 //        c.addEqualTo("current", true);        
 55  
 //            Criteria c = new Criteria(NotificationContentType.class.getName());
 56  
 //            c.eq("name", name);
 57  
 //            c.eq("current", true);
 58  0
             Map<String, Object> c = new HashMap<String, Object>();
 59  0
             c.put("name", name);
 60  0
             c.put("current", new Boolean(true));
 61  
             
 62  0
         Collection<NotificationContentType> coll = businessObjectDao.findMatching(NotificationContentType.class, c);
 63  0
         if (coll.size() == 0) {
 64  0
             return null;
 65  
         } else {
 66  0
             return coll.iterator().next();
 67  
         }
 68  
     }
 69  
 
 70  
     protected int findHighestContentTypeVersion(String name) {
 71  
         // there's probably a better way...'report'? or direct SQL
 72  0
         Map<String, Object> fields = new HashMap<String, Object>(2);
 73  0
         fields.put("name", name);
 74  0
         Collection<NotificationContentType> types = businessObjectDao.findMatchingOrderBy(NotificationContentType.class, fields, "version", false);
 75  0
         if (types.size() > 0) {
 76  0
             return types.iterator().next().getVersion();
 77  
         }
 78  0
         return -1;
 79  
     }
 80  
 
 81  
     /**
 82  
      * @see org.kuali.rice.ken.service.NotificationContentTypeService#saveNotificationContentType(org.kuali.rice.ken.bo.NotificationContentType)
 83  
      */
 84  
     public void saveNotificationContentType(NotificationContentType contentType) {
 85  0
         NotificationContentType previous = getNotificationContentType(contentType.getName());
 86  0
         if (previous != null) {
 87  0
             previous.setCurrent(false);
 88  0
             businessObjectDao.save(previous);
 89  
         }
 90  0
         int lastVersion = findHighestContentTypeVersion(contentType.getName());
 91  
         NotificationContentType next;
 92  0
         if (contentType.getId() == null) {
 93  0
             next = contentType; 
 94  
         } else {
 95  0
             next = new NotificationContentType();
 96  0
             next.setName(contentType.getName());
 97  0
             next.setDescription(contentType.getDescription());
 98  0
             next.setNamespace(contentType.getNamespace());
 99  0
             next.setXsd(contentType.getXsd());
 100  0
             next.setXsl(contentType.getXsl());
 101  
         }
 102  
 
 103  0
         next.setVersion(lastVersion + 1);
 104  0
         next.setCurrent(true);
 105  0
         businessObjectDao.save(next);
 106  
         
 107  
         // update all the old references
 108  0
         if (previous != null) {
 109  0
             Collection<Notification> ns = getNotificationsOfContentType(previous);
 110  0
             for (Notification n: ns) {
 111  0
                 n.setContentType(next);
 112  0
                 businessObjectDao.save(n);
 113  
             }
 114  
         }
 115  0
     }
 116  
 
 117  
     protected Collection<Notification> getNotificationsOfContentType(NotificationContentType ct) {
 118  0
         Map<String, Object> fields = new HashMap<String, Object>(1);
 119  0
         fields.put("contentType", ct.getId());
 120  0
         return businessObjectDao.findMatching(Notification.class, fields);
 121  
     }
 122  
     /**
 123  
      * @see org.kuali.rice.ken.service.NotificationContentTypeService#getAllCurrentContentTypes()
 124  
      */
 125  
     public Collection<NotificationContentType> 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  0
             Map<String, Boolean> c = new HashMap<String, Boolean>();
 132  0
             c.put("current", new Boolean(true));
 133  
    
 134  0
         return businessObjectDao.findMatching(NotificationContentType.class, c);
 135  
     }
 136  
     
 137  
     /**
 138  
      * @see org.kuali.rice.ken.service.NotificationContentTypeService#getAllContentTypes()
 139  
      */
 140  
     public Collection<NotificationContentType> getAllContentTypes() {
 141  0
         return businessObjectDao.findAll(NotificationContentType.class);
 142  
     }
 143  
 }