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.appointment.AppointmentDaoLocal;
16  import de.tivsource.page.entity.appointment.Appointment;
17  import de.tivsource.page.entity.enumeration.Language;
18  
19  public class BackupAppointment {
20  
21      /**
22       * Statischer Logger der Klasse.
23       */
24      private static final Logger LOGGER = LogManager.getLogger(BackupAppointment.class);
25  
26  	private static AppointmentDaoLocal appointmentDaoLocal;
27  
28  	private List<File> backupFiles = new ArrayList<File>();
29  
30  	public static void setAppointmentDaoLocal(AppointmentDaoLocal appointmentDaoLocal) {
31          BackupAppointment.appointmentDaoLocal = appointmentDaoLocal;
32      }
33  
34      public List<File> getBackupFiles() throws IOException {
35  	    LOGGER.info("getBackupFiles() aufgerufen.");
36  
37  		// Datei Kram
38  		File file = new File("/tmp/appointment.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|pictureOnPage|pointInTime|booking|bookingUrl|hasVenue|venue|visibleFrom|");
44  
45      	Iterator<Appointment> appointmentIterator = appointmentDaoLocal.findAll(0, appointmentDaoLocal.countAll()).iterator();
46      	while(appointmentIterator.hasNext()) {
47      	    Appointment next = appointmentIterator.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(Appointment 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|pictureOnPage|pointInTime|booking|bookingUrl|
69  	    // hasVenue|venue|visibleFrom|
70  
71  		StringBuffer nextString = new StringBuffer();
72  
73  		nextString.append(next.getUuid());
74  		nextString.append("|");
75  
76          nextString.append(next.getDescriptionObject(Language.DE).getUuid());
77          nextString.append("|");
78  
79  		nextString.append(next.getDescriptionObject(Language.DE).getName());
80  		nextString.append("|");
81  
82  		nextString.append(next.getDescriptionObject(Language.DE).getDescription());
83  		nextString.append("|");
84  
85  		nextString.append(next.getDescriptionObject(Language.DE).getKeywords());
86  		nextString.append("|");
87  
88          nextString.append(next.getContentObject(Language.DE).getUuid());
89          nextString.append("|");
90  
91  		nextString.append("appointment_" + next.getUuid() + "_DE.txt");
92  		nextString.append("|");
93  
94          nextString.append(simpleDateFormat.format(next.getContentObject(Language.DE).getCreated()));
95          nextString.append("|");
96  
97          nextString.append(simpleDateFormat.format(next.getContentObject(Language.DE).getModified()));
98          nextString.append("|");
99  
100         nextString.append(next.getDescriptionObject(Language.EN).getUuid());
101         nextString.append("|");
102         
103         nextString.append(next.getDescriptionObject(Language.EN).getName());
104         nextString.append("|");
105 
106         nextString.append(next.getDescriptionObject(Language.EN).getDescription());
107         nextString.append("|");
108 
109         nextString.append(next.getDescriptionObject(Language.EN).getKeywords());
110         nextString.append("|");
111 
112         nextString.append(next.getContentObject(Language.EN).getUuid());
113         nextString.append("|");
114 
115         nextString.append("appointment_" + next.getUuid() + "_EN.txt");
116         nextString.append("|");
117 
118 
119         nextString.append(simpleDateFormat.format(next.getContentObject(Language.EN).getCreated()));
120         nextString.append("|");
121 
122         nextString.append(simpleDateFormat.format(next.getContentObject(Language.EN).getModified()));
123         nextString.append("|");
124 
125 		nextString.append(next.getVisible().toString());
126 		nextString.append("|");
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(next.getPictureOnPage().toString());
144         nextString.append("|");
145 
146         nextString.append(simpleDateFormat.format(next.getPointInTime()));
147         nextString.append("|");
148 
149         nextString.append(next.getBooking().toString());
150         nextString.append("|");
151 
152         nextString.append(next.getBookingUrl());
153         nextString.append("|");
154 
155         nextString.append(next.getHasVenue().toString());
156         nextString.append("|");
157 
158         nextString.append(next.getVenue());
159         nextString.append("|");
160 
161         nextString.append(simpleDateFormat.format(next.getVisibleFrom()));
162         nextString.append("|");
163 
164         
165 		return nextString.toString();
166 	}
167 
168 	private void generateContentFile(Appointment appointment, Language language) throws IOException {
169 		File file = new File("/tmp/appointment_" + appointment.getUuid() + "_" + language.toString() +".txt");
170 		FileWriter fileWriter = new FileWriter(file);
171     	BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
172     	bufferedWriter.write(appointment.getContent(language));
173     	bufferedWriter.close();
174     	fileWriter.close();
175     	backupFiles.add(file);
176 	}
177 
178 }// Ende class