19 package org.turro.financials.product;
21 import java.io.BufferedReader;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FilenameFilter;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.util.Collection;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import org.turro.string.Strings;
32 import org.turro.auth.Authentication;
33 import org.turro.command.Command;
34 import org.turro.command.Context;
35 import org.turro.contacts.Contact;
36 import org.turro.contacts.GroupIt;
37 import org.turro.contacts.Grouped;
38 import org.turro.contacts.db.ContactsPU;
39 import org.turro.csv.CSVEntry;
40 import org.turro.csv.CSVList;
41 import org.turro.csv.CSVParser;
42 import org.turro.describeit.DescribeItUtil;
43 import org.turro.elephant.context.ElephantContext;
44 import org.turro.file.FileWrapper;
45 import org.turro.financials.db.FinancialsPU;
46 import org.turro.financials.entity.Contract;
47 import org.turro.financials.entity.Product;
48 import org.turro.financials.entity.ProductByContractor;
49 import org.turro.i18n.I_;
50 import org.turro.jpa.Dao;
51 import org.turro.path.Path;
52 import org.zkoss.zk.ui.util.Clients;
60 public static final String
62 PRODUCT_EXPORT_FOLDER =
"/WEB-INF/product/out";
68 public Object execute(Context context) {
69 Clients.showBusy(
I_.
get(
"Import CVS"));
79 try(BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(file)))) {
80 processFile(
new CSVParser(in).
getCSVs());
81 }
catch (FileNotFoundException ex) {
83 }
catch (IOException ex) {
92 if(!root.exists()) root.mkdir();
93 return root.listFiles(
new FilenameFilter() {
95 public boolean accept(File dir, String name) {
96 return name.endsWith(
".csv");
101 private void processFile(CSVList list) {
102 for(CSVEntry v : list) {
103 GroupIt groupIt = createGroups(v);
105 String productCode = v.getField(
"Code");
106 if(!Strings.isBlank(productCode)) {
108 if(product ==
null) {
110 ((
Product) product).setProductCode(productCode);
113 String codeByProvider = v.getField(
"CodeByProvider");
114 Long providerCode = v.getLongField(
"Provider");
115 if(providerCode > 0 && !Strings.isBlank(codeByProvider)) {
116 ProductByContractor pbc = ProductFactory.getContractorProduct(providerCode, codeByProvider);
118 pbc =
new ProductByContractor();
119 pbc.setContract(getDao().find(Contract.class, providerCode));
120 pbc.setContractorCode(codeByProvider);
121 pbc.setDescription(v.getField(
"Description"));
122 pbc.setPrice(v.getDoubleField(
"Cost"));
123 pbc.setTax(v.getDoubleField(
"Tax"));
124 pbc.setProduct((Product) product);
125 ((Product) product).getProductByContractors().add(pbc);
128 ((Product) product).setDescription(v.getField(
"Description"));
129 ((Product) product).setKeywords(v.getField(
"Keywords"));
130 ((Product) product).setPrice(v.getDoubleField(
"Price"));
131 ((Product) product).setTax(v.getDoubleField(
"Tax"));
132 ((Product) product).setPublishable(v.getBooleanField(
"Publishable"));
133 ((Product) product).setPromoted(v.getBooleanField(
"Promoted"));
134 ((Product) product).setService(v.getBooleanField(
"Service"));
135 ((Product) product).setDelivery(v.getBooleanField(
"Delivery"));
136 ((Product) product).setConcept(v.getBooleanField(
"Concept"));
137 ((Product) product).setAttachments(v.getBooleanField(
"Attachments"));
138 product = (IProduct) getDao().saveObject((Product) product);
139 String productPath = FinancialsPU.getObjectPath((Product) product);
140 String web = v.getField(
"WebDescription");
141 DescribeItUtil.addDescription(productPath,
"Default", web, (Contact) Authentication.getIContact().getContact(),
null);
142 if(groupIt !=
null) {
143 Collection<Grouped> groups = Grouped.groups(productPath, v.getField(
"GroupCat"));
144 boolean done =
false;
145 for(Grouped grouped : groups) {
146 if(grouped.getParent().getId().equals(groupIt.
getId())) {
151 Grouped grouped =
new Grouped();
152 grouped.setParent(groupIt);
153 grouped.setCategory(v.getField(
"GroupCat"));
154 grouped.setPath(productPath);
155 new ContactsPU().saveObject(grouped);
161 private GroupIt createGroups(CSVEntry v) {
162 GroupIt groupIt =
null;
163 if(v.getList().getHeader().hasPrefix(
"Group")) {
164 String category = v.getField(
"GroupCat");
165 if(!Strings.isBlank(category)) {
166 Path path =
new Path(v.getField(
"Group"));
167 if(path.getSize() > 0) {
168 for(
int i = 0; i < path.getSize(); i++) {
169 groupIt = GroupIt.getOrCreate(category, path.getNode(i), groupIt);
179 private Dao getDao() {
181 _dao =
new FinancialsPU();
static String getRealPath(String path)
static String logMsg(String msg)
void upload(final Command command)
static IProduct getProduct(long id)
static final String PRODUCT_IMPORT_FOLDER
static String get(String msg)