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.Iterator;
9   import java.util.SortedSet;
10  
11  import org.apache.logging.log4j.LogManager;
12  import org.apache.logging.log4j.Logger;
13  
14  import de.tivsource.page.dao.location.LocationDaoLocal;
15  import de.tivsource.page.entity.enumeration.Language;
16  import de.tivsource.page.entity.location.Location;
17  import de.tivsource.page.entity.location.OpeningHour;
18  
19  public class BackupLocation {
20  
21      /**
22       * Statischer Logger der Klasse.
23       */
24      private static final Logger LOGGER = LogManager.getLogger(BackupLocation.class);
25  
26  	private static final int MAX = 1500;
27  	
28  	private static LocationDaoLocal locationDaoLocal;
29  
30      public static void setLocationDaoLocal(LocationDaoLocal locationDaoLocal) {
31          BackupLocation.locationDaoLocal = locationDaoLocal;
32      }
33  
34      public static File getBackupFile() throws IOException {
35          LOGGER.info("getBackupFile() aufgerufen.");
36  		// Datei Kram
37  		File backupFile = new File("/tmp/location.csv");
38      	FileWriter backupFileWriter = new FileWriter(backupFile);
39      	BufferedWriter backupFileWriterOut = new BufferedWriter(backupFileWriter);
40  
41      	// Format Definition 
42      	backupFileWriterOut.write("[Format Definition] => uuid|street|zip|city|country|mobile|telephone|fax|email|homepage|uuid(de)|name(de)|description(de)|keywords(de)|uuid(en)|name(en)|description(en)|keywords(en)|openingHours|latitude|longitude|visible|created|modified|modifiedBy|modifiedAddress|events|picture|order|pictureOnPage|inLocationList|");
43  
44      	Iterator<Location> typeIterator = locationDaoLocal.findAll(0, MAX).iterator();
45      	while(typeIterator.hasNext()) {
46      	    Location next = typeIterator.next();
47      		backupFileWriterOut.write("\n");
48      		backupFileWriterOut.write(convertToCsvLine(next));
49      	}
50      	backupFileWriterOut.close();
51      	backupFileWriter.close();
52  
53      	return backupFile;
54  	}// Ende getBackupFiles()
55  
56  	private static String convertToCsvLine(Location next) {
57  
58  	    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
59  
60  		// uuid|street|zip|city|country|mobile|telephone|fax|email|homepage|
61  	    // uuid(de)|name(de)|description(de)|keywords(de)|
62  	    // uuid(en)|name(en)|description(en)|keywords(en)|
63  	    // openingHours|latitude|longitude|visible|created|
64  	    // modified|modifiedBy|modifiedAddress|events|picture|order|pictureOnPage|inLocationList|
65  	    
66  		StringBuffer nextString = new StringBuffer();
67  
68  		
69  		nextString.append(next.getUuid());
70  		nextString.append("|");
71  
72  		
73          nextString.append(next.getAddress().getStreet());
74          nextString.append("|");
75  
76          nextString.append(next.getAddress().getZip());
77          nextString.append("|");
78  
79          nextString.append(next.getAddress().getCity());
80          nextString.append("|");
81  
82          nextString.append(next.getAddress().getCountry());
83          nextString.append("|");
84  
85          
86          nextString.append(next.getContactDetails().getMobile());
87          nextString.append("|");
88  
89          nextString.append(next.getContactDetails().getTelephone());
90          nextString.append("|");
91  
92          nextString.append(next.getContactDetails().getFax());
93          nextString.append("|");
94  
95          nextString.append(next.getContactDetails().getEmail());
96          nextString.append("|");
97  
98          nextString.append(next.getContactDetails().getHomepage());
99          nextString.append("|");
100 
101 
102 
103         nextString.append(next.getDescriptionObject(Language.DE).getUuid());
104         nextString.append("|");
105 
106         nextString.append(next.getDescriptionObject(Language.DE).getName());
107         nextString.append("|");
108 
109         nextString.append(next.getDescriptionObject(Language.DE).getDescription());
110         nextString.append("|");
111 
112         nextString.append(next.getDescriptionObject(Language.DE).getKeywords());
113         nextString.append("|");
114 
115         nextString.append(next.getDescriptionObject(Language.EN).getUuid());
116         nextString.append("|");
117         
118         nextString.append(next.getDescriptionObject(Language.EN).getName());
119         nextString.append("|");
120 
121         nextString.append(next.getDescriptionObject(Language.EN).getDescription());
122         nextString.append("|");
123 
124         nextString.append(next.getDescriptionObject(Language.EN).getKeywords());
125         nextString.append("|");
126 
127         
128         nextString.append(convertOpeningHours(next.getOpeningHours()));
129         nextString.append("|");
130         
131         
132         
133         nextString.append(next.getLatitude());
134         nextString.append("|");
135 
136         nextString.append(next.getLongitude());
137         nextString.append("|");
138         
139         nextString.append(next.getVisible().toString());
140         nextString.append("|");
141         
142         nextString.append(simpleDateFormat.format(next.getCreated()));
143         nextString.append("|");
144 
145         nextString.append(simpleDateFormat.format(next.getModified()));
146         nextString.append("|");
147 
148         nextString.append(next.getModifiedBy());
149         nextString.append("|");
150 
151         nextString.append(next.getModifiedAddress());
152         nextString.append("|");
153 
154         nextString.append(next.getEvent().toString());
155         nextString.append("|");
156 
157         nextString.append(next.getPicture().getUuid());
158         nextString.append("|");
159 
160         nextString.append(next.getOrder().toString());
161         nextString.append("|");
162 
163         nextString.append(next.getPictureOnPage().toString());
164         nextString.append("|");
165 
166         nextString.append(next.getInLocationList().toString());
167         nextString.append("|");
168 
169 		return nextString.toString();
170 	}
171 
172 	private static String convertOpeningHours(SortedSet<OpeningHour> openingHours) {
173 
174 	    LOGGER.info("Anzahl der OpeningHours im SortedSet: " + openingHours.size());
175 	    
176 	    StringBuffer hourString = new StringBuffer();
177 
178 	    Iterator<OpeningHour> iterator = openingHours.iterator();
179 
180 	    while(iterator.hasNext()) {
181 	        OpeningHour next = iterator.next();
182 
183 	        hourString.append(next.getWeekday().toString());
184 	        hourString.append(";");
185 
186             hourString.append(next.getOpen().toString());
187             hourString.append(";");
188 
189             hourString.append(next.getClose().toString());
190             hourString.append("!");
191 	    }
192 
193 	    return hourString.toString();
194 	}
195 }// Ende class