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.io.UnsupportedEncodingException;
11 import java.security.NoSuchAlgorithmException;
12 import java.text.ParseException;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.Map;
18
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21
22 import de.tivsource.page.dao.gallery.GalleryDaoLocal;
23 import de.tivsource.page.dao.picture.PictureDaoLocal;
24 import de.tivsource.page.entity.enumeration.Language;
25 import de.tivsource.page.entity.gallery.Gallery;
26 import de.tivsource.page.entity.namingitem.Description;
27 import de.tivsource.page.entity.namingitem.NamingItem;
28
29
30
31
32
33 public class RestoreGallery {
34
35
36
37
38 private static final Logger LOGGER = LogManager.getLogger(RestoreGallery.class);
39
40 private GalleryDaoLocal galleryDaoLocal;
41
42 private PictureDaoLocal pictureDaoLocal;
43
44 private Map<String, String> pictures = new HashMap<String, String>();
45
46 public RestoreGallery(GalleryDaoLocal galleryDaoLocal, PictureDaoLocal pictureDaoLocal) {
47 super();
48 this.galleryDaoLocal = galleryDaoLocal;
49 this.pictureDaoLocal = pictureDaoLocal;
50 }
51
52 public void generate(InputStream inputStream) {
53 LOGGER.debug("generate(InputStream inputStream) aufgerufen");
54 cleanup();
55 BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
56 try {
57 String line = null;
58 while ((line = in.readLine()) != null) {
59 if (!line.startsWith("[Format Definition]")) {
60 Gallery gallery = convert(line);
61 galleryDaoLocal.merge(gallery);
62 }
63 }
64 } catch (IOException e) {
65 e.printStackTrace();
66 } catch (NoSuchAlgorithmException e) {
67 e.printStackTrace();
68 }
69 }
70
71 public void generateOverviewPictures() {
72 LOGGER.debug("generateOverviewPictures() aufgerufen");
73 Iterator<String> galleryUuids = pictures.keySet().iterator();
74 while (galleryUuids.hasNext()) {
75 String next = galleryUuids.next();
76 convertPictures(next, pictures.get(next));
77 }
78 }
79
80 private Gallery convert(String line) throws NoSuchAlgorithmException, UnsupportedEncodingException {
81
82
83
84
85
86
87
88
89 String[] items = line.split("\\|");
90
91
92 Gallery gallery = new Gallery();
93
94 gallery.setUuid(items[0]);
95
96 Map<Language, Description> descriptionMap = new HashMap<Language, Description>();
97 descriptionMap.put(Language.DE, createDescription(
98 items[1], items[2], items[3],
99 items[4], Language.DE, gallery));
100 descriptionMap.put(Language.EN, createDescription(
101 items[5], items[6], items[7],
102 items[8], Language.EN, gallery));
103 gallery.setDescriptionMap(descriptionMap);
104
105 gallery.setVisible(items[9].equals("true") ? true : false);
106 gallery.setCreated(convertDateString(items[10]));
107 gallery.setModified(convertDateString(items[11]));
108 gallery.setModifiedBy(items[12]);
109 gallery.setModifiedAddress(items[13]);
110
111 pictures.put(items[0], items[14]);
112
113 gallery.setTechnical(items[15]);
114 gallery.setOrderNumber(Integer.parseInt(items[16]));
115 gallery.setPictureOnPage(items[17].equals("true") ? true : false);
116
117
118
119 return gallery;
120 }
121
122 private void convertPictures(String galleryUuid, String pictureUuid) {
123 LOGGER.debug("convertPictures(String gallery, String picture) aufgerufen");
124 Gallery gallery = galleryDaoLocal.findByUuid(galleryUuid);
125 gallery.setPicture(pictureDaoLocal.findByUuid(pictureUuid));
126 galleryDaoLocal.merge(gallery);
127 }
128
129
130
131
132
133
134 private Date convertDateString(String dateString) {
135 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
136 try {
137 return simpleDateFormat.parse(dateString);
138 } catch (ParseException e) {
139 return new Date();
140 }
141 }
142
143 private Description createDescription(
144 String uuid, String name, String description,
145 String keywords, Language language, NamingItem namingItem) {
146 Description pageDescription = new Description();
147 pageDescription.setUuid(uuid);
148 pageDescription.setName(name);
149 pageDescription.setDescription(description);
150 pageDescription.setKeywords(keywords);
151 pageDescription.setLanguage(language);
152 pageDescription.setNamingItem(namingItem);
153 return pageDescription;
154 }
155
156 private void cleanup() {
157 if(galleryDaoLocal.countAll() > 0) {
158 Iterator<Gallery> galleryIterator = galleryDaoLocal.findAll(0, galleryDaoLocal.countAll()).iterator();
159 while(galleryIterator.hasNext()) {
160 Gallery next = galleryIterator.next();
161 galleryDaoLocal.delete(next);
162 }
163 }
164 }
165
166 }