* PlanProperties-id of the plan to clone.
* @return True if cloning was successful, false otherwise.
*/
public boolean clonePlan(Integer planPropertiesId, String newOwner) {
boolean success = false;
Plan selectedPlan;
try {
selectedPlan = (Plan) em
.createQuery("select p from Plan p where p.planProperties.id = " + planPropertiesId).getSingleResult();
String binarydataTempPath = OS.getTmpPath() + "cloneplan_" + System.currentTimeMillis() + "/";
File binarydataTempDir = new File(binarydataTempPath);
binarydataTempDir.mkdirs();
try {
String tempFile = binarydataTempPath + "plan.xml";
projectExportAction
.exportComplete(planPropertiesId, new FileOutputStream(tempFile), binarydataTempPath);
List<Plan> plans = projectImporter.importPlans(new FileInputStream(tempFile));
Notification notification = null;
if (newOwner != null) {
User user = em.createQuery("Select u from User u where u.username = :username", User.class)
.setParameter("username", newOwner).getSingleResult();
for (Plan p : plans) {
PlanProperties prop = p.getPlanProperties();
prop.setDescription(newOwner + "'s copy of: " + prop.getDescription()
+ " (originally created by " + prop.getOwner() + ")");
prop.setOwner(newOwner);
// mark this plan as a playground copy
prop.setPlayground(true);
prop.touch();
PrepareChangesForPersist prep = new PrepareChangesForPersist(newOwner);
prep.prepare(prop);
String message = "A copy has been created: <em>"
+ prop.getName()
+ " - "
+ prop.getDescription()
+ "</em>"
+ "<br/>It is marked as playground. If you want to use it for serious planning, please change this in Plan Settings.";
notification = new Notification(UUID.randomUUID().toString(), new Date(), "PLATO", message,
user);
}
}
// store project
storePlans(plans);
success = true;
log.debug("Plan '" + selectedPlan.getPlanProperties().getName() + "' successfully cloned.");
if (notification != null) {
// and store notification as well
em.persist(notification);
}
} catch (Exception e) {
log.error("Could not clone project: '" + selectedPlan.getPlanProperties().getName() + "'.", e);
} finally {
OS.deleteDirectory(binarydataTempDir);
}
return success;