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.vacancy.VacancyDaoLocal;
16  import de.tivsource.page.entity.enumeration.Language;
17  import de.tivsource.page.entity.vacancy.Vacancy;
18  
19  public class BackupVacancy {
20  
21      /**
22       * Statischer Logger der Klasse.
23       */
24      private static final Logger LOGGER = LogManager.getLogger(BackupVacancy.class);
25  
26  	private static VacancyDaoLocal vacancyDaoLocal;
27  
28  	private static final int max = 1500;
29  
30  	private List<File> backupFiles = new ArrayList<File>();
31  
32  	public static void setVacancyDaoLocal(VacancyDaoLocal vacancyDaoLocal) {
33          BackupVacancy.vacancyDaoLocal = vacancyDaoLocal;
34      }
35  
36      public List<File> getBackupFiles() throws IOException {
37  	    LOGGER.info("getBackupFiles() aufgerufen.");
38  
39  		// Datei Kram
40  		File file = new File("/tmp/vacancy.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|beginning|workingTime|location|picture|pictureOnPage|");
46  
47      	Iterator<Vacancy> vacancyIterator = vacancyDaoLocal.findAll(0, max).iterator();
48      	while(vacancyIterator.hasNext()) {
49      	    Vacancy next = vacancyIterator.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(Vacancy 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|beginning|workingTime|location|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("vacancy_" + 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("vacancy_" + 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(simpleDateFormat.format(next.getBeginning()));
146         nextString.append("|");
147 
148         nextString.append(next.getWorkingTime());
149         nextString.append("|");
150 
151         nextString.append(next.getLocation().getUuid());
152         nextString.append("|");
153 
154         nextString.append(next.getPicture().getUuid());
155         nextString.append("|");
156 
157         nextString.append(next.getPictureOnPage().toString());
158         nextString.append("|");
159 
160 		return nextString.toString();
161 	}
162 
163 	private void generateContentFile(Vacancy vacancy, Language language) throws IOException {
164 		File file = new File("/tmp/vacancy_" + vacancy.getUuid() + "_" + language.toString() +".txt");
165 		FileWriter fileWriter = new FileWriter(file);
166     	BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
167     	bufferedWriter.write(vacancy.getContent(language));
168     	bufferedWriter.close();
169     	fileWriter.close();
170     	backupFiles.add(file);
171 	}
172 
173 }// Ende class