001    /**
002     * Copyright 2004-2013 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.hr.time.scheduler;
017    
018    import java.io.BufferedWriter;
019    import java.io.FileNotFoundException;
020    import java.io.FileOutputStream;
021    import java.io.FileWriter;
022    import java.util.Collection;
023    import java.util.Iterator;
024    
025    import org.kuali.hr.time.timeblock.TimeBlock;
026    import org.kuali.rice.krad.service.KRADServiceLocator;
027    
028    import com.thoughtworks.xstream.XStream;
029    import com.thoughtworks.xstream.io.xml.DomDriver;
030    
031    /**
032     * A temporary Service class to serialize the Object into XML file
033     * 
034     */
035    public class TimeBlockSerializerService {
036            /**
037             * This method will read all the timeBlock objects from DB and will
038             * serialize it to ObjData.xml file in user home folder
039             * 
040             * @throws FileNotFoundException
041             */
042            public void serializeToXML() throws FileNotFoundException {
043                    // Fetching data using BO service
044                    Collection timeBlocks = KRADServiceLocator.getBusinessObjectService()
045                                    .findAll(TimeBlock.class);
046                    Iterator<TimeBlock> itr = timeBlocks.iterator();
047                    while (itr.hasNext()) {
048                            TimeBlock timeBlockObj = itr.next();
049                            XStream xs = new XStream(new DomDriver());
050                            FileOutputStream fos;
051                            // writting it to file temporarily 'temp.xml' file will be created
052                            // in user home directory
053                            fos = new FileOutputStream(System.getProperty("user.home")
054                                            + "\\TimeBlockData.xml", true);
055                            xs.toXML(timeBlockObj, fos);
056                    }
057    
058            }
059    
060            public void serializeToCSV() throws FileNotFoundException {
061                    // Fetching data using BO service
062                    Collection timeBlocks = KRADServiceLocator.getBusinessObjectService()
063                                    .findAll(TimeBlock.class);
064                    Iterator<TimeBlock> itr = timeBlocks.iterator();
065                    while (itr.hasNext()) {
066    
067                            TimeBlock timeBlockObj = itr.next();
068                            try {
069                                    // Create file
070                                    FileWriter fstream = new FileWriter(System.getProperty("user.home")
071                                                    + "\\TimeBlockData.csv", true);
072                                    BufferedWriter out = new BufferedWriter(fstream);
073                                    out.write(timeBlockObj.toCSVString());
074                                    // Close the output BufferedWriter
075                                    out.close();
076                            } catch (Exception e) {// Catch exception if any
077                                    e.printStackTrace();
078                            }
079                    }
080    
081            }
082    
083    }