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.actionitem;
017
018import org.kuali.rice.kew.api.KewApiConstants;
019import org.kuali.rice.kew.api.actionlist.DisplayParameters;
020import org.kuali.rice.kew.api.document.DocumentStatus;
021import org.kuali.rice.kew.api.preferences.Preferences;
022import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueActionListExtension;
023import org.kuali.rice.kew.service.KEWServiceLocator;
024import org.kuali.rice.kew.web.RowStyleable;
025import org.kuali.rice.kim.api.group.Group;
026import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
027import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028
029import javax.persistence.MappedSuperclass;
030import javax.persistence.Transient;
031import java.sql.Timestamp;
032import java.util.HashMap;
033import java.util.Map;
034
035/**
036 * Alternate model object for action list fetches that do not automatically use
037 * ojb collections.  This is here to make action list faster.
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 *
041 */
042@MappedSuperclass
043public class ActionItemActionListExtension extends ActionItem implements RowStyleable {
044
045    private static final long serialVersionUID = -8801104028828059623L;
046
047    @Transient
048    private Timestamp lastApprovedDate;
049    @Transient
050    private Map<String, String> customActions = new HashMap<String, String>();
051    @Transient
052    private String rowStyleClass;
053    @Transient
054    private Integer actionListIndex;
055
056    @Transient
057    private String delegatorName = "";
058    @Transient
059    private String groupName = "";
060    @Transient
061    private DisplayParameters displayParameters;
062    @Transient
063    private boolean isInitialized = false;
064    @Transient
065    private DocumentRouteHeaderValueActionListExtension routeHeader;
066
067    @Transient
068    private boolean lastApprovedDateInitialized = false;
069    @Transient
070    private boolean delegatorNameInitialized = false;
071    @Transient
072    private boolean groupNameInitialized = false;
073
074    public Integer getActionListIndex() {
075        return actionListIndex;
076    }
077
078    public void setActionListIndex(Integer actionListIndex) {
079        this.actionListIndex = actionListIndex;
080    }
081
082    public Timestamp getLastApprovedDate() {
083        initializeLastApprovedDate();
084        return this.lastApprovedDate;
085    }
086
087    public Map<String, String> getCustomActions() {
088        return customActions;
089    }
090
091    public void setCustomActions(Map<String, String> customActions) {
092        this.customActions = customActions;
093    }
094
095    public String getRowStyleClass() {
096        return rowStyleClass;
097    }
098
099    public void setRowStyleClass(String rowStyleClass) {
100        this.rowStyleClass = rowStyleClass;
101    }
102
103    public String getDelegatorName() {
104        initializeDelegatorName();
105        return delegatorName;
106    }
107
108    public String getGroupName() {
109        initializeGroupName();
110        return groupName;
111    }
112
113    public void initialize(Preferences preferences) {
114        // always re-initialize row style class, just in case they changed a preference!
115        initializeRowStyleClass(preferences);
116        if (isInitialized) {
117                return;
118        }
119        if (KewApiConstants.PREFERENCES_YES_VAL.equals(preferences.getShowWorkgroupRequest())) {
120            initializeGroupName();
121        }
122        if (KewApiConstants.PREFERENCES_YES_VAL.equals(preferences.getShowDelegator())) {
123            initializeDelegatorName();
124        }
125        if (KewApiConstants.PREFERENCES_YES_VAL.equals(preferences.getShowDateApproved())) {
126                initializeLastApprovedDate();
127        }
128        this.routeHeader.initialize(preferences);
129        isInitialized = true;
130    }
131
132    private void initializeRowStyleClass(Preferences preferences) {
133        //set background colors for document statuses
134        if (KewApiConstants.ROUTE_HEADER_CANCEL_CD.equalsIgnoreCase(routeHeader.getDocRouteStatus())) {
135            setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorCanceled()));
136        } else if (KewApiConstants.ROUTE_HEADER_DISAPPROVED_CD.equalsIgnoreCase(routeHeader.getDocRouteStatus())) {
137            setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorDisapproved()));
138        } else if (KewApiConstants.ROUTE_HEADER_ENROUTE_CD.equalsIgnoreCase(routeHeader.getDocRouteStatus())) {
139            setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorEnroute()));
140        } else if (KewApiConstants.ROUTE_HEADER_EXCEPTION_CD.equalsIgnoreCase(routeHeader.getDocRouteStatus())) {
141            setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorException()));
142        } else if (KewApiConstants.ROUTE_HEADER_FINAL_CD.equalsIgnoreCase(routeHeader.getDocRouteStatus())) {
143            setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorFinal()));
144        } else if (KewApiConstants.ROUTE_HEADER_INITIATED_CD.equalsIgnoreCase(routeHeader.getDocRouteStatus())) {
145            setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorInitiated()));
146        } else if (KewApiConstants.ROUTE_HEADER_PROCESSED_CD.equalsIgnoreCase(routeHeader.getDocRouteStatus())) {
147            setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorProcessed()));
148        } else if (KewApiConstants.ROUTE_HEADER_SAVED_CD.equalsIgnoreCase(routeHeader.getDocRouteStatus())) {
149            setRowStyleClass(KewApiConstants.ACTION_LIST_COLOR_PALETTE.get(preferences.getColorSaved()));
150        }
151    }
152
153    private void initializeGroupName() {
154        if (!groupNameInitialized) {
155            if (getGroupId() != null) {
156                Group group = super.getGroup();
157                this.groupName = group.getName();
158            }
159            groupNameInitialized = true;
160        }
161    }
162
163    private void initializeDelegatorName() {
164        if (!delegatorNameInitialized) {
165            if (getDelegatorPrincipalId() != null) {
166                EntityNamePrincipalName name = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(getDelegatorPrincipalId());
167                if (name != null) {
168                    this.delegatorName = name.getDefaultName().getCompositeName();
169                }
170            }
171            if (getDelegatorGroupId() != null) {
172                Group delegatorGroup = KimApiServiceLocator.getGroupService().getGroup(getDelegatorGroupId());
173                if (delegatorGroup !=null)
174                    delegatorName = delegatorGroup.getName();
175            }
176            delegatorNameInitialized = true;
177        }
178    }
179
180    private void initializeLastApprovedDate() {
181        if (!lastApprovedDateInitialized) {
182            this.lastApprovedDate = KEWServiceLocator.getActionTakenService().getLastApprovedDate(getDocumentId());
183            lastApprovedDateInitialized = true;
184        }
185    }
186
187        public DisplayParameters getDisplayParameters() {
188                return displayParameters;
189        }
190
191        public void setDisplayParameters(DisplayParameters displayParameters) {
192                this.displayParameters = displayParameters;
193        }
194
195        public DocumentRouteHeaderValueActionListExtension getRouteHeader() {
196                return this.routeHeader;
197        }
198
199        public void setRouteHeader(DocumentRouteHeaderValueActionListExtension routeHeader) {
200                this.routeHeader = routeHeader;
201        }
202
203}
204