View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.scheduler;
17  
18  import java.io.BufferedWriter;
19  import java.io.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.FileWriter;
22  import java.util.Collection;
23  import java.util.Iterator;
24  
25  import org.kuali.hr.time.timeblock.TimeBlock;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  
28  import com.thoughtworks.xstream.XStream;
29  import com.thoughtworks.xstream.io.xml.DomDriver;
30  
31  /**
32   * A temporary Service class to serialize the Object into XML file
33   *
34   */
35  public class TimeBlockSerializerService {
36      /**
37       * This method will read all the timeBlock objects from DB and will
38       * serialize it to ObjData.xml file in user home folder
39       *
40       * @throws FileNotFoundException
41       */
42      public void serializeToXML() throws FileNotFoundException {
43          // Fetching data using BO service
44          Collection timeBlocks = KRADServiceLocator.getBusinessObjectService()
45                  .findAll(TimeBlock.class);
46          Iterator<TimeBlock> itr = timeBlocks.iterator();
47          while (itr.hasNext()) {
48              TimeBlock timeBlockObj = itr.next();
49              XStream xs = new XStream(new DomDriver());
50              FileOutputStream fos;
51              // writting it to file temporarily 'temp.xml' file will be created
52              // in user home directory
53              fos = new FileOutputStream(System.getProperty("user.home")
54                      + "\\TimeBlockData.xml", true);
55              xs.toXML(timeBlockObj, fos);
56          }
57  
58      }
59  
60      public void serializeToCSV() throws FileNotFoundException {
61          // Fetching data using BO service
62          Collection timeBlocks = KRADServiceLocator.getBusinessObjectService()
63                  .findAll(TimeBlock.class);
64          Iterator<TimeBlock> itr = timeBlocks.iterator();
65          while (itr.hasNext()) {
66  
67              TimeBlock timeBlockObj = itr.next();
68              try {
69                  // Create file
70                  FileWriter fstream = new FileWriter(System.getProperty("user.home")
71                          + "\\TimeBlockData.csv", true);
72                  BufferedWriter out = new BufferedWriter(fstream);
73                  out.write(timeBlockObj.toCSVString());
74                  // Close the output BufferedWriter
75                  out.close();
76              } catch (Exception e) {// Catch exception if any
77                  e.printStackTrace();
78              }
79          }
80  
81      }
82  
83  }