View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.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.kpme.tklm.time.timeblock.TimeBlockBo;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  
28  import com.thoughtworks.xstream.XStream;
29  import com.thoughtworks.xstream.io.xml.DomDriver;
30  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
31  
32  /**
33   * A temporary Service class to serialize the Object into XML file
34   * 
35   */
36  public class TimeBlockSerializerService {
37  	/**
38  	 * This method will read all the timeBlock objects from DB and will
39  	 * serialize it to ObjData.xml file in user home folder
40  	 * 
41  	 * @throws FileNotFoundException
42  	 */
43  	public void serializeToXML() throws FileNotFoundException {
44  		// Fetching data using BO service
45  		Collection timeBlocks = KRADServiceLocatorWeb.getLegacyDataAdapter()
46  				.findAll(TimeBlockBo.class);
47  		Iterator<TimeBlockBo> itr = timeBlocks.iterator();
48  		while (itr.hasNext()) {
49  			TimeBlockBo timeBlockObj = itr.next();
50  			XStream xs = new XStream(new DomDriver());
51  			FileOutputStream fos;
52  			// writting it to file temporarily 'temp.xml' file will be created
53  			// in user home directory
54  			fos = new FileOutputStream(System.getProperty("user.home")
55  					+ "\\TimeBlockData.xml", true);
56  			xs.toXML(timeBlockObj, fos);
57  		}
58  
59  	}
60  
61  	public void serializeToCSV() throws FileNotFoundException {
62  		// Fetching data using BO service
63  		Collection timeBlocks = KRADServiceLocatorWeb.getLegacyDataAdapter()
64  				.findAll(TimeBlockBo.class);
65  		Iterator<TimeBlockBo> itr = timeBlocks.iterator();
66  		while (itr.hasNext()) {
67  
68  			TimeBlockBo timeBlockObj = itr.next();
69  			try {
70  				// Create file
71  				FileWriter fstream = new FileWriter(System.getProperty("user.home")
72  						+ "\\TimeBlockData.csv", true);
73  				BufferedWriter out = new BufferedWriter(fstream);
74  				out.write(timeBlockObj.toCSVString());
75  				// Close the output BufferedWriter
76  				out.close();
77  			} catch (Exception e) {// Catch exception if any
78  				e.printStackTrace();
79  			}
80  		}
81  
82  	}
83  
84  }