PageRenderTime 104ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/huataisi/oa/crm/UpLoadFileWindow.java

https://bitbucket.org/htsi/huataioa
Java | 343 lines | 272 code | 32 blank | 39 comment | 66 complexity | 38fd812a5dd747e6efcf3acc0d88c2ae MD5 | raw file
  1. package com.huataisi.oa.crm;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.OutputStream;
  5. import java.math.BigDecimal;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import org.apache.poi.hssf.usermodel.HSSFCell;
  10. import org.apache.poi.hssf.usermodel.HSSFRow;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import com.huataisi.oa.domain.User;
  17. import com.huataisi.oa.security.UserUtil;
  18. import com.huataisi.oa.util.ContainerUtils;
  19. import com.vaadin.addon.jpacontainer.JPAContainer;
  20. import com.vaadin.terminal.ExternalResource;
  21. import com.vaadin.ui.FormLayout;
  22. import com.vaadin.ui.HorizontalLayout;
  23. import com.vaadin.ui.Label;
  24. import com.vaadin.ui.Link;
  25. import com.vaadin.ui.Panel;
  26. import com.vaadin.ui.ProgressIndicator;
  27. import com.vaadin.ui.Upload;
  28. import com.vaadin.ui.Upload.FailedEvent;
  29. import com.vaadin.ui.Upload.Receiver;
  30. import com.vaadin.ui.Upload.StartedEvent;
  31. import com.vaadin.ui.Upload.SucceededEvent;
  32. import com.vaadin.ui.Window;
  33. @SuppressWarnings("serial")
  34. public class UpLoadFileWindow extends Window{
  35. Logger logger = LoggerFactory.getLogger(UpLoadFileWindow.class);
  36. static ByteArrayOutputStream outStream = null;
  37. public UpLoadFileWindow(){
  38. setHeight("400px");
  39. setWidth("400px");
  40. this.setModal(true);
  41. this.center();
  42. Upload upload = new Upload();
  43. upload.setButtonCaption("开始上传");
  44. //TODO 导入
  45. //this.addComponent(upload);
  46. }
  47. private Label state = new Label();
  48. private Label fileName = new Label();
  49. private Label textualProgress = new Label();
  50. private ProgressIndicator pi = new ProgressIndicator();
  51. private Worker1 worker1;
  52. @SuppressWarnings("serial")
  53. private Upload upload = new Upload(null, new Receiver() {
  54. @Override
  55. public OutputStream receiveUpload(String filename, String mimeType) {
  56. outStream = new ByteArrayOutputStream();
  57. return outStream;
  58. }
  59. });
  60. @SuppressWarnings("serial")
  61. public UpLoadFileWindow(String caption) {
  62. super(caption);
  63. setHeight("350px");
  64. setWidth("400px");
  65. this.setModal(true);
  66. this.center();
  67. //TODO 导入
  68. // Link link = new Link("模板下载", new ExternalResource("VAADIN/customer.xls"));
  69. // addComponent(link);
  70. // upload.setImmediate(true);
  71. // upload.setButtonCaption("选择需要上传的文件");
  72. // addComponent(upload);
  73. Panel p = new Panel("状态");
  74. // p.setHeight("250px");
  75. p.setWidth("400");
  76. p.setSizeUndefined();
  77. FormLayout l = new FormLayout();
  78. l.setSizeFull();
  79. l.setMargin(true);
  80. p.setContent(l);
  81. HorizontalLayout stateLayout = new HorizontalLayout();
  82. stateLayout.setSpacing(true);
  83. stateLayout.addComponent(state);
  84. stateLayout.setCaption("当前状态");
  85. state.setValue("空闲");
  86. l.addComponent(stateLayout);
  87. fileName.setCaption("文件名");
  88. l.addComponent(fileName);
  89. pi.setCaption("当前进度");
  90. pi.setVisible(false);
  91. l.addComponent(pi);
  92. textualProgress.setVisible(false);
  93. l.addComponent(textualProgress);
  94. //TODO 导入
  95. //addComponent(p);
  96. upload.addListener(new Upload.StartedListener() {
  97. public void uploadStarted(StartedEvent event) {
  98. // this method gets called immediatedly after upload is
  99. // started
  100. pi.setValue(0f);
  101. pi.setVisible(true);
  102. pi.setPollingInterval(500); // hit server frequantly to get
  103. textualProgress.setVisible(true);
  104. // updates to client
  105. state.setValue("上传中,请稍候...");
  106. fileName.setValue(event.getFilename());
  107. }
  108. });
  109. upload.addListener(new Upload.ProgressListener() {
  110. public void updateProgress(long readBytes, long contentLength) {
  111. // this method gets called several times during the update
  112. pi.setValue(new Float(readBytes / (float) contentLength));
  113. textualProgress.setValue("上传进度 " + readBytes
  114. + "bytes / " + contentLength+"bytes");
  115. // try {
  116. // Thread.sleep(100);
  117. // } catch (InterruptedException e) {
  118. // // TODO Auto-generated catch block
  119. // e.printStackTrace();
  120. // }
  121. }
  122. });
  123. upload.addListener(new Upload.SucceededListener() {
  124. public void uploadSucceeded(SucceededEvent event) {
  125. state.setValue("上传完成,开始导入");
  126. worker1 = new Worker1();
  127. worker1.start();
  128. }
  129. });
  130. upload.addListener(new Upload.FailedListener() {
  131. private static final long serialVersionUID = 1L;
  132. public void uploadFailed(FailedEvent event) {
  133. state.setValue("上传失败 ");
  134. }
  135. });
  136. }
  137. public void prosessed() {
  138. int i = worker1.getCurrent();
  139. int count = worker1.getCount();
  140. if (i == count) {
  141. pi.setEnabled(false);
  142. state.setValue("导入完成");
  143. pi.setValue(1f);
  144. JPAContainer<Customer> container = ContainerUtils.createJPAContainer(Customer.class);
  145. container.refresh();
  146. } else {
  147. pi.setValue((float) i / count);
  148. }
  149. textualProgress.setValue("导入进度 " + i + "条 / " + count+"条");
  150. }
  151. public class Worker1 extends Thread {
  152. int i = 1;
  153. private int countRows ;
  154. @Override
  155. @Transactional
  156. public void run() {
  157. Customer cus = null;
  158. try{
  159. ByteArrayInputStream bin = new ByteArrayInputStream(outStream.toByteArray());
  160. HSSFWorkbook wb = new HSSFWorkbook(bin);
  161. HSSFSheet st = wb.getSheetAt(0);
  162. //从wb中取数据
  163. countRows = st.getPhysicalNumberOfRows()-1;
  164. User nu = new User();
  165. for (; i <= countRows; i++) {
  166. cus = new Customer();
  167. HSSFRow row = st.getRow(i);
  168. if(row != null && row.getCell(0) != null && !"".equals(row.getCell(0).getStringCellValue())){
  169. cus.setName(getStringValue(row.getCell(0)));
  170. //
  171. if(row.getCell(2) != null){
  172. String strIstry = getStringValue(row.getCell(2));
  173. Industry istry = new Industry().findIndustryByName(strIstry);
  174. if (istry == null && strIstry != null && !"".equals(strIstry)) {
  175. istry = new Industry(strIstry);
  176. istry.persist();
  177. }
  178. cus.setIndustry(istry);
  179. }
  180. if(row.getCell(11) != null){
  181. cus.setCategory(CusCategory.valueOf(getStringValue(row.getCell(11))));
  182. }
  183. //地区
  184. if(row.getCell(12) != null){
  185. String strArea = getStringValue(row.getCell(12));
  186. Area area = new Area().getAreaByName(strArea);
  187. if (area == null && strArea != null && !"".equals(strArea)) {
  188. area = new Area(strArea, null);
  189. area.persist();
  190. }
  191. cus.setArea(area);
  192. }
  193. //客户类型
  194. if(row.getCell(13) != null){
  195. String strCusType = getStringValue(row.getCell(13));
  196. CusType cusType = new CusType().findCusTypeByName(strCusType);
  197. if (cusType == null && strCusType != null && !"".equals(strCusType)) {
  198. cusType = new CusType(strCusType);
  199. cusType.persist();
  200. }
  201. cus.setCustomerType(cusType);
  202. }
  203. nu = UserUtil.getCurrentUser();
  204. if(row.getCell(1) != null && !"".equals(row.getCell(1).getStringCellValue())){
  205. cus.setScale(Integer.valueOf(row.getCell(1).getStringCellValue()));
  206. }
  207. cus.setPhone(getStringValue(row.getCell(3)));
  208. if(row.getCell(4) != null){
  209. cus.setUseSoft(row.getCell(4).getBooleanCellValue());
  210. }
  211. cus.setUrl(getStringValue(row.getCell(5)));
  212. cus.setAddress(getStringValue(row.getCell(6)));
  213. cus.setRemark(getStringValue(row.getCell(7)));
  214. if(row.getCell(8) != null){
  215. cus.setNextContactDate(getDateValue(row.getCell(8)));
  216. }
  217. cus.setCreateBy(nu);
  218. Date nextLDate = null;
  219. if(row.getCell(10) != null){
  220. cus.setLastTrackDate(getDateValue(row.getCell(10)));
  221. }
  222. if(row.getCell(14) != null || row.getCell(15) != null || row.getCell(16) != null){
  223. Track track = new Track();
  224. if(row.getCell(14) != null){
  225. track.setTtime(getDateValue(row.getCell(14)));
  226. }
  227. track.setRemark(getStringValue(row.getCell(15)));
  228. track.setNextPlan(getStringValue(row.getCell(16)));
  229. cus.getTracks().add(track);
  230. }
  231. if(row.getCell(17) != null){
  232. Contact contact = new Contact(getStringValue(row.getCell(17)));
  233. contact.setEmail(getStringValue(row.getCell(18)));
  234. contact.setQq(getStringValue(row.getCell(19)));
  235. contact.setPhone(getStringValue(row.getCell(20)));
  236. contact.setTel(getStringValue(row.getCell(21)));
  237. cus.getContacts().add(contact);
  238. }
  239. // CusTypeHistory cusTypeHistory = new CusTypeHistory(row.getCell(18).getStringCellValue(),
  240. // row.getCell(19).getDateCellValue());
  241. // cus.getTypeTrack().add(cusTypeHistory);
  242. cus.persist();
  243. Thread.sleep(10);
  244. }
  245. // All modifications to Vaadin components should be synchronized
  246. // over application instance. For normal requests this is done
  247. // by the servlet. Here we are changing the application state
  248. // via a separate thread.
  249. //TODO 导入
  250. // synchronized (getApplication()) {
  251. // prosessed();
  252. // }
  253. }
  254. }catch (Exception e) {
  255. if(e.getMessage()!= null && e.getMessage().indexOf("Duplicate") != -1){
  256. state.setValue("记录重复:"+cus.getName());
  257. }else{
  258. state.setValue("导入出错");
  259. }
  260. pi.setEnabled(true);
  261. e.printStackTrace();
  262. throw new RuntimeException("导入出错");
  263. }
  264. }
  265. public int getCurrent() {
  266. return i;
  267. }
  268. public int getCount(){
  269. return countRows;
  270. }
  271. private String getStringValue(HSSFCell cell){
  272. if(cell != null){
  273. int type = cell.getCellType();
  274. if(type == HSSFCell.CELL_TYPE_STRING){
  275. return cell.getStringCellValue();
  276. }else if(type == HSSFCell.CELL_TYPE_NUMERIC){
  277. BigDecimal big = new BigDecimal(cell.getNumericCellValue());
  278. return big.longValue()+"";
  279. }else{
  280. // return cell.get
  281. }
  282. }
  283. return "";
  284. }
  285. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  286. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
  287. private Date getDateValue(HSSFCell cell){
  288. if(cell != null){
  289. int type = cell.getCellType();
  290. if(type == HSSFCell.CELL_TYPE_STRING){
  291. String s = cell.getStringCellValue();
  292. try{
  293. return sdf.parse(s);
  294. }catch(Exception e){
  295. try {
  296. return sdf2.parse(s);
  297. } catch (ParseException e1) {
  298. // TODO Auto-generated catch block
  299. e1.printStackTrace();
  300. }
  301. }
  302. }else if(type == HSSFCell.CELL_TYPE_NUMERIC){
  303. return cell.getDateCellValue();
  304. }else{
  305. // return cell.get
  306. }
  307. }
  308. return null;
  309. }
  310. }
  311. }