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