1
2
3
4 package de.tivsource.page.restore;
5
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.text.ParseException;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import java.util.Iterator;
14
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17
18 import de.tivsource.page.dao.property.PropertyDaoLocal;
19 import de.tivsource.page.entity.property.Property;
20
21
22
23
24
25 public class RestoreProperty {
26
27
28
29
30 private static final Logger LOGGER = LogManager.getLogger(RestoreProperty.class);
31
32 private PropertyDaoLocal propertyDaoLocal;
33
34 public RestoreProperty(PropertyDaoLocal propertyDaoLocal) {
35 super();
36 this.propertyDaoLocal = propertyDaoLocal;
37 }
38
39 public void generate(InputStream inputStream) {
40 LOGGER.info("generate(InputStream inputStream) aufgerufen.");
41 cleanup();
42 BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
43 try {
44 String line = null;
45 while ((line = in.readLine()) != null) {
46 if (!line.startsWith("[Format Definition]")) {
47 Property property = convert(line);
48 propertyDaoLocal.merge(property);
49 }
50 }
51 } catch (IOException e) {
52 e.printStackTrace();
53 }
54
55 }
56
57 private Property convert(String line) {
58
59 String[] items = line.split("\\|");
60
61
62 Property property = new Property();
63 property.setKey(items[0]);
64 property.setValue(items[1]);
65 property.setModified(convertDateString(items[2]));
66 property.setModifiedBy(items[3]);
67 property.setModifiedAddress(items[4]);
68
69 return property;
70 }
71
72
73
74
75
76
77 private Date convertDateString(String dateString) {
78 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
79 try {
80 return simpleDateFormat.parse(dateString);
81 } catch (ParseException e) {
82 return new Date();
83 }
84 }
85
86 private void cleanup() {
87 if(propertyDaoLocal.countAll() > 0) {
88 Iterator<Property> propertyIterator = propertyDaoLocal.findAll(0, propertyDaoLocal.countAll()).iterator();
89 while(propertyIterator.hasNext()) {
90 Property next = propertyIterator.next();
91 propertyDaoLocal.delete(next);
92 }
93 }
94 }
95
96 }