View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.cam.businessobject;
20  
21  import java.sql.Timestamp;
22  import java.util.ArrayList;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.kfs.module.cam.CamsConstants;
28  import org.kuali.kfs.sys.context.SpringContext;
29  import org.kuali.rice.core.api.datetime.DateTimeService;
30  import org.kuali.rice.krad.bo.DocumentHeader;
31  import org.kuali.rice.krad.bo.GlobalBusinessObject;
32  import org.kuali.rice.krad.bo.GlobalBusinessObjectDetail;
33  import org.kuali.rice.krad.bo.PersistableBusinessObject;
34  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
35  import org.kuali.rice.krad.service.BusinessObjectService;
36  
37  /**
38   * @author Kuali Nervous System Team (kualidev@oncourse.iu.edu)
39   */
40  
41  public class AssetLocationGlobal extends PersistableBusinessObjectBase implements GlobalBusinessObject {
42  
43  	private String documentNumber;
44      private DocumentHeader documentHeader;
45      private List<AssetLocationGlobalDetail> assetLocationGlobalDetails;
46      
47  	/**
48  	 * Default constructor.
49  	 */
50  	public AssetLocationGlobal() {
51          assetLocationGlobalDetails = new ArrayList<AssetLocationGlobalDetail>();
52  	}
53  
54  	/**
55  	 * Gets the documentNumber attribute.
56  	 * 
57  	 * @return Returns the documentNumber
58  	 * 
59  	 */
60  	public String getDocumentNumber() { 
61  		return documentNumber;
62  	}
63  
64  	/**
65  	 * Sets the documentNumber attribute.
66  	 * 
67  	 * @param documentNumber The documentNumber to set.
68  	 * 
69  	 */
70  	public void setDocumentNumber(String documentNumber) {
71  		this.documentNumber = documentNumber;
72  	}
73  
74  	/**
75       * Gets the documentHeader attribute. 
76       * @return Returns the documentHeader.
77       */
78      public DocumentHeader getDocumentHeader() {
79          return documentHeader;
80      }
81  
82      /**
83       * Sets the documentHeader attribute value.
84       * @param documentHeader The documentHeader to set.
85       * @deprecated
86       */
87      public void setDocumentHeader(DocumentHeader documentHeader) {
88          this.documentHeader = documentHeader;
89      }
90      
91      /**
92       * Gets the assetLocationGlobalDetails attribute. 
93       * @return Returns the assetLocationGlobalDetails.
94       */
95      public List<AssetLocationGlobalDetail> getAssetLocationGlobalDetails() {
96          return assetLocationGlobalDetails;
97      }
98  
99      /**
100      * Sets the assetLocationGlobalDetails attribute value.
101      * @param assetLocationGlobalDetails The assetLocationGlobalDetails to set.
102      */
103     public void setAssetLocationGlobalDetails(List<AssetLocationGlobalDetail> assetLocationGlobalDetails) {
104         this.assetLocationGlobalDetails = assetLocationGlobalDetails;
105     }
106 
107     /**
108      * @see org.kuali.rice.krad.document.GlobalBusinessObject#getGlobalChangesToDelete()
109      */
110     public List<PersistableBusinessObject> generateDeactivationsToPersist() {
111         return null;
112     }
113 
114     /**
115      * This returns a list of Assets to update
116      * 
117      * @see org.kuali.rice.krad.bo.GlobalBusinessObject#generateGlobalChangesToPersist()
118      */
119     public List<PersistableBusinessObject> generateGlobalChangesToPersist() {
120         // the list of persist-ready BOs
121         List<PersistableBusinessObject> persistables = new ArrayList();
122 
123         // walk over each change detail record
124         for (AssetLocationGlobalDetail detail : assetLocationGlobalDetails) {
125             boolean isCampusCodeChanged = false, isBuildingCodeChanged = false, isBuildingRoomNumberChanged = false, isBuildingSubRoomNumberChanged = false, isCampusTagNumberChanged = false; 
126             // load the object by keys
127             Asset asset = (Asset) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(Asset.class, detail.getPrimaryKeys());
128 
129             // if we got a valid asset, do the processing
130             if (asset != null) {
131 
132                 if (!StringUtils.equalsIgnoreCase(asset.getCampusCode(), detail.getCampusCode()) ) {
133                     asset.setCampusCode(detail.getCampusCode());
134                     isCampusCodeChanged = true;
135                 }
136                 
137                 if (!StringUtils.equalsIgnoreCase(asset.getBuildingCode(), detail.getBuildingCode()) ) {
138                     asset.setBuildingCode(detail.getBuildingCode());
139                     isBuildingCodeChanged = true;
140                 }
141                 
142                 if (!StringUtils.equalsIgnoreCase(asset.getBuildingRoomNumber(), detail.getBuildingRoomNumber()) ) {
143                     asset.setBuildingRoomNumber(detail.getBuildingRoomNumber());
144                     isBuildingRoomNumberChanged = true;
145                 }
146                 
147                 if (!StringUtils.equalsIgnoreCase(asset.getBuildingSubRoomNumber(), detail.getBuildingSubRoomNumber()) ) {
148                     asset.setBuildingSubRoomNumber(detail.getBuildingSubRoomNumber());
149                     isBuildingSubRoomNumberChanged = true;
150                 }
151 
152                 if (!StringUtils.equalsIgnoreCase(detail.getCampusTagNumber(), asset.getCampusTagNumber())) {
153                     asset.setOldTagNumber(asset.getCampusTagNumber());
154                     asset.setCampusTagNumber(detail.getCampusTagNumber());
155                     isCampusTagNumberChanged = true;
156                 }
157                 
158                 updateOffCampusWithOnCampusValues(asset);
159                 
160                 if(isCampusCodeChanged || isBuildingCodeChanged || isBuildingRoomNumberChanged || isBuildingSubRoomNumberChanged || isCampusTagNumberChanged)
161                 {
162                     asset.setLastInventoryDate(new Timestamp(SpringContext.getBean(DateTimeService.class).getCurrentSqlDate().getTime()));
163                 }
164 
165                 persistables.add(asset);
166             }
167         }
168 
169         return persistables;
170     }
171  
172     
173     /**
174      * KFSMI-6695 Location Global allows update to building, room when asest has off campus location fields poplulated, after update
175      * assets has both on campus and off campus. An asset should not have both an on campus address ( buidling, and room) and an off
176      * campus addresses. If the building and room are updated from location global then the following fields should be set to null
177      * WHERE the ast_loc_typ_cd = 'O' cm_ast_loc_t.ast_loc_cntnt_nm cm_ast_loc_t.ast_loc_strt_addr cm_ast_loc_t.ast_loc_city_nm
178      * cm_ast_loc_t.ast_loc_state_cd cm_ast_loc_t.ast_loc_cntry_cd cm_ast_loc_t.ast_loc_zip_cd
179      * 
180      * @param asset
181      */
182     private void updateOffCampusWithOnCampusValues(Asset asset) {
183         List<AssetLocation> toDelete = new ArrayList<AssetLocation>();
184         if (asset.getAssetLocations() != null) {
185             for (AssetLocation location : asset.getAssetLocations()) {
186                 boolean offCampus = CamsConstants.AssetLocationTypeCode.OFF_CAMPUS.equals(location.getAssetLocationTypeCode());
187                 boolean buildingOrRoom = StringUtils.isNotBlank(asset.getBuildingCode()) || StringUtils.isNotBlank(asset.getBuildingRoomNumber());
188                 if (offCampus && buildingOrRoom) {
189                   location.setAssetLocationContactName(null); // cm_ast_loc_t.ast_loc_cntnt_nm
190                   location.setAssetLocationStreetAddress(null); // cm_ast_loc_t.ast_loc_strt_addr
191                   location.setAssetLocationCityName(null); // cm_ast_loc_t.ast_loc_city_nm
192                   location.setAssetLocationStateCode(null); // cm_ast_loc_t.ast_loc_state_cd
193                   location.setAssetLocationCountryCode(null); // cm_ast_loc_t.ast_loc_cntry_cd
194                   location.setAssetLocationZipCode(null); // cm_ast_loc_t.ast_loc_zip_cd
195                   
196                   // this didn't work
197                   //toDelete.add(location);
198                 }
199             }
200         }
201         
202         // this didn't work
203         //asset.getAssetLocations().removeAll(toDelete);        
204      }
205     
206     public boolean isPersistable() {
207         return true;
208     }
209     
210     public List<? extends GlobalBusinessObjectDetail> getAllDetailObjects() {
211         return getAssetLocationGlobalDetails();
212     }
213     
214     /**
215 	 * @see org.kuali.rice.krad.bo.BusinessObjectBase#toStringMapper()
216 	 */
217 	protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
218 	    LinkedHashMap m = new LinkedHashMap();	    
219         m.put("documentNumber", this.documentNumber);
220 	    return m;
221     }
222 }