}
@RequiresPermissions("objects:save")
public static void saveObjects(File folder) throws DaoLayerException {
if (objectList.isEmpty()) {
throw new DaoLayerException("There are no object to persist, " + folder.getAbsolutePath()
+ " will not be altered.");
}
if (!folder.isDirectory()) {
throw new DaoLayerException(folder.getAbsoluteFile() + " is not a valid object folder. Skipped");
}
try {
XStream xstream = FreedomXStream.getXstream();
deleteObjectFiles(folder);
// Create file
StringBuilder summary = new StringBuilder();
//print an header for the index.txt file
summary.append("#Filename \t\t #EnvObjectName \t\t\t #EnvObjectType \t\t\t #Protocol \t\t\t #Address")
.append("\n");
for (EnvObjectLogic envObject : objectList.values()) {
String uuid = envObject.getPojo().getUUID();
if ((uuid == null) || uuid.isEmpty()) {
envObject.getPojo().setUUID(UUID.randomUUID().toString());
}
if ((envObject.getPojo().getEnvironmentID() == null)
|| envObject.getPojo().getEnvironmentID().isEmpty()) {
envObject.getPojo()
.setEnvironmentID(EnvironmentPersistence.getEnvironments().get(0).getPojo().getUUID());
}
String fileName = envObject.getPojo().getUUID() + ".xobj";
FileWriter fstream = new FileWriter(folder + "/" + fileName);
BufferedWriter out = new BufferedWriter(fstream);
out.write(xstream.toXML(envObject.getPojo())); //persist only the data not the logic
summary.append(fileName).append("\t").append(envObject.getPojo().getName()).append("\t")
.append(envObject.getPojo().getType()).append("\t")
.append(envObject.getPojo().getProtocol()).append("\t")
.append(envObject.getPojo().getPhisicalAddress()).append("\n");
//Close the output stream
out.close();
fstream.close();
}
//writing a summary .txt file with the list of commands in this folder
FileWriter fstream = new FileWriter(folder + "/index.txt");
BufferedWriter indexfile = new BufferedWriter(fstream);
indexfile.write(summary.toString());
//Close the output stream
indexfile.close();
} catch (IOException ex) {
throw new DaoLayerException(ex);
}
}