001/**
002 * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational
003 * Community License, Version 2.0 (the "License"); you may not use this file
004 * except in compliance with the License. You may obtain a copy of the License
005 * at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
011 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
012 * License for the specific language governing permissions and limitations under
013 * the License.
014 */
015package org.kuali.mobility.emergencyinfo.dao;
016
017import java.io.IOException;
018import java.net.MalformedURLException;
019import java.net.URL;
020import java.util.ArrayList;
021import java.util.List;
022
023import javax.persistence.EntityManager;
024import javax.persistence.PersistenceContext;
025import org.apache.commons.collections.CollectionUtils;
026import org.apache.log4j.Logger;
027import org.kuali.mobility.emergencyinfo.entity.EmergencyInfo;
028import org.kuali.mobility.emergencyinfo.entity.EmergencyInfoImpl;
029
030import org.kuali.mobility.emergencyinfo.entity.EmergencyInfoJPAImpl;
031import org.kuali.mobility.emergencyinfo.util.CampusPredicate;
032import org.kuali.mobility.util.mapper.DataMapper;
033import org.springframework.beans.factory.annotation.Autowired;
034import org.springframework.stereotype.Repository;
035
036@Repository
037public class EmergencyInfoWSDaoImpl implements EmergencyInfoDao {
038
039        private static final Logger LOG = Logger.getLogger( EmergencyInfoWSDaoImpl.class );
040
041        @PersistenceContext
042        private EntityManager entityManager;
043
044        @Autowired
045        private DataMapper dataMapper;
046
047        private String sourceUrl;
048        private List<EmergencyInfoImpl> contacts = null;
049
050        private void initData() {
051                contacts = new ArrayList<EmergencyInfoImpl>();
052                try {
053                        URL url = new URL( getSourceUrl() );
054                        contacts.addAll(getDataMapper().mapData(contacts, url, "emergencyinfoMapping.xml"));
055                } catch( MalformedURLException mue ) {
056                        LOG.error( "URL supplied for sourceUrl is malformed.", mue );
057                } catch( ClassNotFoundException cnfe ) {
058                        LOG.error( "Data mapper unable to load class defined in mapping.", cnfe );
059                } catch( IOException ioe ) {
060                        LOG.error( "Unabled to access emergency info source url.", ioe );
061                } catch (Exception e) {
062                        LOG.error( "If you see this, something really, really strange happened.", e);
063                }
064        }
065
066        public EntityManager getEntityManager() {
067                return entityManager;
068        }
069
070        public void setEntityManager(EntityManager entityManager) {
071                this.entityManager = entityManager;
072        }
073
074        @Override
075        public EmergencyInfo findEmergencyInfoById(Long id) {
076                // TODO Auto-generated method stub
077                return null;
078        }
079
080        @Override
081        public List<? extends EmergencyInfo> findAllEmergencyInfo() {
082                if( null == contacts ) {
083                        initData();
084                }
085                return contacts;
086        }
087
088        @Override
089        public List<? extends EmergencyInfo> findAllEmergencyInfoByCampus(String campus) {
090                if( null == contacts ) {
091                        initData();
092                }
093                List<? extends EmergencyInfo> campusContacts = new ArrayList<EmergencyInfoImpl>();
094                campusContacts.addAll( CollectionUtils.select( contacts, new CampusPredicate(campus)) );
095                return campusContacts;
096        }
097
098        @Override
099        public Long saveEmergencyInfo(EmergencyInfo emergencyInfo) {
100                // TODO Auto-generated method stub
101                return null;
102        }
103
104        @Override
105        public void deleteEmergencyInfoById(Long id) {
106                // TODO Auto-generated method stub
107        }
108
109        @Override
110        public void reorder(Long id, boolean up) {
111                // TODO Auto-generated method stub
112        }
113
114        /**
115         * @return the sourceUrl
116         */
117        public String getSourceUrl() {
118                return sourceUrl;
119        }
120
121        /**
122         * @param sourceUrl the sourceUrl to set
123         */
124        public void setSourceUrl(String sourceUrl) {
125                this.sourceUrl = sourceUrl;
126        }
127
128        /**
129         * @return the dataMapper
130         */
131        public DataMapper getDataMapper() {
132                return dataMapper;
133        }
134
135        /**
136         * @param dataMapper the dataMapper to set
137         */
138        public void setDataMapper(DataMapper dataMapper) {
139                this.dataMapper = dataMapper;
140        }
141}