Coverage Report - org.kuali.rice.core.impl.style.StyleRepositoryServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
StyleRepositoryServiceImpl
56%
26/46
50%
6/12
2.182
 
 1  
 /*
 2  
  * Copyright 2006-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  
 
 17  
 package org.kuali.rice.core.impl.style;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.net.MalformedURLException;
 22  
 import java.util.List;
 23  
 
 24  
 import javax.xml.transform.Templates;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.apache.log4j.Logger;
 28  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 29  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 30  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 31  
 import org.kuali.rice.core.api.style.Style;
 32  
 import org.kuali.rice.core.api.style.StyleRepositoryService;
 33  
 import org.kuali.rice.core.impl.services.CoreImplServiceLocator;
 34  
 import org.kuali.rice.core.util.RiceUtilities;
 35  
 import org.kuali.rice.ksb.cache.RiceCacheAdministrator;
 36  
 
 37  
 
 38  
 /**
 39  
  * Implements generic StyleService via existing EDL style table
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  */
 42  7
 public class StyleRepositoryServiceImpl implements StyleRepositoryService {
 43  1
     private static final Logger LOG = Logger.getLogger(StyleRepositoryServiceImpl.class);
 44  
 
 45  
     static final String TEMPLATES_CACHE_GROUP_NAME = "Templates";
 46  
     private static final String STYLE_CONFIG_PREFIX = "edl.style";
 47  
 
 48  
     private StyleDao styleDao;
 49  
     private RiceCacheAdministrator cache;
 50  
 
 51  
     public void setStyleDao(StyleDao styleDao) {
 52  10
         this.styleDao = styleDao;
 53  10
     }
 54  
     
 55  
     public void setCache(RiceCacheAdministrator cache) {
 56  7
             this.cache = cache;
 57  7
     }
 58  
 
 59  
     /**
 60  
      * Loads the named style from the database, or (if configured) imports it from a file
 61  
      * specified via a configuration parameter with a name of the format edl.style.<styleName>
 62  
      * {@inheritDoc}
 63  
      * @see org.kuali.rice.edl.impl.service.StyleService#getStyle(java.lang.String)
 64  
      */
 65  
     @Override
 66  
     public Style getStyle(String styleName) {
 67  5
             if (StringUtils.isBlank(styleName)) {
 68  3
                     throw new RiceIllegalArgumentException("styleName was null or blank");
 69  
             }
 70  
             
 71  
             // try to fetch the style from the database
 72  2
         StyleBo style = styleDao.getStyle(styleName);
 73  
         // if it's null, look for a config param specifiying a file to load
 74  2
         if (style == null) {
 75  0
             String propertyName = STYLE_CONFIG_PREFIX + "." + styleName;
 76  0
             String location = ConfigContext.getCurrentContextConfig().getProperty(propertyName);
 77  0
             if (location != null) {
 78  
                     
 79  0
                 InputStream xml = null;
 80  
 
 81  
                 try {
 82  0
                     xml = RiceUtilities.getResourceAsStream(location);
 83  0
                 } catch (MalformedURLException e) {
 84  0
                     throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e);
 85  0
                 } catch (IOException e) {
 86  0
                     throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e);
 87  0
                 }
 88  
 
 89  0
                 if (xml == null) {
 90  0
                     throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location) + ", no such file");
 91  
                 }
 92  
                 
 93  0
                 LOG.info("Automatically importing style '" + styleName + "' from '" + location + "' as configured by "+ propertyName);
 94  0
                 CoreImplServiceLocator.getStyleXmlLoader().loadXml(xml, null);
 95  
             }
 96  
         }
 97  2
         return StyleBo.to(style);
 98  
     }
 99  
 
 100  
     /**
 101  
      * This method ...
 102  
      *
 103  
      * @param propertyName
 104  
      * @param location
 105  
      * @return
 106  
      */
 107  
     private String getUnableToLoadMessage(String propertyName, String location) {
 108  0
         return "unable to load resource at '" + location +
 109  
                 "' specified by configuration parameter '" + propertyName + "'";
 110  
     }
 111  
 
 112  
     /**
 113  
      * Does not currently take into account style sheet dependences robustly
 114  
      */
 115  
     private void removeStyleFromCache(String styleName) {
 116  2
         LOG.info("Removing Style " + styleName + " from the style cache");
 117  
         // we don't know what styles may import other styles so we need to flush them all
 118  2
         cache.flushGroup(TEMPLATES_CACHE_GROUP_NAME);
 119  
         //KEWServiceLocator.getCacheAdministrator().flushEntry(getTemplatesCacheKey(styleName));
 120  2
     }
 121  
 
 122  
     @Override
 123  
     public void saveStyle(Style data) {
 124  3
             if (data == null) {
 125  1
                     throw new RiceIllegalArgumentException("The given style was null.");
 126  
             }
 127  2
             StyleBo styleToUpdate = StyleBo.from(data);
 128  2
             saveStyleBo(styleToUpdate);
 129  2
     }
 130  
     
 131  
     protected void saveStyleBo(StyleBo styleBo) {
 132  2
             StyleBo existingData = styleDao.getStyle(styleBo.getName());
 133  2
         if (existingData != null) {
 134  2
             existingData.setActive(false);
 135  2
             styleDao.saveStyle(existingData);
 136  
         }
 137  2
         styleDao.saveStyle(styleBo);
 138  2
         removeStyleFromCache(styleBo.getName());
 139  2
     }
 140  
     
 141  
     @Override
 142  
     public List<String> getAllStyleNames() {
 143  0
         return styleDao.getAllStyleNames();
 144  
     }
 145  
     
 146  
     // cache helper methods
 147  
 
 148  
     /**
 149  
      * Returns the key to be used for caching the Templates for the given style name.
 150  
      */
 151  
     protected String getTemplatesCacheKey(String styleName) {
 152  0
         return TEMPLATES_CACHE_GROUP_NAME + ":" + styleName;
 153  
     }
 154  
 
 155  
     protected Templates fetchTemplatesFromCache(String styleName) {
 156  0
         return (Templates) cache.getFromCache(getTemplatesCacheKey(styleName));
 157  
     }
 158  
 
 159  
     protected void putTemplatesInCache(String styleName, Templates templates) {
 160  0
         cache.putInCache(getTemplatesCacheKey(styleName), templates, TEMPLATES_CACHE_GROUP_NAME);
 161  0
     }
 162  
 
 163  
 }