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