View Javadoc
1   /**
2    * Copyright 2011-2014 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.computerlabs.dao;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.mobility.computerlabs.entity.Lab;
21  import org.kuali.mobility.computerlabs.entity.LabGroup;
22  import org.kuali.mobility.computerlabs.entity.LabGroupImpl;
23  import org.kuali.mobility.computerlabs.entity.Location;
24  import org.kuali.mobility.computerlabs.util.*;
25  import org.springframework.context.ApplicationContext;
26  import org.springframework.context.ApplicationContextAware;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.List;
31  
32  public class ComputerLabsDaoImpl implements ComputerLabsDao, ApplicationContextAware {
33  	private static final Logger LOG = Logger.getLogger(ComputerLabsDaoImpl.class);
34  
35  	private ApplicationContext applicationContext;
36  	private List<? extends LabGroup> labGroups;
37  
38  	@Override
39  	public Lab getLab( String labUid ) {
40  		Lab myLab = null;
41  		for( LabGroup g : getLabGroups() ) {
42  			for( Location l : g.getLocations() ) {
43  				Collection<? extends Lab> myLabs = CollectionUtils.select( l.getLabs(), new LabPredicate( null, labUid ) );
44  				if( null != myLabs && myLabs.size() > 0 ) {
45  					myLab = (Lab)(myLabs.toArray())[0];
46  					break;
47  				}
48  			}
49  		}
50  		if( myLab == null ) {
51  			LOG.error( "Lab not found for UID "+labUid );
52  		}
53  		return myLab;
54  	}
55  
56  	@Override
57  	public List<? extends Lab> getLabs( String locationId, String buildingCode ) {
58  		List<? extends Lab> myLabs = new ArrayList<Lab>();
59  		for( LabGroup g : getLabGroups() ) {
60  			Collection<? extends Location> myLocations = CollectionUtils.select( g.getLocations(), new LocationPredicate( locationId ));
61  			for( Location l : myLocations ) {
62  				myLabs.addAll( CollectionUtils.select( l.getLabs(), new LabPredicate( buildingCode, null ) ) );
63  			}
64  		}
65  		return myLabs;
66  	}
67  
68  	@Override
69  	public List<? extends Location> getLocations( String groupId ) {
70  		List<? extends Location> myLocations;
71          if( null == groupId ) {
72              myLocations = new ArrayList<Location>();
73              for( LabGroup group : getLabGroups() ) {
74                  myLocations.addAll( CollectionUtils.collect(group.getLocations(),new LocationTransform()));
75              }
76          } else {
77              LabGroup myGroup;
78              myGroup = getLabGroup( groupId );
79              myLocations = myGroup.getLocations();
80          }
81  		return myLocations;
82  	}
83  
84  	@Override
85  	public List<? extends LabGroup> getLabGroups() {
86  		if( labGroups == null ) {
87  			List<LabGroupImpl> myLabGroups = new ArrayList<LabGroupImpl>();
88  			labGroups = myLabGroups;
89  		}
90  		return labGroups;
91  	}
92  
93  	@Override
94  	public LabGroup getLabGroup(String groupId) {
95  		LabGroup myGroup = null;
96  		Collection<? extends LabGroup> myGroups = CollectionUtils.select( getLabGroups(), new LabGroupPredicate( groupId ) );
97  
98  		if( myGroups != null ) {
99  			if( myGroups.size() > 1 ) {
100 				LOG.debug( "Multiple groups found for id. This shouldn't happen." );
101 			} else {
102 				LabGroupTransform transform = new LabGroupTransform();
103 				for( Object obj : myGroups ) {
104 					myGroup = transform.transform(obj);
105 				}
106 			}
107 		}
108 		return myGroup;
109 	}
110 
111 	/**
112 	 * @return the applicationContext
113 	 */
114 	public ApplicationContext getApplicationContext() {
115 		return applicationContext;
116 	}
117 
118 	/**
119 	 * @param applicationContext the applicationContext to set
120 	 */
121 	public void setApplicationContext(ApplicationContext applicationContext) {
122 		this.applicationContext = applicationContext;
123 	}
124 
125 	@Override
126 	public void setLabGroups(List<? extends LabGroup> labGroups) {
127 		this.labGroups = labGroups;
128 	}
129 	
130 	public void retrieveAndSaveSpreadsheetDataAsXML( String feedURL ) {
131 	}
132 	
133 }