View Javadoc

1   package de.tivsource.page.admin.backup;
2   
3   import java.io.BufferedWriter;
4   import java.io.File;
5   import java.io.FileWriter;
6   import java.io.IOException;
7   import java.text.SimpleDateFormat;
8   import java.util.ArrayList;
9   import java.util.Iterator;
10  import java.util.List;
11  
12  import org.apache.logging.log4j.LogManager;
13  import org.apache.logging.log4j.Logger;
14  
15  import de.tivsource.page.dao.news.NewsDaoLocal;
16  import de.tivsource.page.entity.enumeration.Language;
17  import de.tivsource.page.entity.news.News;
18  
19  public class BackupNews {
20  
21      /**
22       * Statischer Logger der Klasse.
23       */
24      private static final Logger LOGGER = LogManager.getLogger(BackupNews.class);
25  
26  	private static NewsDaoLocal newsDaoLocal;
27  
28  	private List<File> backupFiles = new ArrayList<File>();
29  
30  	public static void setNewsDaoLocal(NewsDaoLocal newsDaoLocal) {
31  		BackupNews.newsDaoLocal = newsDaoLocal;
32  	}
33  
34  	public List<File> getBackupFiles() throws IOException {
35  	    LOGGER.info("getBackupFiles() aufgerufen.");
36  
37  		// Datei Kram
38  		File file = new File("/tmp/news.csv");
39      	FileWriter fileWriter = new FileWriter(file);
40      	BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
41  
42      	// Format Definition 
43      	bufferedWriter.write("[Format Definition] => uuid|uuid(de)|name(de)|description(de)|keywords(de)|content_uuid(de)|content(de)|content_created(de)|content_modified(de)|uuid(en)|name(en)|description(en)|keywords(en)|content_uuid(en)|content(en)|content_created(en)|content_modified(en)|visible|created|modified|modifiedBy|modifiedAddress|picture|releaseDate|pictureOnPage|");
44  
45      	Iterator<News> pageIterator = newsDaoLocal.findAll(0, newsDaoLocal.countAll()).iterator();
46      	while(pageIterator.hasNext()) {
47      		News next = pageIterator.next();
48      		bufferedWriter.write("\n");
49      		bufferedWriter.write(convertToCsvLine(next));
50      		generateContentFile(next, Language.DE);
51      		generateContentFile(next, Language.EN);
52      	}
53  
54      	bufferedWriter.close();
55      	fileWriter.close();
56      	backupFiles.add(file);
57  
58  		return backupFiles;
59  	}
60  
61  	private String convertToCsvLine(News next) {
62  	    LOGGER.info("convertToCsvLine(Page next) aufgerufen.");
63  
64  	    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
65  		// uuid|
66  	    // uuid(de)|name(de)|description(de)|keywords(de)|content_uuid(de)|content(de)|content_created(de)|content_modified(de)|
67  	    // uuid(en)|name(en)|description(en)|keywords(en)|content_uuid(en)|content(en)|content_created(en)|content_modified(en)|
68  	    // visible|created|modified|modifiedBy|modifiedAddress|picture|releaseDate|pictureOnPage|
69  
70  		StringBuffer nextString = new StringBuffer();
71  
72  		nextString.append(next.getUuid());
73  		nextString.append("|");
74  
75          nextString.append(next.getDescriptionObject(Language.DE).getUuid());
76          nextString.append("|");
77  
78  		nextString.append(next.getDescriptionObject(Language.DE).getName());
79  		nextString.append("|");
80  
81  		nextString.append(next.getDescriptionObject(Language.DE).getDescription());
82  		nextString.append("|");
83  
84  		nextString.append(next.getDescriptionObject(Language.DE).getKeywords());
85  		nextString.append("|");
86  
87          nextString.append(next.getContentObject(Language.DE).getUuid());
88          nextString.append("|");
89  
90  		nextString.append("news_" + next.getUuid() + "_DE.txt");
91  		nextString.append("|");
92  
93          nextString.append(simpleDateFormat.format(next.getContentObject(Language.DE).getCreated()));
94          nextString.append("|");
95  
96          nextString.append(simpleDateFormat.format(next.getContentObject(Language.DE).getModified()));
97          nextString.append("|");
98  
99          nextString.append(next.getDescriptionObject(Language.EN).getUuid());
100         nextString.append("|");
101         
102         nextString.append(next.getDescriptionObject(Language.EN).getName());
103         nextString.append("|");
104 
105         nextString.append(next.getDescriptionObject(Language.EN).getDescription());
106         nextString.append("|");
107 
108         nextString.append(next.getDescriptionObject(Language.EN).getKeywords());
109         nextString.append("|");
110 
111         nextString.append(next.getContentObject(Language.EN).getUuid());
112         nextString.append("|");
113 
114         nextString.append("news_" + next.getUuid() + "_EN.txt");
115         nextString.append("|");
116 
117 
118         nextString.append(simpleDateFormat.format(next.getContentObject(Language.EN).getCreated()));
119         nextString.append("|");
120 
121         nextString.append(simpleDateFormat.format(next.getContentObject(Language.EN).getModified()));
122         nextString.append("|");
123 
124 		nextString.append(next.getVisible().toString());
125 		nextString.append("|");
126 		
127 		
128 		nextString.append(simpleDateFormat.format(next.getCreated()));
129 		nextString.append("|");
130 
131 		nextString.append(simpleDateFormat.format(next.getModified()));
132 		nextString.append("|");
133 
134 		nextString.append(next.getModifiedBy());
135 		nextString.append("|");
136 
137 		nextString.append(next.getModifiedAddress());
138 		nextString.append("|");
139 
140         nextString.append(next.getPicture().getUuid());
141         nextString.append("|");
142 
143 		nextString.append(simpleDateFormat.format(next.getReleaseDate()));
144 		nextString.append("|");
145 
146 	    nextString.append(next.getPictureOnPage().toString());
147 	    nextString.append("|");
148 
149 		return nextString.toString();
150 	}
151 
152 	private void generateContentFile(News news, Language language) throws IOException {
153 		File file = new File("/tmp/news_" + news.getUuid() + "_" + language.toString() +".txt");
154 		FileWriter fileWriter = new FileWriter(file);
155     	BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
156     	bufferedWriter.write(news.getContent(language));
157     	bufferedWriter.close();
158     	fileWriter.close();
159     	backupFiles.add(file);
160 	}
161 
162 }// Ende class