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.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.kuali.mobility.computerlabs.entity.Lab;
22  import org.kuali.mobility.computerlabs.entity.LabGroup;
23  import org.kuali.mobility.computerlabs.entity.LabGroupImpl;
24  import org.kuali.mobility.computerlabs.entity.Location;
25  import org.kuali.mobility.computerlabs.util.*;
26  import org.springframework.context.ApplicationContext;
27  import org.springframework.context.ApplicationContextAware;
28  
29  import java.util.ArrayList;
30  import java.util.Collection;
31  import java.util.List;
32  
33  public class ComputerLabsDaoImpl implements ComputerLabsDao, ApplicationContextAware {
34  	private static final Logger LOG = LoggerFactory.getLogger(ComputerLabsDaoImpl.class);
35  
36  	private ApplicationContext applicationContext;
37  	private List<? extends LabGroup> labGroups;
38  
39  	@Override
40  	public Lab getLab( String labUid ) {
41  		Lab myLab = null;
42  		for( LabGroup g : getLabGroups() ) {
43  			for( Location l : g.getLocations() ) {
44  				Collection<? extends Lab> myLabs = CollectionUtils.select( l.getLabs(), new LabPredicate( null, labUid ) );
45  				if( null != myLabs && myLabs.size() > 0 ) {
46  					myLab = (Lab)(myLabs.toArray())[0];
47  					break;
48  				}
49  			}
50  		}
51  		if( myLab == null ) {
52  			LOG.error( "Lab not found for UID "+labUid );
53  		}
54  		return myLab;
55  	}
56  
57  	@Override
58  	public List<? extends Lab> getLabs( String locationId, String buildingCode ) {
59  		List<? extends Lab> myLabs = new ArrayList<Lab>();
60  		for( LabGroup g : getLabGroups() ) {
61  			Collection<? extends Location> myLocations = CollectionUtils.select( g.getLocations(), new LocationPredicate( locationId ));
62  			for( Location l : myLocations ) {
63  				myLabs.addAll( CollectionUtils.select( l.getLabs(), new LabPredicate( buildingCode, null ) ) );
64  			}
65  		}
66  		return myLabs;
67  	}
68  
69  	@Override
70  	public List<? extends Location> getLocations( String groupId ) {
71  		List<? extends Location> myLocations;
72          if( null == groupId ) {
73              myLocations = new ArrayList<Location>();
74              for( LabGroup group : getLabGroups() ) {
75                  myLocations.addAll( CollectionUtils.collect(group.getLocations(),new LocationTransform()));
76              }
77          } else {
78              LabGroup myGroup;
79              myGroup = getLabGroup( groupId );
80              myLocations = myGroup.getLocations();
81          }
82  		return myLocations;
83  	}
84  
85  	@Override
86  	public List<? extends LabGroup> getLabGroups() {
87  		if( labGroups == null ) {
88  			List<LabGroupImpl> myLabGroups = new ArrayList<LabGroupImpl>();
89  			labGroups = myLabGroups;
90  		}
91  		return labGroups;
92  	}
93  
94  	@Override
95  	public LabGroup getLabGroup(String groupId) {
96  		LabGroup myGroup = null;
97  		Collection<? extends LabGroup> myGroups = CollectionUtils.select( getLabGroups(), new LabGroupPredicate( groupId ) );
98  
99  		if( myGroups != null ) {
100 			if( myGroups.size() > 1 ) {
101 				LOG.debug( "Multiple groups found for id. This shouldn't happen." );
102 			} else {
103 				LabGroupTransform transform = new LabGroupTransform();
104 				for( Object obj : myGroups ) {
105 					myGroup = transform.transform(obj);
106 				}
107 			}
108 		}
109 		return myGroup;
110 	}
111 
112 	/**
113 	 * @return the applicationContext
114 	 */
115 	public ApplicationContext getApplicationContext() {
116 		return applicationContext;
117 	}
118 
119 	/**
120 	 * @param applicationContext the applicationContext to set
121 	 */
122 	public void setApplicationContext(ApplicationContext applicationContext) {
123 		this.applicationContext = applicationContext;
124 	}
125 
126 	@Override
127 	public void setLabGroups(List<? extends LabGroup> labGroups) {
128 		this.labGroups = labGroups;
129 	}
130 	
131 	public void retrieveAndSaveSpreadsheetDataAsXML( String feedURL ) {
132 	}
133 	
134 }