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   
10  import org.apache.logging.log4j.LogManager;
11  import org.apache.logging.log4j.Logger;
12  
13  import de.tivsource.page.dao.companion.CompanionDaoLocal;
14  import de.tivsource.page.entity.companion.Companion;
15  
16  public class BackupCompanion {
17  
18      /**
19       * Statischer Logger der Klasse.
20       */
21      private static final Logger LOGGER = LogManager.getLogger(BackupCompanion.class);
22  
23  	private static CompanionDaoLocal companionDaoLocal;
24  
25  	public static void setCompanionDaoLocal(CompanionDaoLocal companionDaoLocal) {
26          BackupCompanion.companionDaoLocal = companionDaoLocal;
27      }
28  
29      public static File getBackupFile() throws IOException {
30          LOGGER.info("getBackupFile() aufgerufen.");
31  		// Datei Kram
32  		File backupFile = new File("/tmp/companion.csv");
33      	FileWriter backupFileWriter = new FileWriter(backupFile);
34      	BufferedWriter backupFileWriterOut = new BufferedWriter(backupFileWriter);
35  
36      	// Format Definition 
37      	backupFileWriterOut.write("[Format Definition] => uuid|name|appendix|street|zip|city|country|mobile|telephone|fax|email|homepage|group|visible|created|modified|modifiedBy|modifiedAddress|");
38  
39      	Iterator<Companion> typeIterator = companionDaoLocal.findAll(0, companionDaoLocal.countAll()).iterator();
40      	while(typeIterator.hasNext()) {
41      	    Companion next = typeIterator.next();
42      		backupFileWriterOut.write("\n");
43      		backupFileWriterOut.write(convertToCsvLine(next));
44      	}
45      	backupFileWriterOut.close();
46      	backupFileWriter.close();
47  
48      	return backupFile;
49  	}// Ende getBackupFiles()
50  
51  	private static String convertToCsvLine(Companion next) {
52  
53  	    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
54  
55  	    // uuid|name|appendix|street|zip|city|country|mobile|telephone|fax|email|homepage|
56  	    // group|visible|created|modified|modifiedBy|modifiedAddress|
57  	    
58  	    
59  		StringBuffer nextString = new StringBuffer();
60  
61  		nextString.append(next.getUuid());
62  		nextString.append("|");
63  
64          nextString.append(next.getName());
65          nextString.append("|");
66  
67          nextString.append(next.getAppendix());
68          nextString.append("|");
69  
70          nextString.append(next.getAddress().getStreet());
71          nextString.append("|");
72  
73          nextString.append(next.getAddress().getZip());
74          nextString.append("|");
75  
76          nextString.append(next.getAddress().getCity());
77          nextString.append("|");
78  
79          nextString.append(next.getAddress().getCountry());
80          nextString.append("|");
81  
82          nextString.append(next.getContactDetails().getMobile());
83          nextString.append("|");
84  
85          nextString.append(next.getContactDetails().getTelephone());
86          nextString.append("|");
87  
88          nextString.append(next.getContactDetails().getFax());
89          nextString.append("|");
90  
91          nextString.append(next.getContactDetails().getEmail());
92          nextString.append("|");
93  
94          nextString.append(next.getContactDetails().getHomepage());
95          nextString.append("|");
96  
97          nextString.append(next.getGroup().getUuid());
98          nextString.append("|");
99  
100         nextString.append(next.getVisible().toString());
101         nextString.append("|");
102 
103         nextString.append(simpleDateFormat.format(next.getCreated()));
104         nextString.append("|");
105 
106         nextString.append(simpleDateFormat.format(next.getModified()));
107         nextString.append("|");
108 
109         nextString.append(next.getModifiedBy());
110         nextString.append("|");
111 
112         nextString.append(next.getModifiedAddress());
113         nextString.append("|");
114 
115 
116 		return nextString.toString();
117 	}
118 
119 }// Ende class