Imports orders and calendars together

FEA: ItEr77S05BasicProjectImport
This commit is contained in:
Alba Carro 2012-10-22 19:19:04 +02:00 committed by Manuel Rego Casasnovas
parent 20481f8558
commit eb696ddc87
5 changed files with 149 additions and 15 deletions

View file

@ -31,6 +31,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.reader.ProjectReader;
import net.sf.mpxj.reader.ProjectReaderUtility;
@ -66,6 +67,8 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional
public class CalendarImporterMPXJ implements ICalendarImporter {
private static ProjectFile projectFile = null;
@Autowired
private IBaseCalendarDAO baseCalendarDAO;
@ -90,7 +93,33 @@ public class CalendarImporterMPXJ implements ICalendarImporter {
ProjectReader reader = ProjectReaderUtility
.getProjectReader(filename);
return MPXJProjectFileConversor.convertCalendars(reader.read(file));
// In case that orders are imported too
projectFile = reader.read(file);
return MPXJProjectFileConversor.convertCalendars(projectFile);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Makes a {@link OrderDTO} from a InputStream.
*
* Uses the ProjectReader of the class. It must be created before.
*
* @param filename
* String with the name of the original file of the InputStream.
* @return OrderDTO with the data that we want to import.
*/
@Override
public OrderDTO getOrderDTO(String filename) {
try {
return MPXJProjectFileConversor.convert(projectFile, filename);
} catch (Exception e) {

View file

@ -66,4 +66,15 @@ List<CalendarDTO> calendarDTOs)
* TaskGroup with the data.
*/
public void storeBaseCalendars(List<BaseCalendar> baseCalendars);
/**
* Makes a {@link OrderDTO} from a InputStream.
*
* Uses the ProjectReader of the class. It must be created before.
*
* @param filename
* String with the name of the original file of the InputStream.
* @return OrderDTO with the data that we want to import.
*/
OrderDTO getOrderDTO(String filename);
}

View file

@ -54,7 +54,7 @@ public interface IOrderImporter {
* ImportData to extract data from.
* @return Order with all the data that we want.
*/
public Order convertImportDataToOrder(OrderDTO project);
public Order convertImportDataToOrder(OrderDTO project, boolean calendar);
/**
* Makes a {@link TaskGroup} from a {@link ImportData}.
@ -63,7 +63,7 @@ public interface IOrderImporter {
* ImportData to extract data from.
* @return TaskGroup with the data that we want.
*/
public TaskGroup createTask(OrderDTO project);
public TaskGroup createTask(OrderDTO project, boolean calendar);
/**
* Saves a {@link Order} and a {@link TaskGroup} which has all the data that

View file

@ -19,6 +19,8 @@
package org.libreplan.importers;
import static org.libreplan.web.I18nHelper._;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
@ -30,11 +32,13 @@ import net.sf.mpxj.reader.ProjectReaderUtility;
import org.apache.commons.lang.Validate;
import org.joda.time.LocalDate;
import org.libreplan.business.calendars.daos.IBaseCalendarDAO;
import org.libreplan.business.calendars.entities.BaseCalendar;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.common.daos.IEntitySequenceDAO;
import org.libreplan.business.common.entities.EntityNameEnum;
import org.libreplan.business.common.exceptions.ValidationException;
import org.libreplan.business.orders.daos.IOrderDAO;
import org.libreplan.business.orders.daos.IOrderElementDAO;
import org.libreplan.business.orders.entities.Order;
@ -73,6 +77,9 @@ import org.springframework.transaction.annotation.Transactional;
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class OrderImporterMPXJ implements IOrderImporter {
@Autowired
private IBaseCalendarDAO baseCalendarDAO;
@Autowired
private IEntitySequenceDAO entitySequenceDAO;
@ -100,6 +107,7 @@ public class OrderImporterMPXJ implements IOrderImporter {
@Autowired
private IScenarioManager scenarioManager;
/**
* Makes a {@link OrderDTO} from a InputStream.
*
@ -119,8 +127,8 @@ public class OrderImporterMPXJ implements IOrderImporter {
ProjectReader reader = ProjectReaderUtility
.getProjectReader(filename);
return MPXJProjectFileConversor
.convert(reader.read(file), filename);
return MPXJProjectFileConversor
.convert(reader.read(file), filename);
} catch (Exception e) {
@ -152,7 +160,8 @@ public class OrderImporterMPXJ implements IOrderImporter {
*/
@Override
@Transactional(readOnly = true)
public Order convertImportDataToOrder(OrderDTO project) {
public Order convertImportDataToOrder(OrderDTO project,
boolean importCalendar) {
String code = getCode(EntityNameEnum.ORDER);
@ -175,7 +184,12 @@ public class OrderImporterMPXJ implements IOrderImporter {
BaseCalendar calendar = configurationDAO.getConfiguration()
.getDefaultCalendar();
((Order) orderElement).setCalendar(calendar);
if (importCalendar & project.calendarName != null) {
((Order) orderElement).setCalendar(findBaseCalendar(
project.calendarName));
} else {
((Order) orderElement).setCalendar(calendar);
}
orderElement.useSchedulingDataFor(orderVersion);
@ -272,7 +286,7 @@ public class OrderImporterMPXJ implements IOrderImporter {
*/
@Override
@Transactional
public TaskGroup createTask(OrderDTO project) {
public TaskGroup createTask(OrderDTO project, boolean importCalendar) {
Order order = project.order;
@ -291,7 +305,7 @@ public class OrderImporterMPXJ implements IOrderImporter {
for (OrderElementDTO importTask : project.tasks) {
taskElements.add(createTask(importTask));
taskElements.add(createTask(importTask, importCalendar));
}
@ -343,7 +357,7 @@ public class OrderImporterMPXJ implements IOrderImporter {
*
* @return TaskElement TaskElement that represent the data.
*/
private TaskElement createTask(OrderElementDTO task) {
private TaskElement createTask(OrderElementDTO task, boolean importCalendar) {
OrderElement orderElement = task.orderElement;
@ -360,6 +374,10 @@ public class OrderImporterMPXJ implements IOrderImporter {
taskElement = taskSource
.createTaskWithoutDatesInitializedAndLinkItToTaskSource();
if (importCalendar && task.calendarName != null) {
taskElement.setCalendar(findBaseCalendar(task.calendarName));
}
setPositionConstraint((Task) taskElement, task);
} else {
@ -374,7 +392,7 @@ public class OrderImporterMPXJ implements IOrderImporter {
for (OrderElementDTO importTask : task.children) {
taskElements.add(createTask(importTask));
taskElements.add(createTask(importTask, importCalendar));
}
@ -615,4 +633,30 @@ public class OrderImporterMPXJ implements IOrderImporter {
}
}
/**
* Private method.
*
* Return the {@link BaseCalendar} with the same name as the string given.
*
* @param name
* String with the name that we want to find.
* @return BaseCalendar Calendar.
*/
private BaseCalendar findBaseCalendar(String name) {
List<BaseCalendar> baseCalendars = baseCalendarDAO.findByName(name);
BaseCalendar calendar = null;
for (BaseCalendar baseCalendar : baseCalendars) {
if (baseCalendar.getName().equals(name)) {
calendar = baseCalendar;
return calendar;
}
}
throw new ValidationException(_("Linked calendar not found"));
}
}

View file

@ -90,7 +90,15 @@ public class ProjectImportController extends GenericForwardComposer {
} else if (importAll.isChecked()) {
messages.showMessage(Level.WARNING, _("This funcionality is not implemented yet"));
try {
importAll(media.getStreamData(), file);
messages.showMessage(Level.INFO, _(file
+ ": Import successfully!"));
} catch (InstanceNotFoundException e) {
messages.showMessage(Level.ERROR, _("Instance not found."));
} catch (ValidationException e) {
messages.showMessage(Level.ERROR, e.getMessage());
}
} else {
messages.showMessage(Level.WARNING,
@ -104,6 +112,48 @@ public class ProjectImportController extends GenericForwardComposer {
}
/**
* Imports calendars, orders, task and dependencies from a InputStream.
*
* @param streamData
* InputStream with the data that is going to be imported.
* @param file
* Name of the file that we want to import.
*/
@Transactional
private void importAll(InputStream streamData, String file)
throws InstanceNotFoundException, ValidationException {
List<CalendarDTO> calendarDTOs = calendarImporterMPXJ.getCalendarDTOs(
streamData, file);
List<BaseCalendar> baseCalendars = calendarImporterMPXJ
.getBaseCalendars(calendarDTOs);
calendarImporterMPXJ.storeBaseCalendars(baseCalendars);
OrderDTO importData = calendarImporterMPXJ.getOrderDTO(file);
Order order = orderImporterMPXJ.convertImportDataToOrder(importData,
true);
TaskGroup taskGroup = orderImporterMPXJ.createTask(importData, true);
List<Dependency> dependencies = orderImporterMPXJ
.createDependencies(importData);
orderImporterMPXJ.storeOrder(order, taskGroup, dependencies);
}
/**
* Imports the calendars from a InputStream.
*
* @param streamData
* InputStream with the data that is going to be imported.
* @param file
* Name of the file that we want to import.
*/
@Transactional
private void importCalendar(InputStream streamData, String file)
throws InstanceNotFoundException, ValidationException {
@ -125,16 +175,16 @@ public class ProjectImportController extends GenericForwardComposer {
* InputStream with the data that is going to be imported.
* @param file
* Name of the file that we want to import.
* @return boolean True if the streamData was imported, false if not.
*/
@Transactional
private void importProject(InputStream streamData, String file) {
OrderDTO importData = orderImporterMPXJ.getImportData(streamData, file);
Order order = orderImporterMPXJ.convertImportDataToOrder(importData);
Order order = orderImporterMPXJ.convertImportDataToOrder(importData,
false);
TaskGroup taskGroup = orderImporterMPXJ.createTask(importData);
TaskGroup taskGroup = orderImporterMPXJ.createTask(importData, false);
List<Dependency> dependencies = orderImporterMPXJ
.createDependencies(importData);