Coverage Report - org.kuali.rice.kew.preferences.service.impl.PreferencesServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PreferencesServiceImpl
0%
0/141
0%
0/28
4.8
 
 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.kew.preferences.service.impl;
 18  
 
 19  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 20  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 21  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
 22  
 import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
 23  
 import org.kuali.rice.kew.preferences.Preferences;
 24  
 import org.kuali.rice.kew.preferences.service.PreferencesService;
 25  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 26  
 import org.kuali.rice.kew.useroptions.UserOptions;
 27  
 import org.kuali.rice.kew.useroptions.UserOptionsService;
 28  
 import org.kuali.rice.kew.util.KEWConstants;
 29  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 30  
 
 31  
 import java.util.ArrayList;
 32  
 import java.util.Collection;
 33  
 import java.util.HashMap;
 34  
 import java.util.Map;
 35  
 
 36  
 
 37  
 /**
 38  
  * An implementation of the {@link PreferencesService}.
 39  
  *
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  */
 42  0
 public class PreferencesServiceImpl implements PreferencesService {
 43  
 
 44  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PreferencesServiceImpl.class);
 45  
 
 46  
     private static final String DISAPPROVED_DOC_COLOR = "DOCUMENT_STATUS_COLOR_D";
 47  
     private static final String DISSAPPROVED_CANCELLED_DOC_COLOR = "DOCUMENT_STATUS_COLOR_C";
 48  
     private static final String APPROVED_DOC_COLOR = "DOCUMENT_STATUS_COLOR_A";
 49  
     private static final String CANCELLED_DOC_COLOR = "DOCUMENT_STATUS_COLOR_X";
 50  
     private static final String SAVED_DOC_COLOR = "DOCUMENT_STATUS_COLOR_S";
 51  
     private static final String ENROUTE_DOC_COLOR = "DOCUMENT_STATUS_COLOR_R";
 52  
     private static final String PROCESSED_DOC_COLOR = "DOCUMENT_STATUS_COLOR_P";
 53  
     private static final String INITIATED_DOC_COLOR = "DOCUMENT_STATUS_COLOR_I";
 54  
     private static final String FINAL_DOC_COLOR = "DOCUMENT_STATUS_COLOR_F";
 55  
     private static final String EXCEPTION_DOC_COLOR = "DOCUMENT_STATUS_COLOR_E";
 56  
     private static final String REFRESH_RATE_KEY = "REFRESH_RATE";
 57  
     private static final String OPEN_NEW_WINDOW_KEY = "OPEN_ITEMS_NEW_WINDOW";
 58  
     private static final String COLUMN_DOC_TYPE_KEY = "DOC_TYPE_COL_SHOW_NEW";
 59  
     private static final String COLUMN_TITLE_KEY = "TITLE_COL_SHOW_NEW";
 60  
     private static final String COLUMN_ACTION_REQ_KEY = "ACTION_REQUESTED_COL_SHOW_NEW";
 61  
     private static final String COLUMN_INITIATOR_KEY = "INITIATOR_COL_SHOW_NEW";
 62  
     private static final String COLUMN_DELEGATOR_KEY = "DELEGATOR_COL_SHOW_NEW";
 63  
     private static final String COLUMN_DATE_CREATE_KEY = "DATE_CREATED_COL_SHOW_NEW";
 64  
     private static final String COLUMN_DOCUMENT_STATUS_KEY = "DOCUMENT_STATUS_COL_SHOW_NEW";
 65  
     private static final String COLUMN_APP_DOC_STATUS_KEY = "APP_DOC_STATUS_COL_SHOW_NEW";
 66  
     private static final String COLUMN_WORKGROUP_REQUEST_KEY = "WORKGROUP_REQUEST_COL_SHOW_NEW";
 67  
     private static final String COLUMN_CLEAR_FYI_KEY = "CLEAR_FYI_COL_SHOW_NEW";
 68  
     private static final String ACTION_LIST_SIZE_KEY = "ACTION_LIST_SIZE_NEW";
 69  
     private static final String EMAIL_REMINDER_KEY = KEWConstants.EMAIL_RMNDR_KEY;
 70  
     private static final String EMAIL_NOTIFY_PRIMARY_KEY = "EMAIL_NOTIFY_PRIMARY";
 71  
     private static final String EMAIL_NOTIFY_SECONDARY_KEY = "EMAIL_NOTIFY_SECONDARY";
 72  
     private static final String DEFAULT_COLOR = "white";
 73  
     private static final String DEFAULT_ACTION_LIST_SIZE = "10";
 74  
     private static final String DEFAULT_REFRESH_RATE = "15";
 75  
     private static final String ERR_KEY_REFRESH_RATE_WHOLE_NUM = "preferences.refreshRate";
 76  
     private static final String ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM = "preferences.pageSize";
 77  
     private static final String DELEGATOR_FILTER_KEY = "DELEGATOR_FILTER";
 78  
     private static final String PRIMARY_DELEGATE_FILTER_KEY = "PRIMARY_DELEGATE_FILTER";
 79  
     public static final String USE_OUT_BOX = "USE_OUT_BOX";
 80  
     private static final String COLUMN_LAST_APPROVED_DATE_KEY = "LAST_APPROVED_DATE_COL_SHOW_NEW";
 81  
 
 82  
     public static final String COLUMN_CURRENT_NODE_KEY = "CURRENT_NODE_COL_SHOW_NEW";
 83  
 
 84  
     public Preferences getPreferences(String principalId) {
 85  0
         if ( LOG.isDebugEnabled() ) {
 86  0
         LOG.debug("start preferences fetch user " + principalId);
 87  
         }
 88  0
         Collection<UserOptions> options = getUserOptionService().findByWorkflowUser(principalId);
 89  0
         Map<String,UserOptions> optionMap = new HashMap<String, UserOptions>();
 90  0
         for ( UserOptions option : options ) {
 91  0
                 optionMap.put(option.getOptionId(), option);
 92  
         }
 93  
         
 94  0
         ConfigurationService kcs = KRADServiceLocator.getKualiConfigurationService();
 95  
         
 96  0
         String stagingDirectory = kcs.getPropertyString("staging.directory");                                                    
 97  
                                                                                                     
 98  0
         String defaultColor =  kcs.getPropertyString("userOptions.default.color");                                                                                                  
 99  0
         String defaultEmail = kcs.getPropertyString("userOptions.default.email");                                                                                                   
 100  0
         String defaultNotifyPrimary = kcs.getPropertyString("userOptions.default.notifyPrimary");                                                                                   
 101  0
         String defaultNotifySecondary = kcs.getPropertyString("userOptions.default.notifySecondary");                                                                               
 102  0
         String defaultOpenNewWindow = kcs.getPropertyString("userOptions.default.openNewWindow");                                                                                   
 103  0
         String defaultActionListSize = kcs.getPropertyString("userOptions.default.actionListSize");                                                                                 
 104  0
         String defaultRefreshRate = kcs.getPropertyString("userOptions.default.refreshRate");                                                                                       
 105  0
         String defaultShowActionRequired = kcs.getPropertyString("userOptions.default.showActionRequired");                                                                         
 106  0
         String defaultShowDateCreated = kcs.getPropertyString("userOptions.default.showDateCreated");                                                                               
 107  0
         String defaultShowDocType = kcs.getPropertyString("userOptions.default.showDocumentType");                                                                                  
 108  0
         String defaultShowDocStatus = kcs.getPropertyString("userOptions.default.showDocumentStatus");                                                                              
 109  0
         String defaultShowInitiator = kcs.getPropertyString("userOptions.default.showInitiator");                                                                                   
 110  0
         String defaultShowDelegator = kcs.getPropertyString("userOptions.default.showDelegator");                                                                                   
 111  0
         String defaultShowTitle = kcs.getPropertyString("userOptions.default.showTitle");                                                                                           
 112  0
         String defaultShowWorkgroupRequest = kcs.getPropertyString("userOptions.default.showWorkgroupRequest");                                                                     
 113  0
         String defaultShowLastApprovedDate = kcs.getPropertyString("userOptions.default.showLastApprovedDate");                                                                     
 114  0
         String defaultShowClearFYI = kcs.getPropertyString("userOptions.default.showClearFYI");                                                                                     
 115  0
         String defaultShowCurrentNode = kcs.getPropertyString("userOptions.default.showCurrentNode");                                                                               
 116  0
         String defaultDelegatorFilterOnActionList = kcs.getPropertyString("userOptions.default.delegatorFilterOnActionList");                                                       
 117  0
         String defaultPrimaryDelegatorFilterOnActionList = kcs.getPropertyString("userOptions.default.primaryDelegatorFilterOnActionList");                                         
 118  
 
 119  0
         final String defaultUseOutBox = kcs.getPropertyString(KEWConstants.USER_OPTIONS_DEFAULT_USE_OUTBOX_PARAM);
 120  
                                                                                                                                                                                     
 121  0
         Preferences preferences = new Preferences();                                                                                                                                
 122  0
         preferences.setColorApproved(getOption(optionMap,APPROVED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                               
 123  0
         preferences.setColorCanceled(getOption(optionMap,CANCELLED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                              
 124  0
         preferences.setColorDissapproveCancel(getOption(optionMap,DISSAPPROVED_CANCELLED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                        
 125  0
         preferences.setColorDissaproved(getOption(optionMap,DISAPPROVED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                         
 126  0
         preferences.setColorEnroute(getOption(optionMap,ENROUTE_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                                 
 127  0
         preferences.setColorException(getOption(optionMap,EXCEPTION_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                             
 128  0
         preferences.setColorFinal(getOption(optionMap,FINAL_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                                     
 129  0
         preferences.setColorInitiated(getOption(optionMap,INITIATED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                             
 130  0
         preferences.setColorProccessed(getOption(optionMap,PROCESSED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                            
 131  0
         preferences.setColorSaved(getOption(optionMap,SAVED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                                     
 132  0
         preferences.setEmailNotification(getOption(optionMap,EMAIL_REMINDER_KEY, defaultEmail, principalId, preferences).getOptionVal());                                           
 133  0
         preferences.setNotifyPrimaryDelegation(getOption(optionMap,EMAIL_NOTIFY_PRIMARY_KEY, defaultNotifyPrimary, principalId, preferences).getOptionVal());                       
 134  0
         preferences.setNotifySecondaryDelegation(getOption(optionMap,EMAIL_NOTIFY_SECONDARY_KEY, defaultNotifySecondary, principalId, preferences).getOptionVal());                 
 135  0
         preferences.setOpenNewWindow(getOption(optionMap,OPEN_NEW_WINDOW_KEY, defaultOpenNewWindow, principalId, preferences).getOptionVal());                                      
 136  0
         preferences.setPageSize(getOption(optionMap,ACTION_LIST_SIZE_KEY, defaultActionListSize, principalId, preferences).getOptionVal());                                         
 137  0
         preferences.setRefreshRate(getOption(optionMap,REFRESH_RATE_KEY, defaultRefreshRate, principalId, preferences).getOptionVal());                                             
 138  0
         preferences.setShowActionRequested(getOption(optionMap,COLUMN_ACTION_REQ_KEY, defaultShowActionRequired, principalId, preferences).getOptionVal());                         
 139  0
         preferences.setShowDateCreated(getOption(optionMap,COLUMN_DATE_CREATE_KEY, defaultShowDateCreated, principalId, preferences).getOptionVal());                               
 140  0
         preferences.setShowDocType(getOption(optionMap,COLUMN_DOC_TYPE_KEY, defaultShowDocType, principalId, preferences).getOptionVal());                                          
 141  0
         preferences.setShowDocumentStatus(getOption(optionMap,COLUMN_DOCUMENT_STATUS_KEY, defaultShowDocStatus, principalId, preferences).getOptionVal());                          
 142  0
         preferences.setShowInitiator(getOption(optionMap,COLUMN_INITIATOR_KEY, defaultShowInitiator, principalId, preferences).getOptionVal());                                     
 143  0
         preferences.setShowDelegator(getOption(optionMap,COLUMN_DELEGATOR_KEY, defaultShowDelegator, principalId, preferences).getOptionVal());                                     
 144  0
         preferences.setShowDocTitle(getOption(optionMap,COLUMN_TITLE_KEY, defaultShowTitle, principalId, preferences).getOptionVal());                                              
 145  0
         preferences.setShowWorkgroupRequest(getOption(optionMap,COLUMN_WORKGROUP_REQUEST_KEY, defaultShowWorkgroupRequest, principalId, preferences).getOptionVal());               
 146  0
         preferences.setShowClearFyi(getOption(optionMap,COLUMN_CLEAR_FYI_KEY, defaultShowClearFYI, principalId, preferences).getOptionVal());                                       
 147  0
         preferences.setDelegatorFilter(getOption(optionMap,DELEGATOR_FILTER_KEY, defaultDelegatorFilterOnActionList, principalId, preferences).getOptionVal());                     
 148  0
         preferences.setPrimaryDelegateFilter(getOption(optionMap,PRIMARY_DELEGATE_FILTER_KEY, defaultPrimaryDelegatorFilterOnActionList, principalId, preferences).getOptionVal()); 
 149  0
         preferences.setShowDateApproved(getOption(optionMap,COLUMN_LAST_APPROVED_DATE_KEY, defaultShowLastApprovedDate, principalId, preferences).getOptionVal());                  
 150  0
         preferences.setShowCurrentNode(getOption(optionMap,COLUMN_CURRENT_NODE_KEY, defaultShowCurrentNode, principalId, preferences).getOptionVal());                              
 151  0
         preferences.setUseOutbox(getOption(optionMap,USE_OUT_BOX, defaultUseOutBox, principalId, preferences).getOptionVal());                                                      
 152  
 
 153  0
         if ( LOG.isDebugEnabled() ) {
 154  0
         LOG.debug("end preferences fetch user " + principalId);
 155  
         }
 156  0
         return preferences;
 157  
     }
 158  
 
 159  
     /* @see https://test.kuali.org/jira/browse/KULRICE-1726 */
 160  
     //private static ConcurrencyDetector detector = new ConcurrencyDetector("Concurrency in PreferencesServiceImpl", false);
 161  
 
 162  
     private UserOptions getOption(Map<String,UserOptions> optionsMap, String optionKey, String defaultValue, String principalId, Preferences preferences) {
 163  0
             if ( LOG.isDebugEnabled() ) {
 164  0
         LOG.debug("start fetch option " + optionKey + " user " + principalId);
 165  
             }
 166  0
         UserOptions option = optionsMap.get(optionKey);
 167  0
         if (option == null) {
 168  0
                 if ( LOG.isDebugEnabled() ) {
 169  0
             LOG.debug("User option '" + optionKey + "' on user " + principalId + " has no stored value.  Preferences will require save.");
 170  
                 }
 171  0
             option = new UserOptions();
 172  0
             option.setWorkflowId(principalId);
 173  0
             option.setOptionId(optionKey);
 174  0
             option.setOptionVal(defaultValue);
 175  0
             optionsMap.put(optionKey, option); // just in case referenced a second time
 176  0
             if ( optionKey.equals(USE_OUT_BOX) && !ConfigContext.getCurrentContextConfig().getOutBoxOn() ) {
 177  
                     // don't mark as needing save
 178  
             } else {
 179  0
             preferences.setRequiresSave(true);
 180  
         }
 181  
         }
 182  0
         if ( LOG.isDebugEnabled() ) {
 183  0
         LOG.debug("End fetch option " + optionKey + " user " + principalId);
 184  
         }
 185  0
         return option;
 186  
     }
 187  
 
 188  
     public void savePreferences(String principalId, Preferences preferences) {
 189  
             // NOTE: this previously displayed the principalName.  Now it's just the id
 190  0
             if ( LOG.isDebugEnabled() ) {
 191  0
         LOG.debug("saving preferences user " + principalId);
 192  
             }
 193  
 
 194  0
         validate(preferences);
 195  0
         Map<String,String> optionsMap = new HashMap<String,String>(50);
 196  
         
 197  0
         optionsMap.put(DISSAPPROVED_CANCELLED_DOC_COLOR, preferences.getColorDissapproveCancel());
 198  0
         optionsMap.put(DISAPPROVED_DOC_COLOR, preferences.getColorDissaproved());
 199  0
         optionsMap.put(APPROVED_DOC_COLOR, preferences.getColorApproved());
 200  0
         optionsMap.put(CANCELLED_DOC_COLOR, preferences.getColorCanceled());
 201  0
         optionsMap.put(SAVED_DOC_COLOR, preferences.getColorSaved());
 202  0
         optionsMap.put(ENROUTE_DOC_COLOR, preferences.getColorEnroute());
 203  0
         optionsMap.put(PROCESSED_DOC_COLOR, preferences.getColorProccessed());
 204  0
         optionsMap.put(INITIATED_DOC_COLOR, preferences.getColorInitiated());
 205  0
         optionsMap.put(FINAL_DOC_COLOR, preferences.getColorFinal());
 206  0
         optionsMap.put(EXCEPTION_DOC_COLOR, preferences.getColorException());
 207  0
         optionsMap.put(REFRESH_RATE_KEY, preferences.getRefreshRate().trim());
 208  0
         optionsMap.put(OPEN_NEW_WINDOW_KEY, preferences.getOpenNewWindow());
 209  0
         optionsMap.put(COLUMN_DOC_TYPE_KEY, preferences.getShowDocType());
 210  0
         optionsMap.put(COLUMN_TITLE_KEY, preferences.getShowDocTitle());
 211  0
         optionsMap.put(COLUMN_ACTION_REQ_KEY, preferences.getShowActionRequested());
 212  0
         optionsMap.put(COLUMN_INITIATOR_KEY, preferences.getShowInitiator());
 213  0
         optionsMap.put(COLUMN_DELEGATOR_KEY, preferences.getShowDelegator());
 214  0
         optionsMap.put(COLUMN_DATE_CREATE_KEY, preferences.getShowDateCreated());
 215  0
         optionsMap.put(COLUMN_DOCUMENT_STATUS_KEY, preferences.getShowDocumentStatus());
 216  0
         optionsMap.put(COLUMN_APP_DOC_STATUS_KEY, preferences.getShowAppDocStatus());
 217  0
         optionsMap.put(COLUMN_WORKGROUP_REQUEST_KEY, preferences.getShowWorkgroupRequest());
 218  0
         optionsMap.put(COLUMN_CLEAR_FYI_KEY, preferences.getShowClearFyi());
 219  0
         optionsMap.put(ACTION_LIST_SIZE_KEY, preferences.getPageSize().trim());
 220  0
         optionsMap.put(EMAIL_REMINDER_KEY, preferences.getEmailNotification());
 221  0
         optionsMap.put(EMAIL_NOTIFY_PRIMARY_KEY, preferences.getNotifyPrimaryDelegation());
 222  0
         optionsMap.put(EMAIL_NOTIFY_SECONDARY_KEY, preferences.getNotifySecondaryDelegation());
 223  0
         optionsMap.put(DELEGATOR_FILTER_KEY, preferences.getDelegatorFilter());
 224  0
         optionsMap.put(PRIMARY_DELEGATE_FILTER_KEY, preferences.getPrimaryDelegateFilter());
 225  0
         optionsMap.put(COLUMN_LAST_APPROVED_DATE_KEY, preferences.getShowDateApproved());
 226  0
         optionsMap.put(COLUMN_CURRENT_NODE_KEY, preferences.getShowCurrentNode());
 227  0
         if (ConfigContext.getCurrentContextConfig().getOutBoxOn()) {
 228  0
             optionsMap.put(USE_OUT_BOX, preferences.getUseOutbox());
 229  
         }
 230  0
         getUserOptionService().save(principalId, optionsMap);
 231  0
         if ( LOG.isDebugEnabled() ) {
 232  0
         LOG.debug("saved preferences user " + principalId);
 233  
     }
 234  0
     }
 235  
 
 236  
     private void validate(Preferences preferences) {
 237  0
         LOG.debug("validating preferences");
 238  
         
 239  0
         Collection errors = new ArrayList();
 240  
         try {
 241  0
             new Integer(preferences.getRefreshRate().trim());
 242  0
         } catch (NumberFormatException e) {
 243  0
             errors.add(new WorkflowServiceErrorImpl("ActionList Refresh Rate must be in whole " +
 244  
                     "minutes", ERR_KEY_REFRESH_RATE_WHOLE_NUM));
 245  0
         } catch (NullPointerException e1) {
 246  0
             errors.add(new WorkflowServiceErrorImpl("ActionList Refresh Rate must be in whole " +
 247  
                     "minutes", ERR_KEY_REFRESH_RATE_WHOLE_NUM));
 248  0
         }
 249  
 
 250  
         try {
 251  0
             if(new Integer(preferences.getPageSize().trim()) == 0){
 252  0
                     errors.add(new WorkflowServiceErrorImpl("ActionList Page Size must be non-zero ", ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM));
 253  
             }            
 254  0
         } catch (NumberFormatException e) {
 255  0
             errors.add(new WorkflowServiceErrorImpl("ActionList Page Size must be in whole " +
 256  
                     "minutes", ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM));
 257  0
         } catch (NullPointerException e1) {
 258  0
             errors.add(new WorkflowServiceErrorImpl("ActionList Page Size must be in whole " +
 259  
                     "minutes", ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM));
 260  0
         }
 261  
       
 262  0
         LOG.debug("end validating preferences");
 263  0
         if (! errors.isEmpty()) {
 264  0
             throw new WorkflowServiceErrorException("Preference Validation Error", errors);
 265  
         }
 266  0
     }
 267  
 
 268  
     public UserOptionsService getUserOptionService() {
 269  0
         return (UserOptionsService) KEWServiceLocator.getService(
 270  
                 KEWServiceLocator.USER_OPTIONS_SRV);
 271  
     }
 272  
 }