BrightSide Workbench Full Report + Source Code
ProductImport.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.financials.product;
20 
21 import java.io.BufferedReader;
22 import java.io.File;
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;
53 
58 public class ProductImport {
59 
60  public static final String
61  PRODUCT_IMPORT_FOLDER = "/WEB-INF/product/in",
62  PRODUCT_EXPORT_FOLDER = "/WEB-INF/product/out";
63 
64  public static void uploadFile() {
66  fw.upload(new Command() {
67  @Override
68  public Object execute(Context context) {
69  Clients.showBusy(I_.get("Import CVS"));
71  Clients.clearBusy();
72  return null;
73  }
74  });
75  }
76 
77  public void importFromCSV() {
78  for(File file : getCSVs()) {
79  try(BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
80  processFile(new CSVParser(in).getCSVs());
81  } catch (FileNotFoundException ex) {
82  Logger.getLogger(ProductImport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
83  } catch (IOException ex) {
84  Logger.getLogger(ProductImport.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
85  }
86  file.delete();
87  }
88  }
89 
90  public File[] getCSVs() {
91  File root = new File(ElephantContext.getRealPath(PRODUCT_IMPORT_FOLDER));
92  if(!root.exists()) root.mkdir();
93  return root.listFiles(new FilenameFilter() {
94  @Override
95  public boolean accept(File dir, String name) {
96  return name.endsWith(".csv");
97  }
98  });
99  }
100 
101  private void processFile(CSVList list) {
102  for(CSVEntry v : list) {
103  GroupIt groupIt = createGroups(v);
104  IProduct product = null;
105  String productCode = v.getField("Code");
106  if(!Strings.isBlank(productCode)) {
107  product = ProductFactory.getProduct(productCode);
108  if(product == null) {
109  product = new Product();
110  ((Product) product).setProductCode(productCode);
111  }
112  }
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);
117  if(pbc == null) {
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);
126  }
127  }
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())) {
147  done = true;
148  }
149  }
150  if(!done) {
151  Grouped grouped = new Grouped();
152  grouped.setParent(groupIt);
153  grouped.setCategory(v.getField("GroupCat"));
154  grouped.setPath(productPath);
155  new ContactsPU().saveObject(grouped);
156  }
157  }
158  }
159  }
160 
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);
170  }
171  }
172  }
173  }
174  return groupIt;
175  }
176 
177  private Dao _dao;
178 
179  private Dao getDao() {
180  if(_dao == null) {
181  _dao = new FinancialsPU();
182  }
183  return _dao;
184  }
185 
186 }
void upload(final Command command)
static String get(String msg)
Definition: I_.java:41