Permalink
Browse files

ADDED: MasterDetailsEngine.writeFile() override that allows writing a…

… single MasterDetails object

ADDED: New MasterDetailsSample sample

git-svn-id: https://jfilehelpers.svn.sourceforge.net/svnroot/jfilehelpers@36 e6aea695-393b-0410-bcd1-a96c4ba3c4be
  • Loading branch information...
1 parent b2cdde0 commit 99361ea728bbda754c674be2b95aa7ad63ba47bb fcoury committed Oct 21, 2008
@@ -0,0 +1,123 @@
+/*
+ * MasterDetailsSample.java
+ *
+ * Copyright (C) 2007 Felipe Gonçalves Coury <felipe.coury@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+package org.coury.jfilehelpers.samples.masterdetail;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.coury.jfilehelpers.annotations.DelimitedRecord;
+import org.coury.jfilehelpers.masterdetail.MasterDetailEngine;
+import org.coury.jfilehelpers.masterdetail.MasterDetailSelector;
+import org.coury.jfilehelpers.masterdetail.MasterDetails;
+import org.coury.jfilehelpers.masterdetail.RecordAction;
+
+public class MasterDetailsSample {
+ public static void main(String[] args) throws IOException {
+ MasterDetailEngine<Order, Item> engine = new MasterDetailEngine<Order, Item>(Order.class, Item.class, new MasterDetailSelector() {
+
+ @Override
+ public RecordAction getRecordAction(String recordString) {
+ if (recordString.startsWith("-")) {
+ return RecordAction.Detail;
+ }
+ else {
+ return RecordAction.Master;
+ }
+ }
+
+ });
+
+ String teste =
+ "1,4039BF,1130.45" + "\n" +
+ "-1,teste,12,120.01" + "\n" +
+ "2,302512,1320.11" + "\n" +
+ "-2,awesome,14,130.01";
+
+ List<MasterDetails<Order, Item>> orders = engine.fromString(teste);
+ for (MasterDetails<Order, Item> md : orders) {
+ Order order = md.getMaster();
+ System.out.println("Order: " + order.id + " - " + order.orderNo);
+ for (Item item : md.getDetails()) {
+ System.out.println(" - " + (item.id*-1) + " - " + item.description);
+ }
+ }
+
+ engine.writeFile("c:/temp/out.txt", orders);
+
+ MasterDetails<Order, Item> masterDetail = new MasterDetails<Order, Item>();
+
+ Order sampleOrder = new Order(1, "1234ORD", 1032.11);
+ List<Item> items = new ArrayList<Item>();
+ items.add(new Item(-2, "Teste 123", 2, 100.00));
+ items.add(new Item(-3, "Teste 456", 2, 200.00));
+
+ masterDetail.setMaster(sampleOrder);
+ masterDetail.addDetails(items);
+
+ // before the addition of my new method
+ List<MasterDetails<Order, Item>> mdList = new ArrayList<MasterDetails<Order, Item>>();
+ mdList.add(masterDetail);
+
+ engine.writeFile("c:/temp/out2.txt", mdList);
+
+ // after the additions
+ engine.writeFile("c:/temp/out3.txt", masterDetail);
+ }
+
+ @DelimitedRecord(",")
+ public static class Order {
+ public int id;
+ public String orderNo;
+ public double total;
+
+ public Order() {
+ }
+
+ public Order(int id, String orderNo, double total) {
+ super();
+ this.id = id;
+ this.orderNo = orderNo;
+ this.total = total;
+// this.items = items;
+ }
+ }
+
+ @DelimitedRecord(",")
+ public static class Item {
+ public int id;
+ public String description;
+ public int qtd;
+ public double price;
+
+ public Item() {
+ }
+
+ public Item(int id, String description, int qtd, double price) {
+ super();
+ this.description = description;
+ this.id = id;
+ this.qtd = qtd;
+ this.price = price;
+ }
+
+
+ }
+}
@@ -21,6 +21,7 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
@@ -91,6 +92,10 @@ public RecordAction getRecordAction(String recordString) {
return tempRes;
}
+ public List<MasterDetails<MT, DT>> fromString(String s) throws IOException {
+ return readStream(new InputStreamReader(new ByteArrayInputStream(s.getBytes())));
+ }
+
public List<MasterDetails<MT, DT>> readFile(String fileName) throws IOException {
List<MasterDetails<MT, DT>> tempRes = null;
@@ -108,6 +113,13 @@ public RecordAction getRecordAction(String recordString) {
return tempRes;
}
+ public void writeFile(String fileName, MasterDetails<MT, DT> record) throws IOException {
+ List<MasterDetails<MT, DT>> list = new ArrayList<MasterDetails<MT, DT>>();
+ list.add(record);
+
+ writeFile(fileName, list);
+ }
+
public void writeFile(String fileName, List<MasterDetails<MT, DT>> records) throws IOException {
writeFile(fileName, records, -1);
}
@@ -165,6 +177,8 @@ private void writeStream(OutputStreamWriter osr, List<MasterDetails<MT, DT>> rec
writer.write(currentLine + StringHelper.NEW_LINE);
}
}
+
+ writer.flush();
}
catch (Exception ex) {
ex.printStackTrace();

0 comments on commit 99361ea

Please sign in to comment.