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