Adding a project model with a default "project phase" and and "teamtask" upon new creation


#1

Hello guys I have here I java code, and what I want is to set at least 3 childProject or as the UI calls it projectPhase, and it looks like my code is still not working. I assume below that this java file and method below are the ones that are run/use upon creating a new project.

Java file edited: com.axelor.apps.project.service.ProjectServiceImpl.java

Method Edited: generateProject()

Current Code:

public Project generateProject(Project parentProject, String fullName, User assignedTo, Company company,
Partner clientPartner) {
Project project = new Project();
project.setStatusSelect(ProjectRepository.STATE_NEW);
project.setParentProject(parentProject);
if (parentProject != null) {
parentProject.addChildProjectListItem(project);
project.setProjectTypeSelect(ProjectRepository.TYPE_PHASE);
} else {
project.setProjectTypeSelect(ProjectRepository.TYPE_PROJECT);
}
if (Strings.isNullOrEmpty(fullName)) {
fullName = “project”;
}
project.setName(fullName);
project.setFullName(project.getName());
project.setCompany(company);
project.setClientPartner(clientPartner);
project.setAssignedTo(assignedTo);
project.setProgress(BigDecimal.ZERO);

	Project endedPhase = new Project();
	endedPhase.setStatusSelect(ProjectRepository.STATE_NEW);
	endedPhase.setProjectTypeSelect(ProjectRepository.TYPE_PHASE);
	endedPhase.setParentProject(project);
	endedPhase.setName("endedPhase");
	endedPhase.setFullName(endedPhase.getName());
	endedPhase.setCompany(company);
	endedPhase.setClientPartner(clientPartner);
	endedPhase.setAssignedTo(assignedTo);
	endedPhase.setProgress(BigDecimal.ZERO);
	project.addChildProjectListItem(endedPhase);

	Project fisnishedPhase = new Project("fisnishedPhase", null);
	fisnishedPhase.setStatusSelect(ProjectRepository.STATE_NEW);
	fisnishedPhase.setProjectTypeSelect(ProjectRepository.TYPE_PHASE);
	fisnishedPhase.setParentProject(project);
	fisnishedPhase.setName("endedPhase");
	fisnishedPhase.setFullName(endedPhase.getName());
	fisnishedPhase.setCompany(company);
	fisnishedPhase.setClientPartner(clientPartner);
	fisnishedPhase.setAssignedTo(assignedTo);
	fisnishedPhase.setProgress(BigDecimal.ZERO);
	project.addChildProjectListItem(fisnishedPhase);

	Project newContractPhase = new Project("newContractPhase", null);
	newContractPhase.setStatusSelect(ProjectRepository.STATE_NEW);
	newContractPhase.setProjectTypeSelect(ProjectRepository.TYPE_PHASE);
	newContractPhase.setParentProject(project);
	newContractPhase.setName("endedPhase");
	newContractPhase.setFullName(endedPhase.getName());
	newContractPhase.setCompany(company);
	newContractPhase.setClientPartner(clientPartner);
	newContractPhase.setAssignedTo(assignedTo);
	newContractPhase.setProgress(BigDecimal.ZERO);
	project.addChildProjectListItem(fisnishedPhase);

	System.out.println(newContractPhase.getFullName() + "test");

	return project;
}

So if you notice before returning the project I first created three child projects for the new projects instance to be created, I wonder If I am editing the correct file or method?

what do you guys think? thanks for the answers :smiley:


#2

Hi,

You can maybe define your self controller and service and call it using an action-method specified inside the “onSave” (eg: onSave=“create_subtasks,save”).

It’s better than updating the source code to avoid any upgrade troubles
Define your self module:

  • create a service and its impl
  • setup the Guice Injector
  • create a controller that use the injected service
  • create a view with the original project view as template
  • create the action-method calling your controller method
  • add this action to the onSave event of the form followed by the save call.

Regards


#3

Good day sir,

I thank you for always pointing me in the right direction.

I did what you suggested today but still I could not prepolulate/save 3 childProjects on a parentProject that is about to be created.

would you be so kind to look at my code sir of what could I have missed?

here is the controller I created:

~regards


#4

Hi,

From my point of view and my knowlegde on the ADK/ABS you can’t update an object from the ActionRequest but from the ActionResponse.

