001    /**
002     * Copyright 2005-2014 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.rice.ken.core;
017    
018    import org.apache.log4j.Logger;
019    import org.kuali.rice.core.api.lifecycle.LifecycleBean;
020    import org.springframework.beans.BeansException;
021    import org.springframework.beans.factory.BeanFactory;
022    import org.springframework.beans.factory.BeanFactoryAware;
023    import org.springframework.transaction.PlatformTransactionManager;
024    import org.springframework.transaction.support.TransactionTemplate;
025    
026    import javax.sql.DataSource;
027    
028    /**
029     * Eager-initializing singleton bean that performs some notification startup operations
030     * @author Kuali Rice Team (rice.collab@kuali.org)
031     */
032    public class NotificationLifeCycle extends LifecycleBean implements BeanFactoryAware {
033        private static final Logger LOG = Logger.getLogger(NotificationLifeCycle.class);
034    
035        //private String ojbPlatform;
036        private BeanFactory theFactory;
037        private PlatformTransactionManager txMgr;
038        private DataSource dataSource;
039    
040        /**
041         * This method sets the OJB platform.
042         * @param platform
043         */
044        /*
045        public void setOjbPlatform(String platform) {
046            this.ojbPlatform = platform;
047        }*/
048    
049        /**
050         * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
051         */
052        public void setBeanFactory(BeanFactory theFactory) throws BeansException {
053             this.theFactory = theFactory;
054        }
055    
056        public void setTransactionManager(PlatformTransactionManager txMgr) {
057            this.txMgr = txMgr;
058        }
059        
060        public void setDataSource(DataSource dataSource) {
061            this.dataSource = dataSource;
062        }
063    
064        /**
065         * Helper method for creating a TransactionTemplate initialized to create
066         * a new transaction
067         * @return a TransactionTemplate initialized to create a new transaction
068         */
069        protected TransactionTemplate createNewTransaction() {
070            TransactionTemplate tt = new TransactionTemplate(txMgr);
071            tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
072            return tt;
073        }
074    
075        /**
076         * @see org.kuali.rice.ken.core.BaseLifecycle#start()
077         */
078        public void start() throws Exception {
079            /*if (ojbPlatform == null) {
080                throw new BeanInitializationException("No platform was configured, please configure the datasource.ojb.platform property.");
081            }*/
082    
083            GlobalNotificationServiceLocator.init(theFactory);
084            /*
085            createNewTransaction().execute(new TransactionCallback() {
086                public Object doInTransaction(TransactionStatus txStatus) {
087                    JdbcTemplate t = new JdbcTemplate(dataSource);
088                    Boolean dataLoaded = (Boolean) t.execute(new StatementCallback() {
089                        public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
090                            ResultSet rs = stmt.executeQuery("select * from APP_META_T where NAME = 'ken.bootstrap.loaded' and VALUE = 'true'");
091                            try {
092                                return rs.next();
093                            } finally {
094                                rs.close();
095                            }
096                        }
097                    });
098                    if (!dataLoaded.booleanValue()) {
099                        loadXmlFile("classpath:data/NotificationData.xml");
100                        
101                        t.execute(new StatementCallback() {
102                            public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
103                                ResultSet rs = stmt.executeQuery("update APP_META_T where NAME = 'ken.bootstrap.loaded' and VALUE = 'true'");
104                                try {
105                                    return rs.next();
106                                } finally {
107                                    rs.close();
108                                }
109                            }
110                        });
111                    }
112                }
113            });
114            */
115            
116            //LOG.info("Setting OJB platform to: " + ojbPlatform);
117            //PersistenceBrokerFactory.defaultPersistenceBroker().serviceConnectionManager().getConnectionDescriptor().setDbms(ojbPlatform);
118            super.start();
119        }
120    
121        // yanked from KEWXmlDataLoaderLifecycle
122        /*
123        protected void loadXmlFile(String fileName) throws Exception {
124            Resource resource = new DefaultResourceLoader().getResource(fileName);
125            InputStream xmlFile = resource.getInputStream();
126            if (xmlFile == null) {
127                    throw new ConfigurationException("Didn't find file " + fileName);
128            }
129            List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
130            XmlDocCollection docCollection = getFileXmlDocCollection(xmlFile, "UnitTestTemp");
131            xmlFiles.add(docCollection);
132            KEWServiceLocator.getXmlIngesterService().ingest(xmlFiles);
133            for (Iterator iterator = docCollection.getXmlDocs().iterator(); iterator.hasNext();) {
134                    XmlDoc doc = (XmlDoc) iterator.next();
135                    if (!doc.isProcessed()) {
136                            throw new RuntimeException("Failed to ingest xml doc: " + doc.getName());
137                    }
138            }
139        }
140    
141        protected FileXmlDocCollection getFileXmlDocCollection(InputStream xmlFile, String tempFileName) throws IOException {
142            if (xmlFile == null) {
143                throw new RuntimeException("Didn't find the xml file " + tempFileName);
144            }
145            File temp = File.createTempFile(tempFileName, ".xml");
146            FileOutputStream fos = new FileOutputStream(temp);
147            int data = -1;
148            while ((data = xmlFile.read()) != -1) {
149                fos.write(data);
150            }
151            fos.close();
152            return new FileXmlDocCollection(temp);
153        }*/
154    
155        /**
156         * @see org.kuali.rice.ken.core.BaseLifecycle#stop()
157         */
158        public void stop() throws Exception {
159            GlobalNotificationServiceLocator.destroy();
160            super.stop();
161        }
162    }