001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.sec.businessobject;
017    
018    import java.sql.Date;
019    import java.sql.Timestamp;
020    import java.util.LinkedHashMap;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.sec.SecPropertyConstants;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.rice.kim.bo.Person;
026    import org.kuali.rice.kim.bo.group.dto.GroupInfo;
027    import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
028    import org.kuali.rice.kim.service.GroupService;
029    import org.kuali.rice.kim.service.PersonService;
030    import org.kuali.rice.kim.service.RoleManagementService;
031    import org.kuali.rice.kim.util.KimConstants;
032    import org.kuali.rice.kns.bo.PersistableBusinessObjectBase;
033    import org.kuali.rice.kns.util.KualiInteger;
034    
035    
036    /**
037     * Associates a member (principal, role, or group) to a model. These become the members of the model role created in KIM
038     */
039    public class SecurityModelMember extends PersistableBusinessObjectBase {
040        private KualiInteger modelId;
041        private String memberId;
042        private String memberTypeCode;
043        private Timestamp activeFromDate;
044        private Timestamp activeToDate;
045    
046        private SecurityModel securityModel;
047    
048        // non db
049        private String memberName;
050    
051        private ModelMember modelMember;
052    
053        public SecurityModelMember() {
054            super();
055        }
056    
057    
058        /**
059         * Gets the modelId attribute.
060         * 
061         * @return Returns the modelId.
062         */
063        public KualiInteger getModelId() {
064            return modelId;
065        }
066    
067    
068        /**
069         * Sets the modelId attribute value.
070         * 
071         * @param modelId The modelId to set.
072         */
073        public void setModelId(KualiInteger modelId) {
074            this.modelId = modelId;
075        }
076    
077    
078        /**
079         * Gets the memberId attribute.
080         * 
081         * @return Returns the memberId.
082         */
083        public String getMemberId() {
084            return memberId;
085        }
086    
087    
088        /**
089         * Sets the memberId attribute value.
090         * 
091         * @param memberId The memberId to set.
092         */
093        public void setMemberId(String memberId) {
094            this.memberId = memberId;
095        }
096    
097    
098        /**
099         * Gets the memberTypeCode attribute.
100         * 
101         * @return Returns the memberTypeCode.
102         */
103        public String getMemberTypeCode() {
104            return memberTypeCode;
105        }
106    
107    
108        /**
109         * Sets the memberTypeCode attribute value.
110         * 
111         * @param memberTypeCode The memberTypeCode to set.
112         */
113        public void setMemberTypeCode(String memberTypeCode) {
114            this.memberTypeCode = memberTypeCode;
115        }
116    
117    
118        /**
119         * Gets the activeFromDate attribute.
120         * 
121         * @return Returns the activeFromDate.
122         */
123        public Timestamp getActiveFromDate() {
124            return activeFromDate;
125        }
126    
127    
128        /**
129         * Sets the activeFromDate attribute value.
130         * 
131         * @param activeFromDate The activeFromDate to set.
132         */
133        public void setActiveFromDate(Timestamp activeFromDate) {
134            this.activeFromDate = activeFromDate;
135        }
136    
137    
138        /**
139         * Gets the activeToDate attribute.
140         * 
141         * @return Returns the activeToDate.
142         */
143        public Timestamp getActiveToDate() {
144            return activeToDate;
145        }
146    
147    
148        /**
149         * Sets the activeToDate attribute value.
150         * 
151         * @param activeToDate The activeToDate to set.
152         */
153        public void setActiveToDate(Timestamp activeToDate) {
154            this.activeToDate = activeToDate;
155        }
156    
157    
158        /**
159         * Gets the memberName attribute.
160         * 
161         * @return Returns the memberName.
162         */
163        public String getMemberName() {
164            if (StringUtils.isNotBlank(memberName)) {
165                return memberName;
166            }
167    
168            if (StringUtils.isNotBlank(memberTypeCode) && StringUtils.isNotBlank(memberId)) {
169                if (KimConstants.KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(memberTypeCode)) {
170                    Person person = SpringContext.getBean(PersonService.class).getPerson(memberId);
171                    if (person != null) {
172                        return person.getName();
173                    }
174                }
175                else if (KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE.equals(memberTypeCode)) {
176                    KimRoleInfo roleInfo = SpringContext.getBean(RoleManagementService.class).getRole(memberId);
177                    if (roleInfo != null) {
178                        return roleInfo.getRoleName();
179                    }
180                }
181                else if (KimConstants.KimUIConstants.MEMBER_TYPE_GROUP_CODE.equals(memberTypeCode)) {
182                    GroupInfo groupInfo = SpringContext.getBean(GroupService.class).getGroupInfo(memberId);
183                    if (groupInfo != null) {
184                        return groupInfo.getGroupName();
185                    }
186                }
187            }
188    
189            return "";
190        }
191    
192    
193        /**
194         * Sets the memberName attribute value.
195         * 
196         * @param memberName The memberName to set.
197         */
198        public void setMemberName(String memberName) {
199            this.memberName = memberName;
200        }
201    
202    
203        /**
204         * Gets the securityModel attribute.
205         * 
206         * @return Returns the securityModel.
207         */
208        public SecurityModel getSecurityModel() {
209            return securityModel;
210        }
211    
212    
213        /**
214         * Sets the securityModel attribute value.
215         * 
216         * @param securityModel The securityModel to set.
217         */
218        public void setSecurityModel(SecurityModel securityModel) {
219            this.securityModel = securityModel;
220        }
221    
222    
223        /**
224         * Gets the modelMember attribute.
225         * 
226         * @return Returns the modelMember.
227         */
228        public ModelMember getModelMember() {
229            return modelMember;
230        }
231    
232    
233        /**
234         * Sets the modelMember attribute value.
235         * 
236         * @param modelMember The modelMember to set.
237         */
238        public void setModelMember(ModelMember modelMember) {
239            this.modelMember = modelMember;
240        }
241        
242        /**
243         * Builds a string representation of the model definition assignmentss
244         * 
245         * @return String
246         */
247        public String getModelDefinitionSummary() {
248            String summary = "";
249    
250            for (SecurityModelDefinition modelDefinition : securityModel.getModelDefinitions()) {
251                summary += "Definition Name: " + modelDefinition.getSecurityDefinition().getName();
252                summary += ", Constraint Code: " + modelDefinition.getConstraintCode();
253                summary += ", Operator Code: " + modelDefinition.getOperatorCode();
254                summary += ", Value: " + modelDefinition.getAttributeValue();
255                summary += "; ";
256            }
257    
258            return summary;
259        }
260    
261    
262        /**
263         * @see org.kuali.rice.kns.bo.BusinessObjectBase#toStringMapper()
264         */
265        @Override
266        protected LinkedHashMap toStringMapper() {
267            LinkedHashMap m = new LinkedHashMap();
268    
269            m.put(SecPropertyConstants.MODEL_ID, this.modelId);
270            m.put(SecPropertyConstants.MEMBER_ID, this.memberId);
271    
272            return m;
273        }
274    
275    }