Coverage Report - org.kuali.rice.core.impl.style.StyleRepositoryServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
StyleRepositoryServiceImpl
0%
0/39
0%
0/16
3.833
 
 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.core.impl.style;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 21  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 22  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 23  
 import org.kuali.rice.core.api.style.Style;
 24  
 import org.kuali.rice.core.api.style.StyleRepositoryService;
 25  
 import org.kuali.rice.core.api.util.RiceUtilities;
 26  
 import org.kuali.rice.core.impl.services.CoreImplServiceLocator;
 27  
 
 28  
 import java.io.IOException;
 29  
 import java.io.InputStream;
 30  
 import java.net.MalformedURLException;
 31  
 import java.util.List;
 32  
 
 33  
 
 34  
 /**
 35  
  * Implements generic StyleService via existing EDL style table
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  0
 public class StyleRepositoryServiceImpl implements StyleRepositoryService {
 40  0
     private static final Logger LOG = Logger.getLogger(StyleRepositoryServiceImpl.class);
 41  
 
 42  
     private static final String STYLE_CONFIG_PREFIX = "edl.style";
 43  
 
 44  
     private StyleDao styleDao;
 45  
 
 46  
     public void setStyleDao(StyleDao styleDao) {
 47  0
         this.styleDao = styleDao;
 48  0
     }
 49  
 
 50  
     /**
 51  
      * Loads the named style from the database, or (if configured) imports it from a file
 52  
      * specified via a configuration parameter with a name of the format edl.style.<styleName>
 53  
      * {@inheritDoc}
 54  
      */
 55  
     @Override
 56  
     public Style getStyle(String styleName) {
 57  0
         if (StringUtils.isBlank(styleName)) {
 58  0
             throw new RiceIllegalArgumentException("styleName was null or blank");
 59  
         }
 60  
 
 61  
         // try to fetch the style from the database
 62  0
         StyleBo style = styleDao.getStyle(styleName);
 63  
         // if it's null, look for a config param specifiying a file to load
 64  0
         if (style == null) {
 65  0
             String propertyName = STYLE_CONFIG_PREFIX + "." + styleName;
 66  0
             String location = ConfigContext.getCurrentContextConfig().getProperty(propertyName);
 67  0
             if (location != null) {
 68  
 
 69  
                 final InputStream xml;
 70  
 
 71  
                 try {
 72  0
                     xml = RiceUtilities.getResourceAsStream(location);
 73  0
                 } catch (MalformedURLException e) {
 74  0
                     throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e);
 75  0
                 } catch (IOException e) {
 76  0
                     throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location), e);
 77  0
                 }
 78  
 
 79  0
                 if (xml == null) {
 80  0
                     throw new RiceRuntimeException(getUnableToLoadMessage(propertyName, location) + ", no such file");
 81  
                 }
 82  
 
 83  0
                 LOG.info("Automatically loading style '" + styleName + "' from '" + location + "' as configured by " + propertyName);
 84  0
                 List<Style> styles = CoreImplServiceLocator.getStyleXmlLoader().parseStyles(xml);
 85  0
                 for (Style autoLoadedStyle : styles) {
 86  0
                     if (autoLoadedStyle.getName().equals(styleName)) {
 87  0
                         return autoLoadedStyle;
 88  
                     }
 89  
                 }
 90  0
                 throw new RiceRuntimeException("Failed to locate auto-loaded style '" + styleName + "' after successful parsing of file from '" + location + "' as configured by " + propertyName);
 91  
             }
 92  
         }
 93  0
         return StyleBo.to(style);
 94  
     }
 95  
 
 96  
     private String getUnableToLoadMessage(String propertyName, String location) {
 97  0
         return "unable to load resource at '" + location +
 98  
                 "' specified by configuration parameter '" + propertyName + "'";
 99  
     }
 100  
 
 101  
     @Override
 102  
     public void saveStyle(Style data) {
 103  0
         if (data == null) {
 104  0
             throw new RiceIllegalArgumentException("The given style was null.");
 105  
         }
 106  0
         StyleBo styleToUpdate = StyleBo.from(data);
 107  0
         saveStyleBo(styleToUpdate);
 108  0
     }
 109  
 
 110  
     protected void saveStyleBo(StyleBo styleBo) {
 111  0
         StyleBo existingData = styleDao.getStyle(styleBo.getName());
 112  0
         if (existingData != null) {
 113  0
             existingData.setActive(false);
 114  0
             styleDao.saveStyle(existingData);
 115  
         }
 116  0
         styleDao.saveStyle(styleBo);
 117  0
     }
 118  
 
 119  
     @Override
 120  
     public List<String> getAllStyleNames() {
 121  0
         return styleDao.getAllStyleNames();
 122  
     }
 123  
 }