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.administration.RoleDaoLocal;
14  import de.tivsource.page.entity.administration.Role;
15  
16  public class BackupRole {
17  
18      /**
19       * Statischer Logger der Klasse.
20       */
21      private static final Logger LOGGER = LogManager.getLogger(BackupRole.class);
22  
23  	private static final int MAX = 1500;
24  	
25  	private static RoleDaoLocal roleDaoLocal;
26  
27  	public static void setRoleDaoLocal(RoleDaoLocal roleDaoLocal) {
28  		BackupRole.roleDaoLocal = roleDaoLocal;
29  	}
30  
31  	public static File getBackupFile() throws IOException {
32  	    LOGGER.info("getBackupFile() aufgerufen.");
33  
34  		// Datei Kram
35  		File backupFile = new File("/tmp/role.csv");
36      	FileWriter backupFileWriter = new FileWriter(backupFile);
37      	BufferedWriter backupFileWriterOut = new BufferedWriter(backupFileWriter);
38  
39      	// Format Definition 
40      	backupFileWriterOut.write("[Format Definition] => uuid|technical|created|modified|modifiedBy|modifiedAddress|");
41  
42      	Iterator<Role> typeIterator = roleDaoLocal.findAll(0, MAX).iterator();
43      	while(typeIterator.hasNext()) {
44      		Role next = typeIterator.next();
45      		backupFileWriterOut.write("\n");
46      		backupFileWriterOut.write(convertToCsvLine(next));
47      	}
48      	backupFileWriterOut.close();
49      	backupFileWriter.close();
50  
51      	return backupFile;
52  	}// Ende getBackupFiles()
53  
54  	private static String convertToCsvLine(Role next) {
55  
56  		// uuid|technical|created|modified|modifiedBy|modifiedAddress|
57  
58  		StringBuffer nextString = new StringBuffer();
59  
60  		nextString.append(next.getUuid());
61  		nextString.append("|");
62  
63  		nextString.append(next.getTechnical());
64  		nextString.append("|");
65  
66  		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
67  		nextString.append(simpleDateFormat.format(next.getCreated()));
68  		nextString.append("|");
69  
70  		nextString.append(simpleDateFormat.format(next.getModified()));
71  		nextString.append("|");
72  
73  		nextString.append(next.getModifiedBy());
74  		nextString.append("|");
75  
76  		nextString.append(next.getModifiedAddress());
77  		nextString.append("|");
78  
79  		return nextString.toString();
80  	}
81  	
82  	
83  }// Ende class