View Javadoc
1   /**
2    * Copyright 2005-2014 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.kew.useroptions;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Properties;
23  import java.util.Map.Entry;
24  
25  import org.kuali.rice.core.api.criteria.QueryByCriteria;
26  import org.kuali.rice.kew.api.KewApiConstants;
27  import org.kuali.rice.krad.data.DataObjectService;
28  
29  import org.springframework.transaction.annotation.Transactional;
30  
31  import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
32  
33  /**
34   * /**
35   * An implementation of the {@link UserOptionsService}.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  @Transactional
40  public class UserOptionsServiceImpl implements UserOptionsService {
41  
42      // KRAD Data Layer API containing basic CRUD operations and access to a metadata repository.
43      private DataObjectService dataObjectService;
44  
45      // default properties for this class
46      private static final Properties defaultProperties = new Properties();
47  
48      // set the default properties for this class
49      static {
50          defaultProperties.setProperty(KewApiConstants.EMAIL_RMNDR_KEY, KewApiConstants.EMAIL_RMNDR_WEEK_VAL);
51      }
52  
53      /** {@inheritDoc}
54       */
55      @Override
56      public Collection<UserOptions> findByWorkflowUser(String principalId) {
57  
58          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
59          criteria.setPredicates(equal("workflowId", principalId));
60  
61          return dataObjectService.findMatching(UserOptions.class, criteria.build()).getResults();
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public List<UserOptions> findByUserQualified(String principalId, String likeString) {
69          if ((principalId == null)) {
70              return new ArrayList<UserOptions>(0);
71          }
72          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
73          criteria.setPredicates(and(equal("workflowId", principalId),like("optionId", likeString)));
74          return this.dataObjectService.findMatching(UserOptions.class, criteria.build()).getResults();
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public UserOptions findByOptionId(String optionId, String principalId) {
82          if (optionId == null || "".equals(optionId) || principalId == null || "".equals(principalId)) {
83              return null;
84          }
85  
86          return this.dataObjectService.find(UserOptions.class, new UserOptionsId(principalId, optionId));
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public void save(UserOptions userOptions) {
94          this.dataObjectService.save(userOptions);
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public void save(String principalId, Map<String,String> optionsMap) {
102 
103     	// build UserOptions from the principalId and optionMap and save them
104         if (optionsMap != null && !optionsMap.isEmpty()) {
105     		for (Entry<String, String> entry : optionsMap.entrySet()) {
106     			UserOptions option = findByOptionId(entry.getKey(), principalId);
107     			if (option == null) {
108     				option = new UserOptions();
109     				option.setWorkflowId(principalId);
110     			}
111     			option.setOptionId(entry.getKey());
112     			option.setOptionVal(entry.getValue());
113                 this.save(option);
114     		}
115     	}
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void save(String principalId, String optionId, String optionValue) {
123         //KULRICE-7796 Don't save where val is greater than field length
124         if(optionValue.length() <= 2000)
125         {
126             UserOptions option = findByOptionId(optionId, principalId);
127             if (option == null) {
128                 option = new UserOptions();
129                 option.setWorkflowId(principalId);
130             }
131             option.setOptionId(optionId);
132             option.setOptionVal(optionValue);
133             this.save(option);
134         }
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public void deleteUserOptions(UserOptions userOptions) {
142         this.dataObjectService.delete(userOptions);
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public List<UserOptions> retrieveEmailPreferenceUserOptions(String emailSetting) {
150 
151         QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
152         criteria.setPredicates(
153                 or(
154                     equal("optionId", KewApiConstants.EMAIL_RMNDR_KEY),
155                     like("optionId", "%" + KewApiConstants.DOCUMENT_TYPE_NOTIFICATION_PREFERENCE_SUFFIX)
156                 ),
157                 equal("optionVal", emailSetting)
158         );
159 
160         return this.dataObjectService.findMatching(UserOptions.class, criteria.build()).getResults();
161     }
162 
163     /**
164      * Returns an instance of the {@link DataObjectService}.
165      * @return  a instance of {@link DataObjectService}
166      */
167     public DataObjectService getDataObjectService() {
168         return dataObjectService;
169     }
170 
171     /**
172      * @see org.kuali.rice.kew.useroptions.UserOptionsServiceImpl#getDataObjectService()
173      */
174     public void setDataObjectService(DataObjectService dataObjectService) {
175         this.dataObjectService = dataObjectService;
176     }
177 }