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.getPropertyValueAsString("staging.directory");
 97  
                                                                                                     
 98  0
         String defaultColor =  kcs.getPropertyValueAsString("userOptions.default.color");
 99  0
         String defaultEmail = kcs.getPropertyValueAsString("userOptions.default.email");
 100  0
         String defaultNotifyPrimary = kcs.getPropertyValueAsString("userOptions.default.notifyPrimary");
 101  0
         String defaultNotifySecondary = kcs.getPropertyValueAsString("userOptions.default.notifySecondary");
 102  0
         String defaultOpenNewWindow = kcs.getPropertyValueAsString("userOptions.default.openNewWindow");
 103  0
         String defaultActionListSize = kcs.getPropertyValueAsString("userOptions.default.actionListSize");
 104  0
         String defaultRefreshRate = kcs.getPropertyValueAsString("userOptions.default.refreshRate");
 105  0
         String defaultShowActionRequired = kcs.getPropertyValueAsString("userOptions.default.showActionRequired");
 106  0
         String defaultShowDateCreated = kcs.getPropertyValueAsString("userOptions.default.showDateCreated");
 107  0
         String defaultShowDocType = kcs.getPropertyValueAsString("userOptions.default.showDocumentType");
 108  0
         String defaultShowDocStatus = kcs.getPropertyValueAsString("userOptions.default.showDocumentStatus");
 109  0
         String defaultShowInitiator = kcs.getPropertyValueAsString("userOptions.default.showInitiator");
 110  0
         String defaultShowDelegator = kcs.getPropertyValueAsString("userOptions.default.showDelegator");
 111  0
         String defaultShowTitle = kcs.getPropertyValueAsString("userOptions.default.showTitle");
 112  0
         String defaultShowWorkgroupRequest = kcs.getPropertyValueAsString("userOptions.default.showWorkgroupRequest");
 113  0
         String defaultShowLastApprovedDate = kcs.getPropertyValueAsString("userOptions.default.showLastApprovedDate");
 114  0
         String defaultShowClearFYI = kcs.getPropertyValueAsString("userOptions.default.showClearFYI");
 115  0
         String defaultShowCurrentNode = kcs.getPropertyValueAsString("userOptions.default.showCurrentNode");
 116  0
         String defaultDelegatorFilterOnActionList = kcs.getPropertyValueAsString(
 117  
                 "userOptions.default.delegatorFilterOnActionList");
 118  0
         String defaultPrimaryDelegatorFilterOnActionList = kcs.getPropertyValueAsString(
 119  
                 "userOptions.default.primaryDelegatorFilterOnActionList");
 120  
 
 121  0
         final String defaultUseOutBox = kcs.getPropertyValueAsString(KEWConstants.USER_OPTIONS_DEFAULT_USE_OUTBOX_PARAM);
 122  
                                                                                                                                                                                     
 123  0
         Preferences preferences = new Preferences();                                                                                                                                
 124  0
         preferences.setColorApproved(getOption(optionMap,APPROVED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                               
 125  0
         preferences.setColorCanceled(getOption(optionMap,CANCELLED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                              
 126  0
         preferences.setColorDissapproveCancel(getOption(optionMap,DISSAPPROVED_CANCELLED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                        
 127  0
         preferences.setColorDissaproved(getOption(optionMap,DISAPPROVED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                         
 128  0
         preferences.setColorEnroute(getOption(optionMap,ENROUTE_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                                 
 129  0
         preferences.setColorException(getOption(optionMap,EXCEPTION_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                             
 130  0
         preferences.setColorFinal(getOption(optionMap,FINAL_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                                     
 131  0
         preferences.setColorInitiated(getOption(optionMap,INITIATED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                             
 132  0
         preferences.setColorProccessed(getOption(optionMap,PROCESSED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                            
 133  0
         preferences.setColorSaved(getOption(optionMap,SAVED_DOC_COLOR, defaultColor, principalId, preferences).getOptionVal());                                                     
 134  0
         preferences.setEmailNotification(getOption(optionMap,EMAIL_REMINDER_KEY, defaultEmail, principalId, preferences).getOptionVal());                                           
 135  0
         preferences.setNotifyPrimaryDelegation(getOption(optionMap,EMAIL_NOTIFY_PRIMARY_KEY, defaultNotifyPrimary, principalId, preferences).getOptionVal());                       
 136  0
         preferences.setNotifySecondaryDelegation(getOption(optionMap,EMAIL_NOTIFY_SECONDARY_KEY, defaultNotifySecondary, principalId, preferences).getOptionVal());                 
 137  0
         preferences.setOpenNewWindow(getOption(optionMap,OPEN_NEW_WINDOW_KEY, defaultOpenNewWindow, principalId, preferences).getOptionVal());                                      
 138  0
         preferences.setPageSize(getOption(optionMap,ACTION_LIST_SIZE_KEY, defaultActionListSize, principalId, preferences).getOptionVal());                                         
 139  0
         preferences.setRefreshRate(getOption(optionMap,REFRESH_RATE_KEY, defaultRefreshRate, principalId, preferences).getOptionVal());                                             
 140  0
         preferences.setShowActionRequested(getOption(optionMap,COLUMN_ACTION_REQ_KEY, defaultShowActionRequired, principalId, preferences).getOptionVal());                         
 141  0
         preferences.setShowDateCreated(getOption(optionMap,COLUMN_DATE_CREATE_KEY, defaultShowDateCreated, principalId, preferences).getOptionVal());                               
 142  0
         preferences.setShowDocType(getOption(optionMap,COLUMN_DOC_TYPE_KEY, defaultShowDocType, principalId, preferences).getOptionVal());                                          
 143  0
         preferences.setShowDocumentStatus(getOption(optionMap,COLUMN_DOCUMENT_STATUS_KEY, defaultShowDocStatus, principalId, preferences).getOptionVal());                          
 144  0
         preferences.setShowInitiator(getOption(optionMap,COLUMN_INITIATOR_KEY, defaultShowInitiator, principalId, preferences).getOptionVal());                                     
 145  0
         preferences.setShowDelegator(getOption(optionMap,COLUMN_DELEGATOR_KEY, defaultShowDelegator, principalId, preferences).getOptionVal());                                     
 146  0
         preferences.setShowDocTitle(getOption(optionMap,COLUMN_TITLE_KEY, defaultShowTitle, principalId, preferences).getOptionVal());                                              
 147  0
         preferences.setShowWorkgroupRequest(getOption(optionMap,COLUMN_WORKGROUP_REQUEST_KEY, defaultShowWorkgroupRequest, principalId, preferences).getOptionVal());               
 148  0
         preferences.setShowClearFyi(getOption(optionMap,COLUMN_CLEAR_FYI_KEY, defaultShowClearFYI, principalId, preferences).getOptionVal());                                       
 149  0
         preferences.setDelegatorFilter(getOption(optionMap,DELEGATOR_FILTER_KEY, defaultDelegatorFilterOnActionList, principalId, preferences).getOptionVal());                     
 150  0
         preferences.setPrimaryDelegateFilter(getOption(optionMap,PRIMARY_DELEGATE_FILTER_KEY, defaultPrimaryDelegatorFilterOnActionList, principalId, preferences).getOptionVal()); 
 151  0
         preferences.setShowDateApproved(getOption(optionMap,COLUMN_LAST_APPROVED_DATE_KEY, defaultShowLastApprovedDate, principalId, preferences).getOptionVal());                  
 152  0
         preferences.setShowCurrentNode(getOption(optionMap,COLUMN_CURRENT_NODE_KEY, defaultShowCurrentNode, principalId, preferences).getOptionVal());                              
 153  0
         preferences.setUseOutbox(getOption(optionMap,USE_OUT_BOX, defaultUseOutBox, principalId, preferences).getOptionVal());                                                      
 154  
 
 155  0
         if ( LOG.isDebugEnabled() ) {
 156  0
         LOG.debug("end preferences fetch user " + principalId);
 157  
         }
 158  0
         return preferences;
 159  
     }
 160  
 
 161  
     /* @see https://test.kuali.org/jira/browse/KULRICE-1726 */
 162  
     //private static ConcurrencyDetector detector = new ConcurrencyDetector("Concurrency in PreferencesServiceImpl", false);
 163  
 
 164  
     private UserOptions getOption(Map<String,UserOptions> optionsMap, String optionKey, String defaultValue, String principalId, Preferences preferences) {
 165  0
             if ( LOG.isDebugEnabled() ) {
 166  0
         LOG.debug("start fetch option " + optionKey + " user " + principalId);
 167  
             }
 168  0
         UserOptions option = optionsMap.get(optionKey);
 169  0
         if (option == null) {
 170  0
                 if ( LOG.isDebugEnabled() ) {
 171  0
             LOG.debug("User option '" + optionKey + "' on user " + principalId + " has no stored value.  Preferences will require save.");
 172  
                 }
 173  0
             option = new UserOptions();
 174  0
             option.setWorkflowId(principalId);
 175  0
             option.setOptionId(optionKey);
 176  0
             option.setOptionVal(defaultValue);
 177  0
             optionsMap.put(optionKey, option); // just in case referenced a second time
 178  0
             if ( optionKey.equals(USE_OUT_BOX) && !ConfigContext.getCurrentContextConfig().getOutBoxOn() ) {
 179  
                     // don't mark as needing save
 180  
             } else {
 181  0
             preferences.setRequiresSave(true);
 182  
         }
 183  
         }
 184  0
         if ( LOG.isDebugEnabled() ) {
 185  0
         LOG.debug("End fetch option " + optionKey + " user " + principalId);
 186  
         }
 187  0
         return option;
 188  
     }
 189  
 
 190  
     public void savePreferences(String principalId, Preferences preferences) {
 191  
             // NOTE: this previously displayed the principalName.  Now it's just the id
 192  0
             if ( LOG.isDebugEnabled() ) {
 193  0
         LOG.debug("saving preferences user " + principalId);
 194  
             }
 195  
 
 196  0
         validate(preferences);
 197  0
         Map<String,String> optionsMap = new HashMap<String,String>(50);
 198  
         
 199  0
         optionsMap.put(DISSAPPROVED_CANCELLED_DOC_COLOR, preferences.getColorDissapproveCancel());
 200  0
         optionsMap.put(DISAPPROVED_DOC_COLOR, preferences.getColorDissaproved());
 201  0
         optionsMap.put(APPROVED_DOC_COLOR, preferences.getColorApproved());
 202  0
         optionsMap.put(CANCELLED_DOC_COLOR, preferences.getColorCanceled());
 203  0
         optionsMap.put(SAVED_DOC_COLOR, preferences.getColorSaved());
 204  0
         optionsMap.put(ENROUTE_DOC_COLOR, preferences.getColorEnroute());
 205  0
         optionsMap.put(PROCESSED_DOC_COLOR, preferences.getColorProccessed());
 206  0
         optionsMap.put(INITIATED_DOC_COLOR, preferences.getColorInitiated());
 207  0
         optionsMap.put(FINAL_DOC_COLOR, preferences.getColorFinal());
 208  0
         optionsMap.put(EXCEPTION_DOC_COLOR, preferences.getColorException());
 209  0
         optionsMap.put(REFRESH_RATE_KEY, preferences.getRefreshRate().trim());
 210  0
         optionsMap.put(OPEN_NEW_WINDOW_KEY, preferences.getOpenNewWindow());
 211  0
         optionsMap.put(COLUMN_DOC_TYPE_KEY, preferences.getShowDocType());
 212  0
         optionsMap.put(COLUMN_TITLE_KEY, preferences.getShowDocTitle());
 213  0
         optionsMap.put(COLUMN_ACTION_REQ_KEY, preferences.getShowActionRequested());
 214  0
         optionsMap.put(COLUMN_INITIATOR_KEY, preferences.getShowInitiator());
 215  0
         optionsMap.put(COLUMN_DELEGATOR_KEY, preferences.getShowDelegator());
 216  0
         optionsMap.put(COLUMN_DATE_CREATE_KEY, preferences.getShowDateCreated());
 217  0
         optionsMap.put(COLUMN_DOCUMENT_STATUS_KEY, preferences.getShowDocumentStatus());
 218  0
         optionsMap.put(COLUMN_APP_DOC_STATUS_KEY, preferences.getShowAppDocStatus());
 219  0
         optionsMap.put(COLUMN_WORKGROUP_REQUEST_KEY, preferences.getShowWorkgroupRequest());
 220  0
         optionsMap.put(COLUMN_CLEAR_FYI_KEY, preferences.getShowClearFyi());
 221  0
         optionsMap.put(ACTION_LIST_SIZE_KEY, preferences.getPageSize().trim());
 222  0
         optionsMap.put(EMAIL_REMINDER_KEY, preferences.getEmailNotification());
 223  0
         optionsMap.put(EMAIL_NOTIFY_PRIMARY_KEY, preferences.getNotifyPrimaryDelegation());
 224  0
         optionsMap.put(EMAIL_NOTIFY_SECONDARY_KEY, preferences.getNotifySecondaryDelegation());
 225  0
         optionsMap.put(DELEGATOR_FILTER_KEY, preferences.getDelegatorFilter());
 226  0
         optionsMap.put(PRIMARY_DELEGATE_FILTER_KEY, preferences.getPrimaryDelegateFilter());
 227  0
         optionsMap.put(COLUMN_LAST_APPROVED_DATE_KEY, preferences.getShowDateApproved());
 228  0
         optionsMap.put(COLUMN_CURRENT_NODE_KEY, preferences.getShowCurrentNode());
 229  0
         if (ConfigContext.getCurrentContextConfig().getOutBoxOn()) {
 230  0
             optionsMap.put(USE_OUT_BOX, preferences.getUseOutbox());
 231  
         }
 232  0
         getUserOptionService().save(principalId, optionsMap);
 233  0
         if ( LOG.isDebugEnabled() ) {
 234  0
         LOG.debug("saved preferences user " + principalId);
 235  
     }
 236  0
     }
 237  
 
 238  
     private void validate(Preferences preferences) {
 239  0
         LOG.debug("validating preferences");
 240  
         
 241  0
         Collection errors = new ArrayList();
 242  
         try {
 243  0
             new Integer(preferences.getRefreshRate().trim());
 244  0
         } catch (NumberFormatException e) {
 245  0
             errors.add(new WorkflowServiceErrorImpl("ActionList Refresh Rate must be in whole " +
 246  
                     "minutes", ERR_KEY_REFRESH_RATE_WHOLE_NUM));
 247  0
         } catch (NullPointerException e1) {
 248  0
             errors.add(new WorkflowServiceErrorImpl("ActionList Refresh Rate must be in whole " +
 249  
                     "minutes", ERR_KEY_REFRESH_RATE_WHOLE_NUM));
 250  0
         }
 251  
 
 252  
         try {
 253  0
             if(new Integer(preferences.getPageSize().trim()) == 0){
 254  0
                     errors.add(new WorkflowServiceErrorImpl("ActionList Page Size must be non-zero ", ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM));
 255  
             }            
 256  0
         } catch (NumberFormatException e) {
 257  0
             errors.add(new WorkflowServiceErrorImpl("ActionList Page Size must be in whole " +
 258  
                     "minutes", ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM));
 259  0
         } catch (NullPointerException e1) {
 260  0
             errors.add(new WorkflowServiceErrorImpl("ActionList Page Size must be in whole " +
 261  
                     "minutes", ERR_KEY_ACTION_LIST_PAGE_SIZE_WHOLE_NUM));
 262  0
         }
 263  
       
 264  0
         LOG.debug("end validating preferences");
 265  0
         if (! errors.isEmpty()) {
 266  0
             throw new WorkflowServiceErrorException("Preference Validation Error", errors);
 267  
         }
 268  0
     }
 269  
 
 270  
     public UserOptionsService getUserOptionService() {
 271  0
         return (UserOptionsService) KEWServiceLocator.getService(
 272  
                 KEWServiceLocator.USER_OPTIONS_SRV);
 273  
     }
 274  
 }