I think that the better way for you is to call your controller after the “save” action. In that way you could retreive the project from the repository and update / save it.

  1. use the onSave attribute on the form as "onSave=“save,controller_call_action”
  2. inside you controller retreive the project from the repository using the object id inside the ActionRequest
  3. don’t forget to use “repo.save(object)” for your taks first and your projet too.
  4. be care to not create subtask if you already done it (the save can be done as new ou updated project). So just check before adding subtask that the project not already has ones.

Maybe you could also use the “ActionResponse.setAttr” to fill your tasks.

Another way is just to create an abstract repository (look at here: https://docs.axelor.com/adk/5.0/dev-guide/models/repositories.html)

Regards


Can a 'template' be created from a Project?
#5

Good day sir i just want to tell you that I finally did it and I could not done it without your guidance

you even warn me of a potential bug.

many thanks to you sir.


#6

It’s my pleasure :slight_smile:


#7

Good day sir,

Im afraid im gonna ask you some more about my new task that is releated to this field.

So now the client wants me to import their project into axelor but before actually writing their projects into the database I need to modify it first and add some default childProject and tasks just like what I did on axelor UI this time I have to do it while importing projects.

So I refered on the docs: https://docs.axelor.com/adk/5.0/dev-guide/data-import/scripting.html

and add a call method before saving the object into database.

my binding files looks like this:

input file=“ProjectTest.csv” type=“com.axelor.apps.project.db.Project” call=“com.axelor.apps.project.web.MaverickImportController:generateMaverickProject”/>

and my java code:
public class MaverickImportController extends JpaSupport {

@Inject
private ProjectService service;

@Inject
private TeamTaskService teamTaskService;

public Project generateMaverickProject(Object bean, Map<String, Object> m) {

	Project main = (Project) bean;

	ArrayList<Project> p = new ArrayList<>();
	ArrayList<TeamTask> t = new ArrayList<>();

	Project Ended = service.generateProject(main, "Ended " + main.getFullName(), main.getAssignedTo(),
			main.getCompany(), main.getClientPartner());
	Project Finished = service.generateProject(main, "Finished " + main.getFullName(), main.getAssignedTo(),
			main.getCompany(), main.getClientPartner());
	Project NewContract = service.generateProject(main, "New Contract " + main.getFullName(), main.getAssignedTo(),
			main.getCompany(), main.getClientPartner());
	Project PreSales = service.generateProject(main, "Pre-sales " + main.getFullName(), main.getAssignedTo(),
			main.getCompany(), main.getClientPartner());
	Project production = service.generateProject(main, "Production " + main.getFullName(), main.getAssignedTo(),
			main.getCompany(), main.getClientPartner());
	Project Warranty = service.generateProject(main, "Warranty " + main.getFullName(), main.getAssignedTo(),
			main.getCompany(), main.getClientPartner());

	TeamTask design = teamTaskService.create("Design " + production.getFullName(), production,
			production.getAssignedTo());
	TeamTask prewire = teamTaskService.create("Prewire " + production.getFullName(), production,
			production.getAssignedTo());
	TeamTask t1t2 = teamTaskService.create("T1/T2 " + production.getFullName(), production,
			production.getAssignedTo());
	TeamTask finish = teamTaskService.create("Finish " + production.getFullName(), production,
			production.getAssignedTo());
	TeamTask programming = teamTaskService.create("Programming " + production.getFullName(), production,
			production.getAssignedTo());
	TeamTask other = teamTaskService.create("Other " + production.getFullName(), production,
			production.getAssignedTo());
	TeamTask warranty = teamTaskService.create("Warranty " + production.getFullName(), production,
			production.getAssignedTo());
	TeamTask service = teamTaskService.create("Service " + production.getFullName(), production,
			production.getAssignedTo());

	// t.add(design);
	// t.add(prewire);Mapper.toBean(Project.class, m);
	// t.add(t1t2);
	// t.add(finish);
	// t.add(programming);
	// t.add(other);
	// t.add(warranty);
	// t.add(services);

	p.add(Ended);
	p.add(Finished);
	p.add(NewContract);
	p.add(PreSales);
	p.add(production);
	p.add(Warranty);

	m.put("childProjectList", p);

	return main;
}

}

so I wanted those childProjects and tasks added to the project that is being imported before persisting to the database.

the error im having right now is “java.lang.NullPointerException: null”


#8

Hi,

I’m not a specialist of the project module but I think your problem comes from m.put(“childProjectList”, p); Maybe you could try main.addChildProjectListItem(Ended) instead.

Regards
: