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.coa.businessobject;
017    
018    import java.lang.reflect.InvocationTargetException;
019    import java.util.LinkedHashMap;
020    import java.util.SortedSet;
021    import java.util.TreeSet;
022    
023    import org.apache.commons.beanutils.PropertyUtils;
024    import org.apache.commons.lang.StringUtils;
025    import org.kuali.kfs.coa.service.ObjectCodeService;
026    import org.kuali.kfs.sys.KFSConstants;
027    import org.kuali.kfs.sys.KFSPropertyConstants;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.rice.kns.bo.GlobalBusinessObjectDetailBase;
030    
031    /**
032     * This is a representation of an Organization Reversion Detail, made specifically for Global Organization Reversions. However, as
033     * OrganizationReversionDetail lists Organization as a primary key and Global Organization Reversions deal with several
034     * Organizations, that class could not be re-used for Globals.
035     */
036    public class OrganizationReversionGlobalDetail extends GlobalBusinessObjectDetailBase {
037        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionGlobalDetail.class);
038    
039        private String documentNumber;
040        private String organizationReversionCategoryCode;
041        private String organizationReversionObjectCode;
042        private String organizationReversionCode;
043    
044        private OrganizationReversionCategory organizationReversionCategory;
045        private OrganizationReversionGlobal parentGlobalOrganizationReversion;
046        private ObjectCode organizationReversionObject;
047    
048        /**
049         * @see org.kuali.rice.kns.bo.BusinessObjectBase#toStringMapper()
050         */
051        @Override
052        protected LinkedHashMap toStringMapper() {
053            LinkedHashMap stringMapper = new LinkedHashMap();
054            stringMapper.put(KFSPropertyConstants.DOCUMENT_NUMBER, this.documentNumber);
055            stringMapper.put("OrganizationReversionCategoryCode", this.organizationReversionCategoryCode);
056            return stringMapper;
057        }
058    
059        /**
060         * Constructs an OrganizationReversionGlobalDocumentDetail.
061         */
062        public OrganizationReversionGlobalDetail() {
063        }
064    
065        /**
066         * Gets the documentNumber attribute.
067         * 
068         * @return Returns the documentNumber.
069         */
070        public String getDocumentNumber() {
071            return documentNumber;
072        }
073    
074        /**
075         * Sets the documentNumber attribute value.
076         * 
077         * @param documentNumber The documentNumber to set.
078         */
079        public void setDocumentNumber(String documentNumber) {
080            this.documentNumber = documentNumber;
081        }
082    
083        /**
084         * Gets the organizationReversionCode attribute.
085         * 
086         * @return Returns the organizationReversionCode.
087         */
088        public String getOrganizationReversionCode() {
089            return organizationReversionCode;
090        }
091    
092        /**
093         * Sets the organizationReversionCode attribute value.
094         * 
095         * @param organizationReversionCode The organizationReversionCode to set.
096         */
097        public void setOrganizationReversionCode(String organizationReversionCode) {
098            this.organizationReversionCode = organizationReversionCode;
099        }
100    
101        /**
102         * Gets the organizationReversionCategory attribute.
103         * 
104         * @return Returns the organizationReversionCategory.
105         */
106        public OrganizationReversionCategory getOrganizationReversionCategory() {
107            return organizationReversionCategory;
108        }
109    
110        /**
111         * Sets the organizationReversionCategory attribute value.
112         * 
113         * @param organizationReversionCategory The organizationReversionCategory to set.
114         */
115        public void setOrganizationReversionCategory(OrganizationReversionCategory organizationReversionCategory) {
116            this.organizationReversionCategory = organizationReversionCategory;
117        }
118    
119        /**
120         * Gets the organizationReversionCategoryCode attribute.
121         * 
122         * @return Returns the organizationReversionCategoryCode.
123         */
124        public String getOrganizationReversionCategoryCode() {
125            return organizationReversionCategoryCode;
126        }
127    
128        /**
129         * Sets the organizationReversionCategoryCode attribute value.
130         * 
131         * @param organizationReversionCategoryCode The organizationReversionCategoryCode to set.
132         */
133        public void setOrganizationReversionCategoryCode(String organizationReversionCategoryCode) {
134            this.organizationReversionCategoryCode = organizationReversionCategoryCode;
135        }
136    
137        /**
138         * Gets the organizationReversionObjectCode attribute.
139         * 
140         * @return Returns the organizationReversionObjectCode.
141         */
142        public String getOrganizationReversionObjectCode() {
143            return organizationReversionObjectCode;
144        }
145    
146        /**
147         * Sets the organizationReversionObjectCode attribute value.
148         * 
149         * @param organizationReversionObjectCode The organizationReversionObjectCode to set.
150         */
151        public void setOrganizationReversionObjectCode(String organizationReversionObjectCode) {
152            this.organizationReversionObjectCode = organizationReversionObjectCode;
153        }
154    
155        /**
156         * Gets the parentGlobalOrganizationReversion attribute. This field does not persist, and is populated by
157         * OrganzationReversionChangeMaintainable.
158         * 
159         * @return Returns the parentGlobalOrganizationReversion.
160         */
161        public OrganizationReversionGlobal getParentGlobalOrganizationReversion() {
162            return parentGlobalOrganizationReversion;
163        }
164    
165        /**
166         * Sets the parentGlobalOrganizationReversion attribute value. This field does not persist, and is populated by
167         * OrganizationReversionGlobalMaintainableImpl.
168         * 
169         * @param parentGlobalOrganizationReversion The parentGlobalOrganizationReversion to set.
170         */
171        public void setParentGlobalOrganizationReversion(OrganizationReversionGlobal parentGlobalOrganizationReversion) {
172            this.parentGlobalOrganizationReversion = parentGlobalOrganizationReversion;
173        }
174    
175        /**
176         * Gets the organizationReversionObject attribute. 
177         * @return Returns the organizationReversionObject.
178         */
179        public ObjectCode getOrganizationReversionObject() {
180            return organizationReversionObject;
181        }
182    
183        /**
184         * Sets the organizationReversionObject attribute value.
185         * @param organizationReversionObject The organizationReversionObject to set.
186         */
187        public void setOrganizationReversionObject(ObjectCode organizationReversionObject) {
188            this.organizationReversionObject = organizationReversionObject;
189        }
190    
191        /**
192         * This utility method converts the name of a property into a string suitable for being part of a locking representation.
193         * 
194         * @param keyName the name of the property to convert to a locking representation
195         * @return a part of a locking representation
196         */
197        private String convertKeyToLockingRepresentation(String keyName) {
198            StringBuffer sb = new StringBuffer();
199            sb.append(keyName);
200            sb.append(KFSConstants.Maintenance.AFTER_FIELDNAME_DELIM);
201            String keyValue = "";
202            try {
203                Object keyValueObj = PropertyUtils.getProperty(this, keyName);
204                if (keyValueObj != null) {
205                    keyValue = keyValueObj.toString();
206                }
207            }
208            catch (IllegalAccessException iae) {
209                LOG.info("Illegal access exception while attempting to read property " + keyName, iae);
210            }
211            catch (InvocationTargetException ite) {
212                LOG.info("Illegal Target Exception while attempting to read property " + keyName, ite);
213            }
214            catch (NoSuchMethodException nsme) {
215                LOG.info("There is no such method to read property " + keyName + " in this class.", nsme);
216            }
217            finally {
218                sb.append(keyValue);
219            }
220            sb.append(KFSConstants.Maintenance.AFTER_VALUE_DELIM);
221            return sb.toString();
222        }
223    
224        /**
225         * This returns a string of object code names associated with the object code in this org rev change detail.
226         * 
227         * @return String of distinct object code names
228         */
229        public String getObjectCodeNames() {
230            String objectCodeNames = "";
231            if (!StringUtils.isBlank(this.getOrganizationReversionObjectCode())) {
232                if (this.getParentGlobalOrganizationReversion().getUniversityFiscalYear() != null && this.getParentGlobalOrganizationReversion().getOrganizationReversionGlobalOrganizations() != null && this.getParentGlobalOrganizationReversion().getOrganizationReversionGlobalOrganizations().size() > 0) {
233                    // find distinct chart of account codes
234                    SortedSet<String> chartCodes = new TreeSet<String>();
235                    for (OrganizationReversionGlobalOrganization org : this.getParentGlobalOrganizationReversion().getOrganizationReversionGlobalOrganizations()) {
236                        chartCodes.add(org.getChartOfAccountsCode());
237                    }
238                    String[] chartCodesArray = new String[chartCodes.size()];
239                    int i = 0;
240                    for (String chartCode : chartCodes) {
241                        chartCodesArray[i] = chartCode;
242                        i++;
243                    }
244                    objectCodeNames = (String) SpringContext.getBean(ObjectCodeService.class).getObjectCodeNamesByCharts(this.getParentGlobalOrganizationReversion().getUniversityFiscalYear(), chartCodesArray, this.getOrganizationReversionObjectCode());
245                }
246            }
247            return objectCodeNames;
248        }
249    }