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