001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.useroptions;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.List;
021import java.util.Map;
022import java.util.Properties;
023import java.util.Map.Entry;
024
025import org.kuali.rice.core.api.criteria.QueryByCriteria;
026import org.kuali.rice.kew.api.KewApiConstants;
027import org.kuali.rice.krad.data.DataObjectService;
028
029import org.springframework.transaction.annotation.Transactional;
030
031import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
032
033/**
034 * /**
035 * An implementation of the {@link UserOptionsService}.
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039@Transactional
040public class UserOptionsServiceImpl implements UserOptionsService {
041
042    // KRAD Data Layer API containing basic CRUD operations and access to a metadata repository.
043    private DataObjectService dataObjectService;
044
045    // default properties for this class
046    private static final Properties defaultProperties = new Properties();
047
048    // set the default properties for this class
049    static {
050        defaultProperties.setProperty(KewApiConstants.EMAIL_RMNDR_KEY, KewApiConstants.EMAIL_RMNDR_WEEK_VAL);
051    }
052
053    /** {@inheritDoc}
054     */
055    @Override
056    public Collection<UserOptions> findByWorkflowUser(String principalId) {
057
058        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
059        criteria.setPredicates(equal("workflowId", principalId));
060
061        return dataObjectService.findMatching(UserOptions.class, criteria.build()).getResults();
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public List<UserOptions> findByUserQualified(String principalId, String likeString) {
069        if ((principalId == null)) {
070            return new ArrayList<UserOptions>(0);
071        }
072        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
073        criteria.setPredicates(and(equal("workflowId", principalId),like("optionId", likeString)));
074        return this.dataObjectService.findMatching(UserOptions.class, criteria.build()).getResults();
075    }
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public UserOptions findByOptionId(String optionId, String principalId) {
082        if (optionId == null || "".equals(optionId) || principalId == null || "".equals(principalId)) {
083            return null;
084        }
085
086        return this.dataObjectService.find(UserOptions.class, new UserOptionsId(principalId, optionId));
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    public void save(UserOptions userOptions) {
094        this.dataObjectService.save(userOptions);
095    }
096
097    /**
098     * {@inheritDoc}
099     */
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}