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.kim.impl.common.attribute; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import javax.persistence.Column; 025import javax.persistence.FetchType; 026import javax.persistence.JoinColumn; 027import javax.persistence.MappedSuperclass; 028import javax.persistence.OneToOne; 029import javax.persistence.Transient; 030 031import org.apache.commons.collections.CollectionUtils; 032import org.apache.commons.lang.StringUtils; 033import org.eclipse.persistence.annotations.JoinFetch; 034import org.eclipse.persistence.annotations.JoinFetchType; 035import org.kuali.rice.kim.api.common.attribute.KimAttributeDataContract; 036import org.kuali.rice.kim.api.services.KimApiServiceLocator; 037import org.kuali.rice.kim.api.type.KimType; 038import org.kuali.rice.kim.api.type.KimTypeAttribute; 039import org.kuali.rice.kim.api.type.KimTypeInfoService; 040import org.kuali.rice.kim.impl.type.KimTypeBo; 041import org.kuali.rice.krad.bo.DataObjectBase; 042import org.kuali.rice.krad.data.KradDataServiceLocator; 043import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator; 044 045@MappedSuperclass 046@PortableSequenceGenerator(name = "KRIM_ATTR_DATA_ID_S") 047public abstract class KimAttributeDataBo extends DataObjectBase implements KimAttributeDataContract { 048 049 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KimAttributeDataBo.class); 050 private static final long serialVersionUID = 1L; 051 052 private static KimTypeInfoService kimTypeInfoService; 053 054 @Column(name="ATTR_VAL") 055 private String attributeValue; 056 057 @Column(name="KIM_ATTR_DEFN_ID") 058 private String kimAttributeId; 059 060 @JoinFetch(value= JoinFetchType.OUTER) 061 @OneToOne(fetch=FetchType.EAGER) 062 @JoinColumn(name = "KIM_ATTR_DEFN_ID", insertable = false, updatable = false) 063 private KimAttributeBo kimAttribute; 064 065 @Column(name="KIM_TYP_ID") 066 private String kimTypeId; 067 068 @Transient 069 private KimTypeBo kimType; 070 071 public abstract void setId(String id); 072 073 public abstract void setAssignedToId(String assignedToId); 074 075 @Override 076 public KimAttributeBo getKimAttribute() { 077 if(this.kimAttribute == null 078 && StringUtils.isNotBlank(kimAttributeId)) { 079 KradDataServiceLocator.getDataObjectService().wrap(this).fetchRelationship("kimAttribute"); 080 } 081 return kimAttribute; 082 } 083 084 @Override 085 public KimTypeBo getKimType() { 086 if (kimType == null && StringUtils.isNotEmpty(getId())) { 087 kimType = KimTypeBo.from(KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId)); 088 } 089 return kimType; 090 } 091 092 public static <T extends KimAttributeDataBo> Map<String, String> toAttributes(Collection<T> bos) { 093 Map<String, String> m = new HashMap<String, String>(); 094 if(CollectionUtils.isNotEmpty(bos)) { 095 for (T it : bos) { 096 if (it != null) { 097 KimTypeAttribute attribute = null; 098 if ( it.getKimType() != null ) { 099 attribute = KimTypeBo.to(it.getKimType()).getAttributeDefinitionById(it.getKimAttributeId()); 100 } 101 if ( attribute != null ) { 102 m.put(attribute.getKimAttribute().getAttributeName(), it.getAttributeValue()); 103 } else { 104 m.put(it.getKimAttribute().getAttributeName(), it.getAttributeValue()); 105 } 106 } 107 } 108 } 109 return m; 110 } 111 112 /** creates a list of KimAttributeDataBos from attributes, kimTypeId, and assignedToId. */ 113 public static <T extends KimAttributeDataBo> List<T> createFrom(Class<T> type, Map<String, String> attributes, String kimTypeId) { 114 if (attributes == null) { 115 //purposely not using Collections.emptyList() b/c we do not want to return an unmodifiable list. 116 return new ArrayList<T>(); 117 } 118 List<T> attrs = new ArrayList<T>(); 119 for (Map.Entry<String, String> it : attributes.entrySet()) { 120 //return attributes.entrySet().collect { 121 KimTypeAttribute attr = getKimTypeInfoService().getKimType(kimTypeId).getAttributeDefinitionByName(it.getKey()); 122 if (attr == null) { 123 LOG.error("Attribute " + it.getKey() + " was not found for kimType " + getKimTypeInfoService().getKimType(kimTypeId).getName()); 124 } 125 KimType theType = getKimTypeInfoService().getKimType(kimTypeId); 126 if (attr != null && StringUtils.isNotBlank(it.getValue())) { 127 try { 128 T newDetail = type.newInstance(); 129 newDetail.setKimAttributeId(attr.getKimAttribute().getId()); 130 newDetail.setKimAttribute(KimAttributeBo.from(attr.getKimAttribute())); 131 newDetail.setKimTypeId(kimTypeId); 132 newDetail.setKimType(KimTypeBo.from(theType)); 133 newDetail.setAttributeValue(it.getValue()); 134 attrs.add(newDetail); 135 } catch (InstantiationException e) { 136 LOG.error(e.getMessage(), e); 137 } catch (IllegalAccessException e) { 138 LOG.error(e.getMessage(), e); 139 } 140 141 } 142 } 143 return attrs; 144 } 145 146 @Override 147 public String getAttributeValue() { 148 return attributeValue; 149 } 150 151 public void setAttributeValue(String attributeValue) { 152 this.attributeValue = attributeValue; 153 } 154 155 public String getKimAttributeId() { 156 return kimAttributeId; 157 } 158 159 public void setKimAttributeId(String kimAttributeId) { 160 this.kimAttributeId = kimAttributeId; 161 } 162 163 @Override 164 public String getKimTypeId() { 165 return kimTypeId; 166 } 167 168 public void setKimTypeId(String kimTypeId) { 169 this.kimTypeId = kimTypeId; 170 } 171 172 public void setKimType(KimTypeBo kimType) { 173 this.kimType = kimType; 174 } 175 176 public void setKimAttribute(KimAttributeBo kimAttribute) { 177 this.kimAttribute = kimAttribute; 178 } 179 180 181 public static KimTypeInfoService getKimTypeInfoService() { 182 if (kimTypeInfoService==null) { 183 kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService(); 184 } 185 return kimTypeInfoService; 186 } 187 188 public static void setKimTypeInfoService(KimTypeInfoService kimTypeInfoService) { 189 KimAttributeDataBo.kimTypeInfoService = kimTypeInfoService; 190 } 191 192}