--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.manager;
+
+
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import com.pentila.evalcomp.dao.IAcquitmentDAO;
+import com.pentila.evalcomp.dao.IEvaluationInstanceDAO;
+import com.pentila.evalcomp.dao.IScenarioInstanceAbstractDAO;
+import com.pentila.evalcomp.dao.IScenarioInstanceDAO;
+import com.pentila.evalcomp.domain.EObject;
+import com.pentila.evalcomp.domain.Entity;
+import com.pentila.evalcomp.domain.Event;
+import com.pentila.evalcomp.domain.Group;
+import com.pentila.evalcomp.domain.Message;
+import com.pentila.evalcomp.domain.User;
+import com.pentila.evalcomp.domain.definition.Competence;
+import com.pentila.evalcomp.domain.definition.EntityDefinition;
+import com.pentila.evalcomp.domain.definition.EvaluationDefinition;
+import com.pentila.evalcomp.domain.definition.EvaluationObject;
+import com.pentila.evalcomp.domain.definition.EvaluationSubject;
+import com.pentila.evalcomp.domain.definition.EventActivity;
+import com.pentila.evalcomp.domain.definition.Level;
+import com.pentila.evalcomp.domain.definition.MethodEval;
+import com.pentila.evalcomp.domain.definition.ProcessInstanciationDefinition;
+import com.pentila.evalcomp.domain.definition.ProcessRoleDefinition;
+import com.pentila.evalcomp.domain.definition.Property;
+import com.pentila.evalcomp.domain.definition.Referentiel;
+import com.pentila.evalcomp.domain.definition.ScenarioDefinition;
+import com.pentila.evalcomp.domain.definition.ScenarioDefinitionAbstract;
+import com.pentila.evalcomp.domain.instance.Acquitment;
+import com.pentila.evalcomp.domain.instance.CompetenceInstance;
+import com.pentila.evalcomp.domain.instance.EvaluationInstance;
+import com.pentila.evalcomp.domain.instance.EvaluationSubjectInstance;
+import com.pentila.evalcomp.domain.instance.ScenarioInstance;
+import com.pentila.evalcomp.domain.instance.ScenarioInstanceAbstract;
+import com.pentila.evalcomp.domain.plan.Plan;
+import com.pentila.evalcomp.domain.plan.PlanRef;
+import com.pentila.evalcomp.domain.transit.Activity;
+import com.pentila.evalcomp.domain.transit.ContextRef;
+import com.pentila.evalcomp.domain.transit.Process;
+import com.pentila.evalcomp.domain.transit.Session;
+import com.pentila.evalcomp.domain.transit.TCompetence;
+import com.pentila.evalcomp.domain.transit.TCompetenceState;
+import com.pentila.evalcomp.domain.transit.TCompositeDataMap;
+import com.pentila.evalcomp.domain.transit.TDomain;
+import com.pentila.evalcomp.domain.transit.TEvaluationDefinitionState;
+import com.pentila.evalcomp.domain.transit.TEvaluationSubjectInstance;
+import com.pentila.evalcomp.domain.transit.TListEObject;
+import com.pentila.evalcomp.domain.transit.TPlan;
+import com.pentila.evalcomp.domain.transit.TReferentiel;
+import com.pentila.evalcomp.domain.transit.Todo;
+import com.pentila.evalcomp.manager.interfacedef.IDefinitionBase;
+import com.pentila.evalcomp.manager.interfacedef.IEntityBase;
+import com.pentila.evalcomp.manager.interfacedef.IEvaluationObjectBase;
+import com.pentila.evalcomp.manager.interfacedef.IEvaluationSubjectInstanceBase;
+import com.pentila.evalcomp.manager.interfacedef.IExamenBase;
+import com.pentila.evalcomp.manager.interfacedef.IInstanceBase;
+import com.pentila.evalcomp.manager.interfacedef.IWorkflowBase;
+import com.pentila.evalcomp.utilities.InstanceUtil;
+import com.pentila.evalcomp.utilities.Utils;
+import com.pentila.evalcomp.workflow.BonitaProcessLaunch;
+
+/**
+ * The Class InstanceManager.
+ */
+public class InstanceManager implements IInstanceBase {
+
+ /** The logger. */
+ org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(InstanceManager.class);
+
+ // Internal DAO
+
+ /** The acquitment dao. */
+
+ IAcquitmentDAO acquitmentDAO;
+
+ /** The definition manager. */
+
+ IDefinitionBase definitionManager;
+
+ /** The entity manager. */
+
+ IEntityBase entityManager;
+
+ /** The evaluation instance dao. */
+
+ IEvaluationInstanceDAO evaluationInstanceDAO;
+
+ // Linked Manager
+
+ /** The evaluation subject instance manager. */
+
+ IEvaluationSubjectInstanceBase evaluationSubjectInstanceManager;
+
+
+
+ /** The scenario instance dao. */
+
+ IScenarioInstanceDAO scenarioInstanceDAO;
+
+ /** The scenario instance abstract dao. */
+
+ IScenarioInstanceAbstractDAO scenarioInstanceAbstractDAO;
+
+
+
+ /** The workflow manager. */
+
+ IWorkflowBase workflowManager;
+
+ /** The evaluation object manager. */
+
+ IEvaluationObjectBase evaluationObjectManager;
+
+ /** The SIZ e_ max. */
+ int SIZE_MAX = 1;
+
+ /**
+ * Gets the scenario instance abstract dao.
+ *
+ * @return the scenario instance abstract dao
+ */
+ public IScenarioInstanceAbstractDAO getScenarioInstanceAbstractDAO() {
+ return scenarioInstanceAbstractDAO;
+ }
+
+ /**
+ * Sets the scenario instance abstract dao.
+ *
+ * @param scenarioInstanceAbstractDAO the new scenario instance abstract dao
+ */
+ public void setScenarioInstanceAbstractDAO(
+ IScenarioInstanceAbstractDAO scenarioInstanceAbstractDAO) {
+ this.scenarioInstanceAbstractDAO = scenarioInstanceAbstractDAO;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#addEvaluationInstance
+ * (com.pentila.evalcomp.domain.definition.EvaluationDefinition)
+ */
+ public EvaluationInstance addEvaluationInstance(EvaluationDefinition ed,
+ EntityDefinition EDef) {
+ // TODO Auto-generated method stub
+
+
+
+ EvaluationInstance EI = new EvaluationInstance();
+ EI.setEvaluationDefinition(ed);
+ EI.setEntityDefinition(EDef);
+
+ evaluationInstanceDAO.saveOrUpdate(EI);
+
+ logger.debug("addEvaluationInstance {}", EI.getId() );
+
+ return EI;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * addEvaluationSubjectInstance
+ * (com.pentila.evalcomp.domain.definition.EvaluationSubject,
+ * com.pentila.evalcomp.domain.instance.EvaluationInstance)
+ */
+ public EvaluationSubjectInstance addEvaluationSubjectInstance(
+ EvaluationSubject es, EvaluationInstance ei) {
+ // TODO Auto-generated method stub
+ EvaluationSubjectInstance ESI = new EvaluationSubjectInstance();
+
+ ESI.setName(es.getComputedName());
+ ESI.setEvaluationSubject(es);
+ //ESI.setEvaluationInstance(ei);
+
+ ESI = evaluationSubjectInstanceManager
+ .addEvaluationSubjectInstance(ESI);
+
+ logger.debug("addEvaluationSubjectInstance {}", ESI.getId());
+
+ return ESI;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#addParticipants
+ * (java.lang.Long, java.util.Map)
+ */
+ public boolean addParticipants(Long siid, Map<String, Set<String>> map) {
+
+ ScenarioInstance SI = scenarioInstanceDAO.get(siid);
+
+ Map<String, Set<Entity>> map2 = new HashMap<String, Set<Entity>>();
+
+ for (Entry<String, Set<String>> e : map.entrySet()) {
+ String role = e.getKey();
+ Set<String> suid = e.getValue();
+
+ Set<Entity> su = new HashSet<Entity>();
+
+ for (String uid : suid) {
+ User u = entityManager.getUser(uid);
+ su.add(u);
+ }
+
+ map2.put(role, su);
+ }
+ return addParticipants(SI, map2);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#addParticipants
+ * (com.pentila.evalcomp.domain.instance.ScenarioInstance, java.util.Map)
+ */
+ public boolean addParticipants(ScenarioInstanceAbstract si,
+ Map<String, Set<Entity>> map) {
+
+ for (Entry<String, Set<Entity>> e : map.entrySet()) {
+ String role = e.getKey();
+ Set<Entity> su = e.getValue();
+
+ for (Entity u : su) {
+ String localrole = role + "_" + u.getUid();
+ si.getLocalroles().add(localrole);
+
+ logger.debug("addParticipants {}", localrole);
+
+ }
+
+ }
+ scenarioInstanceAbstractDAO.saveOrUpdate(si);
+ /*
+
+ No Need to flush ???????
+
+ */
+
+ workflowManager.refreshAssignation(si.getProcessInstanceId());
+
+ return true;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#addScenarioInstance
+ * (com.pentila.evalcomp.domain.instance.EvaluationInstance,
+ * com.pentila.evalcomp.domain.definition.ScenarioDefinition,
+ * com.pentila.evalcomp.domain.instance.EvaluationSubjectInstance,
+ * java.util.Set)
+ */
+ public ScenarioInstance addScenarioInstance(EvaluationInstance ei,
+ ScenarioDefinition sdt, EvaluationSubjectInstance esi, Set<User> su) {
+ ScenarioInstance SI = new ScenarioInstance();
+ SI.setEvaluationInstance(ei);
+ SI.setScenarioDefinition(sdt);
+ SI.setEvaluationSubjectInstance(esi);
+
+ ProcessInstanciationDefinition ppp = sdt.getPid();
+ for (ProcessRoleDefinition roleDef : ppp.getProcessRoleDefinitions()) {
+
+
+ if (roleDef.getInitRole()) {
+
+ for (User USU : su) {
+ SI.addLocalRole(roleDef.getName(), USU.getUid());
+ }
+ } else {
+
+ // Just for test !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
+ /*
+ * tmpUS = vED.getUsers(); for (User USU : tmpUS){ String
+ * localrole = roleDef.getName() + "_" + USU.getUid();
+ * System.out.println(localrole);
+ * SI.getLocalroles().add(localrole); }
+ */
+ }
+
+ // }
+
+ }
+
+ scenarioInstanceDAO.saveOrUpdate(SI);
+ return SI;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * buildCompetenceInstances
+ * (com.pentila.evalcomp.domain.definition.EvaluationSubject, java.util.Set,
+ * com.pentila.evalcomp.domain.instance.EvaluationSubjectInstance,
+ * com.pentila.evalcomp.domain.instance.EvaluationInstance,
+ * com.pentila.evalcomp.domain.definition.Level)
+ */
+ public void buildCompetenceInstances(List<EvaluationObject> seo,
+ Set<User> su, EvaluationSubjectInstance esi, Level initLevel) {
+
+
+ Date d = new Date();
+
+ for (EvaluationObject EO : seo) {
+
+ for (Competence C : EO.getAllCompetences()) {
+ for (User UUU : su) {
+ CompetenceInstance CI = new CompetenceInstance();
+ CI.setEvaluationSubjectInstance(esi);
+ //CI.setEvaluationInstance(esi.getEvaluationInstance());
+ CI.setCompetence(C);
+ CI.setLevel(initLevel);
+ CI.setOwner(UUU);
+
+ CI.setLastModified(d);
+
+ evaluationSubjectInstanceManager.addCompetenceInstance(CI);
+ }
+ }
+ }
+
+
+ }
+
+
+ /**
+ * Builds the competence instances.
+ *
+ * @param seo the seo
+ * @param listMotivation the list motivation
+ * @param su the su
+ * @param esi the esi
+ * @param initLevel the init level
+ */
+ public void buildCompetenceInstances(List<EvaluationObject> seo, List<String> listMotivation,
+ Set<User> su, EvaluationSubjectInstance esi, Level initLevel) {
+
+
+ Date d = new Date();
+ int i = 0;
+
+ for (EvaluationObject EO : seo) {
+
+
+ for (Competence C : EO.getAllCompetences()) {
+
+ for (User UUU : su) {
+ CompetenceInstance CI = new CompetenceInstance();
+ CI.setEvaluationSubjectInstance(esi);
+ //CI.setEvaluationInstance(esi.getEvaluationInstance());
+ CI.setCompetence(C);
+ CI.setLevel(initLevel);
+ CI.setOwner(UUU);
+ CI.setCompetenceMotivation(listMotivation.get(i));
+
+ CI.setLastModified(d);
+
+ evaluationSubjectInstanceManager.addCompetenceInstance(CI);
+ }
+ }
+
+ i++;
+ }
+
+
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#buildCompetenceInstances(java.lang.Long, java.util.List)
+ */
+ public void buildCompetenceInstances(Long SIID, List<Long> eoIds) {
+
+ List<EvaluationObject> seo = null;
+
+ ScenarioInstance SI = this.getScenarioInstance(SIID);
+
+ EvaluationDefinition ED = SI.getEvaluationInstance()
+ .getEvaluationDefinition();
+
+ EvaluationSubjectInstance ESI = SI.getEvaluationSubjectInstance();
+
+ Level initLevel = ED.getMethodEval().getInitialLevel();
+
+ if (eoIds != null && !eoIds.isEmpty()) {
+ seo = new ArrayList<EvaluationObject>();
+ for (Long id : eoIds){
+ seo.add(evaluationObjectManager.get(id));
+ }
+
+ //seo = evaluationObjectManager.getEvaluationObjects(eoIds);
+ } else {
+ seo = new ArrayList<EvaluationObject>(ESI.getEvaluationSubject().getEvaluationObjects());
+ }
+ Set<User> su = entityManager.getUsersByUids(SI.getRelatedUsersUid());
+
+ if (su != null && !su.isEmpty() && seo != null && !seo.isEmpty()) {
+ this.buildCompetenceInstances(seo, su, ESI, initLevel);
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * deleteCompetenceInstances(java.util.Set, java.util.Set,
+ * com.pentila.evalcomp.domain.instance.EvaluationSubjectInstance)
+ */
+ public void deleteCompetenceInstances(List<EvaluationObject> seo,
+ Set<User> su, EvaluationSubjectInstance esi) {
+ evaluationSubjectInstanceManager
+ .deleteCompetenceInstances(seo, su, esi);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * deleteEvaluationInstances(java.util.Set)
+ */
+ public void deleteEvaluationInstances(
+ Set<EvaluationInstance> evaluationInstances) {
+ Set<ScenarioInstance> ssi = new HashSet<ScenarioInstance>();
+ for (EvaluationInstance EI : evaluationInstances) {
+ ssi.addAll(EI.getScenarioInstances());
+ }
+ this.deleteScenarioInstances(ssi);
+ }
+
+ /**
+ * Delete scenario instance.
+ *
+ * @param si
+ * the si
+ */
+ public void deleteScenarioInstance(ScenarioInstance si) {
+ if (logger.isDebugEnabled()){
+ logger.debug("deleteScenarioInstance");
+ }
+ // Delete Bonita Process Instance
+ if (si.getProcessInstanceId() != null) {
+
+ workflowManager.removeProcess(si.getProcessInstanceId());
+ if (logger.isDebugEnabled()){
+ logger.debug("delete Bonita workflow with id {} and processInstanceId {}", si.getId(), si.getProcessInstanceId());
+ }
+ }
+ // Remove links from parents objects
+ si.getEvaluationInstance().getScenarioInstances().remove(si);
+
+ evaluationSubjectInstanceManager.deleteESI(si
+ .getEvaluationSubjectInstance());
+
+ scenarioInstanceDAO.delete(si);
+ if (logger.isDebugEnabled()){
+ logger.debug("delete sceanrioinstance id {}", si.getId());
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * deleteScenarioInstances(java.util.Set)
+ */
+ public void deleteScenarioInstances(Set<ScenarioInstance> ssi) {
+ if (logger.isDebugEnabled()){
+ logger.debug("deleteScenarioInstances");
+ }
+ try{
+ for (ScenarioInstance si : ssi) {
+
+ si = scenarioInstanceDAO.get(si.getId());
+
+
+ EvaluationInstance EI = si.getEvaluationInstance();
+ EI.getScenarioInstances().remove(si);
+
+ deleteScenarioInstance(si);
+
+ // Need to relaod it to get an update-to-date Instance
+ //EvaluationInstance EI = evaluationInstanceDAO.get(idEI);
+
+
+
+ if (EI.getScenarioInstances().isEmpty()) {
+
+ if (logger.isDebugEnabled()){
+ logger.debug("EvaluationInstance id {} has not ScenarioInstance left", EI.getId());
+ }
+
+ EI.getEvaluationDefinition().getEvaluationInstances()
+ .remove(EI);
+ EI.setEntityDefinition(null);
+
+ evaluationInstanceDAO.delete(EI);
+
+ if (logger.isDebugEnabled()){
+ logger.debug("Delete EvaluationInstance id {}", EI.getId());
+ }
+
+ }
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * deleteScenarioInstancesForUsers(java.util.Set, java.lang.Long,
+ * java.lang.Long)
+ */
+ public void deleteScenarioInstancesForUsers(Set<String> msuid, Long defId,
+ Long idEDef) {
+
+ Set<ScenarioInstance> ssi = new HashSet<ScenarioInstance>();
+ // Build a set of ScenariuoInstance
+ // but we must fix this
+ for (String login : msuid) {
+ if (logger.isDebugEnabled()){
+ logger.debug("deleteScenarioInstancesForUsers : " + login);
+ }
+ Set<ScenarioInstance> ssitmp = getScenarioInstances(login);
+ for (ScenarioInstance si : ssitmp) {
+ if (si.getEvaluationInstance().getEvaluationDefinition()
+ .getId().equals(defId)) {
+ ssi.add(si);
+ }
+ }
+ }
+
+ if (ssi != null) {
+
+ for (ScenarioInstance si : ssi) {
+
+ // Filter SIs on EvaluationDefintion and EDef appartenance.
+ // Do not delete all SI by only conserned by ED and By EDef.
+
+ EvaluationInstance EI = si.getEvaluationInstance();
+
+ Long idEI = EI.getId();
+ Long idDef = EI.getEvaluationDefinition().getId();
+ Long idEIEDef = null;
+ if (EI.getEntityDefinition() != null) {
+ idEIEDef = EI.getEntityDefinition().getId();
+ }
+
+ // Filter SIs on EvaluationDefintion and EDef appartenance.
+ // Do not delete all SI by only conserned by ED and By EDef.
+ if ((defId != null && !idDef.equals(defId))
+ || (idEDef != null && idEIEDef != null && !idEDef
+ .equals(idEIEDef))) {
+ continue;
+ }
+
+ String initRole = si.getScenarioDefinition().getInitRole();
+
+ // Remove all unwanted user on SI
+ // FIX ME
+ // Here we potentialy remove some user that we don't must delete
+ // on this SI
+ for (String login : msuid) {
+ if (logger.isDebugEnabled()){
+ logger.debug("deleteScenarioInstancesForUsers : " + login + " on SI : " + si.getId());
+ }
+ si.removeLocalRoleForUser(login);
+ }
+ Set<String> suid = si.getUserForRole(initRole, false);
+
+ if (suid.size() == 0) {
+ // Delete SI
+
+ // Long idEI = si.getEvaluationInstance().getId();
+ if (logger.isDebugEnabled()){
+ logger.debug("deleteScenarioInstancesForUsers : Delete SI");
+ }
+ deleteScenarioInstance(si);
+
+ // Need to relaod it to get an update-to-date Instance
+ EvaluationInstance EI2 = evaluationInstanceDAO.get(idEI);
+
+ if (EI2.getScenarioInstances().isEmpty()) {
+ if (logger.isDebugEnabled()){
+ logger.debug("deleteScenarioInstancesForUsers : Delete EI : " + EI2.getId());
+ }
+ EI2.getEvaluationDefinition().getEvaluationInstances()
+ .remove(EI2);
+ evaluationInstanceDAO.delete(EI2);
+ }
+ } else {
+ // update Only with delete User reference in SI
+ // Case where SI is shared by other users (Instance shared
+ // by more than one user of init-role)
+
+ // Here we have a problem
+ // The sub graph ESI + CI + Ressource is not deleted for
+ // each user !!
+ // MUST FIX IT in Shared Instance Mode (not individual
+ // instanciation)
+
+
+
+ List<CompetenceInstance> sci = new ArrayList<CompetenceInstance>();
+ for (CompetenceInstance CI : si
+ .getEvaluationSubjectInstance()
+ .getCompetenceInstances()) {
+
+
+ if (logger.isDebugEnabled()){
+ logger.debug("ECHO1 : " + CI);
+ logger.debug("ECHO2 : " + CI.getOwner());
+ logger.debug("ECHO3" + msuid);
+ }
+
+
+
+ // CI.getOwner() can be null ? YES !
+ if (CI.getOwner() == null || msuid.contains(CI.getOwner().getUid())) {
+ sci.add(CI);
+
+ if (logger.isDebugEnabled()){
+ String verbose_uid = "MISSING USER";
+ if (CI.getOwner() != null){
+ verbose_uid = CI.getOwner().getUid();
+ }
+
+ logger.debug("deleteScenarioInstancesForUsers : deleteCompetenceInstance : CI " + CI.getId() + " owned by : " + verbose_uid);
+ }
+ }
+ }
+ si.getEvaluationSubjectInstance().getCompetenceInstances()
+ .removeAll(sci);
+ evaluationSubjectInstanceManager
+ .deleteCompetenceInstances(sci);
+
+ scenarioInstanceDAO.saveOrUpdate(si);
+ //scenarioInstanceDAO.flush();
+ workflowManager.refreshAssignation(si
+ .getProcessInstanceId());
+
+ }
+
+ }
+
+ }
+ }
+
+ /**
+ * Gets the acquitment dao.
+ *
+ * @return the acquitment dao
+ */
+ public IAcquitmentDAO getAcquitmentDAO() {
+ return acquitmentDAO;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#getAllEvents(java
+ * .lang.String)
+ */
+ public List<Event> getAllEvents(String login) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /**
+ * Gets the definition manager.
+ *
+ * @return the definition manager
+ */
+ public IDefinitionBase getDefinitionManager() {
+ return definitionManager;
+ }
+
+ /**
+ * Gets the entity manager.
+ *
+ * @return the entity manager
+ */
+ public IEntityBase getEntityManager() {
+ return entityManager;
+ }
+
+ /**
+ * Gets the evaluation instance dao.
+ *
+ * @return the evaluation instance dao
+ */
+ public IEvaluationInstanceDAO getEvaluationInstanceDAO() {
+ return evaluationInstanceDAO;
+ }
+
+ /**
+ * Gets the evaluation subject instance manager.
+ *
+ * @return the evaluation subject instance manager
+ */
+ public IEvaluationSubjectInstanceBase getEvaluationSubjectInstanceManager() {
+ return evaluationSubjectInstanceManager;
+ }
+
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#getEvents(java.lang
+ * .String, int)
+ */
+ public List<Event> getEvents(String login, int batchsize) {
+
+ List<Event> res = new ArrayList<Event>();
+
+ Set<Long> sid = new HashSet<Long>();//workflowManager.getUserScenarioInstanceIds(login);
+
+ Set<String> currentActivities = new HashSet<String>();
+
+ Set<Process> sp = workflowManager.getProcesses(login);
+
+ //for (Long siid : sid) {
+ //ScenarioInstance sce = scenarioInstanceDAO.get(siid);
+
+ for (Process process : sp) {
+ sid.add(process.getSIID());
+ //if (process.getType().startsWith(ScenarioDefinition.EVALUATION) && process.getSIID().equals(siid)) {
+ if (process.getType().startsWith(ScenarioDefinition.EVALUATION)){
+ Set<Activity> setAct = process.getActivities();
+
+ for (Activity activity : setAct) {
+
+ currentActivities.add(process.getSIID() + "DEADLINE_" + activity.getId());
+ }
+ }
+ }
+ //}
+ List<Event> lalerts = new ArrayList<Event>();
+
+ if (sid != null && !sid.isEmpty() && currentActivities != null && !currentActivities.isEmpty()){
+ lalerts = scenarioInstanceDAO.getAlerts(sid, currentActivities, 0);
+ }
+
+ List<Event> linfos = getEntityManager().getMessageManager().getEvents(login);
+
+ List<Event> lalertsED = definitionManager.getEvents(login);
+
+ lalerts.addAll(linfos);
+ lalerts.addAll(lalertsED);
+
+ Collections.sort(lalerts, new Comparator<Event>() {
+
+ public int compare(Event o1, Event o2) {
+ try {
+ return o2.getCreationDate().compareTo(o1.getCreationDate());
+ } catch (Exception e) {
+ return 0;
+ }
+ }
+
+ });
+
+ if (lalerts != null && batchsize != -1) {
+
+ if (batchsize > lalerts.size()) {
+ batchsize = lalerts.size();
+ }
+
+ for (int i = 0; i < batchsize; i++) {
+ res.add(lalerts.get(i));
+ }
+ } else if (lalerts != null && batchsize == -1) {
+ res = lalerts;
+ }
+
+
+ return res;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#getGroupStat(java
+ * .lang.String, java.lang.Long)
+ */
+ public Map<String, Integer> getGroupStat(String uidGroup, Long idRef, Integer sizeLimit) {
+
+ Map<String, Integer> stat = new HashMap<String, Integer>();
+
+ stat.put("validable", 0);
+ stat.put("nonvalidable", 0);
+
+ Group g = entityManager.getGroup(uidGroup);
+
+ if (g.getUsers().size() < sizeLimit){
+
+ int validable = 0;
+ int nonvalidable = 0;
+ int nbcomp = 0;
+ for (User u : g.getUsers()) {
+
+ TReferentiel tr = getUserReferentielStates(u.getUid(), idRef);
+
+ nbcomp = tr.getCompetenceInfos().keySet().size();
+
+ Map<String, Set<TCompetence>> s = tr.getCompetencesValidability();
+
+ validable = validable + s.get("validable").size();
+ nonvalidable = nonvalidable + s.get("nonvalidable").size();
+
+ }
+
+ stat.put("validable", validable);
+ stat.put("nonvalidable", nonvalidable);
+ stat.put("nbusers", g.getUsers().size());
+ stat.put("nbcompetences", nbcomp);
+ }else{
+ return null;
+ }
+ return stat;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#getScenarioInstance
+ * (java.lang.Long)
+ */
+ public ScenarioInstance getScenarioInstance(Long idsi) {
+
+ ScenarioInstance si = scenarioInstanceDAO.get(idsi);
+
+ return si;
+ }
+
+ /**
+ * Gets the scenario instance dao.
+ *
+ * @return the scenario instance dao
+ */
+ public IScenarioInstanceDAO getScenarioInstanceDAO() {
+ return scenarioInstanceDAO;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getScenarioInstances
+ * (java.lang.String)
+ */
+ public Set<ScenarioInstance> getScenarioInstances(String login) {
+ return scenarioInstanceDAO.getUserScenarioInstances(login, null);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getTodoList(java
+ * .lang.String)
+ */
+ public Todo updateTodo(Todo todo, int start, int limit, String filterSearch, String uid) {
+
+ ScenarioInstance si = scenarioInstanceDAO.get(todo.getScenarioInstanceAbstract().getId());
+
+ todo.setScenarioInstanceAbstract(si);
+ List<User> lu = null;
+
+
+ if (uid != null && !uid.equals("")){
+ lu = new ArrayList<User>();
+ lu.add(entityManager.getUser(uid, null));
+ }else{
+ lu = new ArrayList<User>(entityManager.getUsersByUids(si
+ .getRelatedUsersUid(), filterSearch));
+ }
+
+ Collections.sort(lu , new Comparator<User>(){
+
+ public int compare(User u1, User u2) {
+
+ String n1 = u1.getLastname() + " " + u1.getFirstname() + u1.getId().toString();
+ String n2 = u2.getLastname() + " " + u2.getFirstname() + u2.getId().toString();
+ return n1.compareTo(n2);
+ }
+
+ });
+
+
+ int a = 0;
+ if (start > 0){
+ a =start;
+ }
+
+ limit = start + limit;
+
+ if (limit > lu.size()){
+ limit = lu.size();
+ }
+
+ //Set<User> su = new HashSet<User>();
+ Set<Long> usersIds = new HashSet<Long>();
+ for (int i=a; i<limit; i++){
+ User u = lu.get(i);
+
+ usersIds.add(u.getId());
+
+ //su.add(u);
+ }
+
+ todo.setTotalRelatedUsers(lu.size());
+
+ int ciTotal = si.getEvaluationSubjectInstance().getCompetenceInstances().size();
+
+ Set<CompetenceInstance> sci = evaluationSubjectInstanceManager.getCompetenceInstancesForUsers(si.getEvaluationSubjectInstance().getId(), usersIds);
+
+ si.getEvaluationSubjectInstance().setCompetenceInstances(sci);
+
+
+ attachEntityDefinitionName(todo);
+
+ TCompositeDataMap T = new TCompositeDataMap();
+
+ Map<String, String[]> mapKeys = new HashMap<String, String[]>();
+
+ String[][] mapData = new String[sci.size()][];
+
+ for (CompetenceInstance ci : sci){
+ String evalName = "";
+ String evalId = "";
+ if (ci.getEvaluateur() != null){
+ evalName = ci.getEvaluateur().getLastname() + " " + ci.getEvaluateur().getFirstname();
+ evalId = ci.getEvaluateur().getId().toString();
+
+ }
+
+ String candName = "";
+ String candId = "";
+ if (ci.getOwner() != null){
+ candName = ci.getOwner().getLastname() + " " + ci.getOwner().getFirstname();
+ candId = ci.getOwner().getId().toString();
+
+ }
+
+ String motiv = "";
+ if (ci.getCompetenceMotivation() != null){
+ motiv = ci.getCompetenceMotivation();
+ }
+
+ String idLevel = "";
+ if (ci.getLevel() != null){
+ idLevel = ci.getLevel().getId().toString();
+ }
+ String idCompetence = "";
+ String nameCompetence = "";
+ if (ci.getCompetence() != null){
+ idCompetence = ci.getCompetence().getId().toString();
+ nameCompetence = ci.getCompetence().getName();
+ }
+
+ String groupName = "";
+ if (ci.getGroupName() != null){
+ groupName = ci.getGroupName();
+ }
+
+
+
+ String[] m = new String[]{
+ ci.getId().toString(),
+ idLevel,
+ motiv,
+ groupName,
+ evalId,
+ evalName,
+
+ idCompetence,
+
+ candId,
+ candName
+
+
+ };
+ mapKeys.put(candName+"-"+ nameCompetence + "-"+ci.getId().toString(), m);
+ }
+
+
+ List<String> keys = new ArrayList<String>(mapKeys.keySet());
+ Collections.sort(keys);
+ int i = 0;
+ for (String key : keys){
+ mapData[i++] = mapKeys.get(key);
+ }
+
+ T.setDataMap(mapData);
+ T.addItemInfo("", "ciId", null);
+ T.addItemInfo("", "levelId", null);
+ T.addItemInfo("", "ciMotiv", null);
+ T.addItemInfo("", "ciGroupname", null);
+ T.addItemInfo("", "evalId", null);
+ T.addItemInfo("", "evalName", null);
+ T.addItemInfo("", "cId", null);
+ T.addItemInfo("", "candId", null);
+ T.addItemInfo("", "candName", null);
+
+ T.setTotalCount(ciTotal);
+
+ todo.setDataMap(T);
+
+
+ int delay = si.getDelay();
+
+ //On récupère les EventActivity de la campagne
+ Set<EventActivity> eas = si.getEvaluationSubjectInstance().getEvaluationSubject().getEventActivities();
+
+ // Récupere le Bean de l'interpreter
+ //Interpreter bsh = (Interpreter) Beans.getApplicationContext().getBean("bshInterpretor");
+
+
+ // Set du contexte
+ /*try {
+ bsh.set("now", new Date());
+ */
+
+ Set<String> roleNames = new HashSet<String>();
+
+ if (todo.getProcess() != null){
+ Map<String, Object> map = new HashMap<String, Object>();
+
+ for (EventActivity eventActivity : eas) {
+ map.put(eventActivity.getActivityId(), Utils.addDays(eventActivity.getEndDate(), delay));
+
+ }
+
+ InstanceUtil util = new InstanceUtil();
+
+ Process p = util.applyContext(todo.getProcess(), map, null);
+
+ for (Activity act : p.getActivities()){
+ roleNames.addAll(act.getRoleNamePerformers());
+ }
+
+
+ }
+
+ /*Set<String> currentUserRoles = si.getLocalRolesForUser(entityManager
+ .getCurrentUser().getUid());*/
+
+ if (logger.isDebugEnabled()){
+ logger.debug("updateTodo : role for user : + " +entityManager.getCurrentUser().getUid() + " : " );
+ for (String r : roleNames){
+ logger.debug("role : " + r);
+ }
+ }
+
+ todo.setCurrentUserRoles(roleNames);
+
+ Utils.acquiredRoles(roleNames);
+
+
+
+ todo.setRelatedUsers(null);
+ si.getEvaluationSubjectInstance().setCompetenceInstances(null);
+
+ return todo;
+
+ }
+
+
+
+ /**
+ * Attach entity definition name.
+ *
+ * @param todo the todo
+ */
+ private void attachEntityDefinitionName(Todo todo){
+
+ if (todo.getScenarioInstanceAbstract() instanceof ScenarioInstance){
+
+ ScenarioInstance si = (ScenarioInstance) todo.getScenarioInstanceAbstract();
+
+ EntityDefinition e = si.getEvaluationInstance().getEntityDefinition();
+
+ String groupName = null;
+
+ if (e != null){
+ if (e.getEntites().size() == 1 && e.getEntites().iterator().next() instanceof User){
+ groupName = "";
+ }else{
+ groupName = e.getName();;
+
+ }
+
+ for (CompetenceInstance ci : si.getEvaluationSubjectInstance().getCompetenceInstances()){
+ ci.setGroupName(groupName);
+ }
+ }else{
+ // Search groupName
+ Map<String, Set<String>> map = new HashMap<String, Set<String>>();
+ Set<EntityDefinition> sed = si.getEvaluationInstance().getEvaluationDefinition().getInstanciationUnits();
+
+ for (EntityDefinition edef : sed){
+
+ if (edef.getEntites().size() == 1 && edef.getEntites().iterator().next() instanceof User){
+ continue;
+ }
+
+ Set<User> su = edef.getUsers();
+
+ if (!map.containsKey(edef.getName())){
+ map.put(edef.getName(), new HashSet<String>());
+ }
+
+ for (User u : su){
+ map.get(edef.getName()).add(u.getUid());
+ }
+
+ }
+
+ for (CompetenceInstance ci : si.getEvaluationSubjectInstance().getCompetenceInstances()){
+ ci.setGroupName(groupName);
+
+ String uid = ci.getOwner().getUid();
+
+ for (Entry<String,Set<String>> entry : map.entrySet()){
+
+ if (entry.getValue().contains(uid)){
+ ci.setGroupName(entry.getKey());
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstance#
+ * getUserCompetenceInstances(java.lang.String)
+ */
+ public Map<Long, Set<CompetenceInstance>> getUserCompetenceInstances(
+ String login) {
+
+ Map<Long, Set<CompetenceInstance>> ee = evaluationSubjectInstanceManager
+ .getUserCompetenceInstances(login);
+
+ return ee;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#getUserReferentiels
+ * (java.lang.String)
+ */
+ public Set<TReferentiel> getUserReferentiels(String login) {
+ Set<TReferentiel> str = new HashSet<TReferentiel>();
+
+
+ Set<Long> sid = entityManager.getEntitiesOfUser(login);
+
+
+ Set<Referentiel> sr = evaluationObjectManager.getUserReferentiels(sid);
+
+ for (Referentiel ref : sr){
+ TReferentiel tr = new TReferentiel();
+ tr.setId(ref.getId());
+ tr.setName(ref.getName());
+ str.add(tr);
+ }
+
+
+
+ return str;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getUserReferentiel2(java.lang.String, java.lang.Long)
+ */
+ public TReferentiel getUserReferentiel2(String login, Long idRef){
+ // Ensemble des id d'objets Entity du user 'login' : Lui meme et ses groupes
+ Set<Long> sid = entityManager.getEntitiesOfUser(login);
+
+ Referentiel ref = evaluationObjectManager.getReferentiel(idRef);
+
+ Set<ProcessRoleDefinition> sprd = definitionManager.getUserRolesByReferentiel(sid, idRef);
+
+
+ Set<PlanRef> spr = new HashSet<PlanRef>();
+ for (ProcessRoleDefinition prd: sprd){
+ ScenarioDefinition sd = (ScenarioDefinition)prd.getPid().getScenarioDefinitionAbstract();
+ Plan p = sd.getEvaluationDefinition().getPlan();
+ if (p != null){
+ for (PlanRef pr : p.getPlanRefs()){
+ if (pr.getReferentiel().getId().equals(idRef)){
+ spr.add(pr);
+ }
+ }
+ }
+
+ }
+
+
+ TReferentiel tr = new TReferentiel();
+ tr.setPlanRefs(spr);
+
+ tr.setId(ref.getId());
+ tr.setName(ref.getName());
+
+
+ tr.setProcessRoleDefinitions(sprd);
+
+
+ return tr;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getUserReferentiel(java.lang.String, java.lang.Long)
+ */
+ @Deprecated
+ public TReferentiel getUserReferentiel(String login, Long idRef){
+ TReferentiel str = new TReferentiel();
+
+ // Ensemble des referentiels dans lesquels le user 'login' à au moins une CompetenceInstance.
+ // ( Candidats dans une campagne à peu de chose pres )
+ Set<Referentiel> sr = evaluationSubjectInstanceManager
+ .getUserReferentiels(login);
+
+ // Ensemble des id d'objets Entity du user 'login' : Lui meme et ses groupes
+ Set<Long> sid = entityManager.getEntitiesOfUser(login);
+
+ // Ensemble des EntityDefinition en relation avec ce user 'login'
+ // dans des campagnes d'evaluation.
+ Set<EntityDefinition> sed = definitionManager
+ .getRelatedEntityDefinitions(sid);
+
+ // Fabrication d'une structure ED / Ensemble de User de l'EntityDefinition.
+ Map<EvaluationDefinition, Set<User>> mesu = new HashMap<EvaluationDefinition, Set<User>>();
+
+ Set<Long> idsED = new HashSet<Long>();
+
+ for (EntityDefinition EDef : sed) {
+
+ ScenarioDefinition SD = (ScenarioDefinition)EDef.getProcessRoleDefinition().getPid()
+ .getScenarioDefinitionAbstract();
+
+ EvaluationDefinition ED = SD.getEvaluationDefinition();
+
+ if (idsED.contains(ED.getId())) {
+ mesu.get(ED).addAll(EDef.getUsers());
+ } else {
+
+ Set<User> ssu = new HashSet<User>();
+ ssu.addAll(EDef.getUsers());
+
+ mesu.put(ED, ssu);
+ idsED.add(ED.getId());
+ }
+
+ }
+
+
+ // Construction d'un TReferentiel
+
+
+ for (Entry<EvaluationDefinition, Set<User>> ent : mesu.entrySet()) {
+
+ EvaluationDefinition ED = ent.getKey();
+
+ Set<User> su = ent.getValue();
+
+ for (Referentiel r : ED.getRelatedReferentiels()) {
+
+ if (r.getId().equals(idRef)){
+ str = new TReferentiel();
+ str.setId(r.getId());
+ str.setName(r.getName());
+
+ if (sr.contains(r)) {
+ str.setForme(true);
+ }
+ if (str.getRelatedUsers() != null){
+ str.getRelatedUsers().addAll(su);
+ }else{
+ str.setRelatedUsers(su);
+ }
+
+ break;
+ }
+ }
+ }
+
+ for (Referentiel r : sr) {
+ if (r.getId().equals(idRef)){
+ str.setId(r.getId());
+ str.setName(r.getName());
+ str.setForme(true);
+ break;
+ }
+ }
+
+
+ return str;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#getUserReferentielStates
+ * (java.lang.String, java.lang.Long)
+ */
+ public TReferentiel getUserReferentielStates(String login, Long idRef) {
+
+ TReferentiel str = new TReferentiel();
+
+ Date now = new Date();
+
+ Map<EvaluationDefinition, Set<CompetenceInstance>> ee = evaluationSubjectInstanceManager
+ .getUserEDefinitionCompetenceInstanceByRef(login, idRef);
+
+ Map<Long, Set<TCompetenceState>> mcs = new HashMap<Long, Set<TCompetenceState>>();
+
+ for (Entry<EvaluationDefinition, Set<CompetenceInstance>> e : ee
+ .entrySet()) {
+ EvaluationDefinition ed = e.getKey();
+ Set<CompetenceInstance> sci = e.getValue();
+
+ TEvaluationDefinitionState TES = new TEvaluationDefinitionState();
+ TES.setId(ed.getId());
+ TES.setName(ed.getName());
+ int state = ed.getState();
+
+ if (now.after(ed.getDateEnd())) {
+ state = EvaluationDefinition.FINISHED;
+ }
+
+ TES.setState(state);
+
+ for (CompetenceInstance ci : sci) {
+
+ TCompetenceState TCS = new TCompetenceState();
+
+ TCS.setId(ci.getId());
+ TCS.setModifiedDate(ci.getLastModified());
+ TCS.setOwnerName(ci.getOwner().getLastname() + " "
+ + ci.getOwner().getFirstname());
+
+ if (ci.getEvaluateur() != null) {
+ TCS.setEvalName(ci.getEvaluateur().getLastname() + " "
+ + ci.getEvaluateur().getFirstname());
+ }
+ Level l = ci.getLevel();
+
+ MethodEval me = l.getMethodEval();
+ Level threshold = me.getThresholdLevel();
+
+ TCS.setInitial(l.getInitial());
+ TCS.setLevelName(l.getName());
+ TCS.setLevelValue(l.getValue());
+
+ TCS.setValidable(l.getValue() >= threshold.getValue());
+
+ TCS.setEvaluationDefinitionState(TES);
+
+ if (!mcs.containsKey(ci.getCompetence().getId())) {
+ mcs.put(ci.getCompetence().getId(),
+ new HashSet<TCompetenceState>());
+ }
+ mcs.get(ci.getCompetence().getId()).add(TCS);
+
+ }
+
+ }
+
+ Referentiel r = definitionManager.getReferentiel(idRef);
+
+ str.setId(r.getId());
+ str.setName(r.getName());
+ Map<TCompetence, Set<TCompetenceState>> infos = str
+ .getCompetenceInfos();
+
+ for (Competence c : r.getAllCompetences()) {
+
+ TCompetence tc = new TCompetence();
+ tc.setId(c.getId());
+ tc.setName(c.getName());
+ tc.setRequired(c.getRequired());
+ tc.setDescription(c.getDescription());
+
+ TDomain td = new TDomain();
+ td.setId(c.getDomain().getId());
+ td.setName(c.getDomain().getName());
+ td.setDescription(c.getDomain().getDescription());
+ tc.setDomain(td);
+
+ Set<TCompetenceState> stcs = null;
+
+ if (!mcs.containsKey(c.getId())) {
+ Set<TCompetenceState> tt = new HashSet<TCompetenceState>();
+ stcs = tt;
+ mcs.put(c.getId(), tt);
+ }
+ stcs = mcs.get(c.getId());
+
+ infos.put(tc, stcs);
+
+ }
+
+ return str;
+ }
+
+ /**
+ * Gets the workflow manager.
+ *
+ * @return the workflow manager
+ */
+ public IWorkflowBase getWorkflowManager() {
+ return workflowManager;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#instanciateProcess
+ * (com.pentila.evalcomp.domain.instance.ScenarioInstance, java.lang.String,
+ * java.util.Map)
+ */
+ public void instanciateProcess(ScenarioInstance si, String processDefId,
+ Map parameters) {
+ // TODO Auto-generated method stub
+
+
+
+
+ String wid = workflowManager.instanciate("a", processDefId, parameters);
+
+ si.setProcessInstanceId(wid);
+ Date date = new Date();
+
+ si.setCreationDate(date);
+ // scenarioInstanceDAO.evict(si);
+
+
+ scenarioInstanceDAO.merge(si);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#instanciateProcesses
+ * (java.util.Set)
+ */
+ public void instanciateProcesses(Set<BonitaProcessLaunch> sbpl) {
+
+ workflowManager.instanciateProcesses(sbpl);
+
+ for (BonitaProcessLaunch bpl : sbpl) {
+
+ ScenarioInstance si = (ScenarioInstance) bpl.getSI();
+
+ String wid = bpl.getBonitaProcessId();
+ si.setProcessInstanceId(wid);
+ Date date = new Date();
+
+ si.setCreationDate(date);
+ // scenarioInstanceDAO.evict(si);
+
+ scenarioInstanceDAO.merge(si);
+
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#notifyByMessage
+ * (java.lang.Object, java.lang.String, java.lang.String, java.lang.String,
+ * java.util.Set)
+ */
+ public boolean notifyByMessage(Object objissuer, String SIID,
+ String subject, String body, Set<String> rolesRecipient) {
+ // TODO Auto-generated method stub
+ User issuer = null;
+ if (objissuer != null && objissuer instanceof User) {
+ issuer = (User) objissuer;
+ }
+
+ Long siid = new Long(SIID);
+
+ ScenarioInstance SI = scenarioInstanceDAO.get(siid);
+
+ Message m = new Message();
+ m.setAuthor(issuer);
+ m.setSubject(subject);
+ m.setEsi(SI.getEvaluationSubjectInstance());
+
+ m.setBody(body);
+
+ Set<Entity> recipients = new HashSet<Entity>();
+
+ for (String role : rolesRecipient) {
+ Set<String> uids = SI.getUserForRole(role, false);
+
+ for (String uid : uids) {
+ User u = (entityManager).getUser(uid);
+ recipients.add(u);
+ }
+
+ }
+ m.setRecipients(recipients);
+
+ this.getEntityManager().getMessageManager().addMessage(m);
+
+ return true;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#performActivity(java
+ * .lang.String, java.lang.String, java.lang.String, java.lang.String)
+ */
+ public String performActivity(String login, String processuuid,
+ String activityId, String transitionId) {
+
+ workflowManager.runProcessActivity(login, processuuid, activityId,
+ transitionId);
+ return "";
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#registerEvent
+ * (java.lang.Object, java.lang.String, java.lang.String, java.util.Set)
+ */
+ public boolean registerEvent(Object objissuer, String SIID,
+ String description, Set<String> rolesRecipient) {
+ User issuer = null;
+ if (objissuer != null && objissuer instanceof User) {
+ issuer = (User) objissuer;
+ }
+
+ Long siid = new Long(SIID);
+
+ ScenarioInstance SI = scenarioInstanceDAO.get(siid);
+ Date creationDate = new Date();
+ int type = Event.TYPE_INFO;
+
+
+
+ for (String role : rolesRecipient) {
+
+
+
+ Set<String> uids = SI.getUserForRole(role, false);
+
+ if (logger.isDebugEnabled()){
+ logger.debug("registerEvent : for role : " + role );
+ for (String uid : uids){
+ logger.debug("registerEvent : user uid : " + uid);
+ }
+ }
+
+
+ Set<User> su = entityManager.getUsersByUids(uids);
+ for (User u : su) {
+ Event event = new Event();
+
+ event.setCreationDate(creationDate);
+ event.setStartDate(creationDate);
+ event.setScenarioInstanceAbstract(SI);
+ event.setType(type);
+ event.setDescription(description);
+ event.setIssuer(issuer);
+ event.setRecipient(u);
+
+ getEntityManager().getMessageManager().addEvent(event);
+
+ }
+
+ }
+
+ return true;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * reInitScenarioInstance(java.lang.Long)
+ */
+ public void reInitScenarioInstance(Long siid) {
+ // FIXME : Translation needed inside
+
+ Set<BonitaProcessLaunch> sbpl = new HashSet<BonitaProcessLaunch>();
+
+ ScenarioInstance SI = scenarioInstanceDAO.get(siid);
+
+ EvaluationDefinition ED = SI.getEvaluationInstance()
+ .getEvaluationDefinition();
+ Level initLevel = null;
+ if (ED.getMethodEval() != null){
+ initLevel = ED.getMethodEval().getInitialLevel();
+ }
+ EvaluationSubject ES = SI.getEvaluationSubjectInstance()
+ .getEvaluationSubject();
+ ScenarioDefinition sdt = ED.getScenarioDefinitions().iterator().next();
+
+ // Get the logins of User in this SI (for the INITRole)
+ Set<String> logins = SI.getUserForRole(sdt.getInitRole(), false);
+
+ EvaluationInstance EI = SI.getEvaluationInstance();
+
+ Set<User> SU = entityManager.getUsersByUids(logins);
+
+ User sender = entityManager.getCurrentUser();
+
+ String subject = "Réinitilisation de votre évaluation";
+ String body = String
+ .format(
+ "Votre évaluation portant sur %s dans la campagne %s a été réinitialisée.",
+ ES.getName(), ED.getName());
+
+ getEntityManager().getMessageManager().notifyByMessage(sender, subject,
+ body, SU);
+
+ // Delete SI Instnance
+ deleteScenarioInstance(SI);
+
+ EvaluationSubjectInstance ESI = null;
+ if (definitionManager.checkInstanciateScenario(sdt)){
+
+ // Start to rebuild
+
+ ESI = addEvaluationSubjectInstance(ES, EI);
+ ScenarioInstance newSI = addScenarioInstance(EI, sdt, ESI, SU);
+
+ Map<String, Object> parameters = new HashMap<String, Object>();
+ parameters.put("SIID", newSI.getId().toString());
+ ProcessInstanciationDefinition pid = sdt.getPid();
+ for (Property prop : pid.getProperties()) {
+ if (prop.getType().equals(Property.PROPERTY)) {
+ parameters.put(prop.getName(), Utils.convertStringToType(prop.getValue(), prop.getDataTypeClassName()));
+ }
+ }
+
+
+ sbpl.add(new BonitaProcessLaunch(newSI, sdt.getProcessDefinitionUUID(),
+ parameters));
+ }
+
+ if (definitionManager.checkInstanciateCI(sdt)){
+ buildCompetenceInstances(new ArrayList<EvaluationObject>(ES.getEvaluationObjects()), SU, ESI, initLevel);
+
+ //scenarioInstanceDAO.flush();
+
+ }
+ if (definitionManager.checkInstanciateScenario(sdt)){
+
+ instanciateProcesses(sbpl);
+ }
+
+ definitionManager.buildExamenInstances(SU, ESI);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#removeAcquitment(
+ * java.lang.String, java.lang.String)
+ */
+ public void removeAcquitment(String SIID, String activityId) {
+
+ acquitmentDAO.deleteBySIID(new Long(SIID), activityId);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#removeParticipants
+ * (com.pentila.evalcomp.domain.instance.ScenarioInstance, java.util.Map)
+ */
+ public boolean removeParticipants(ScenarioInstanceAbstract si,
+ Map<String, Set<Entity>> map) {
+
+ boolean modified = false;
+
+ // Update the SI instance to be sure SI exist yet...
+ si = scenarioInstanceAbstractDAO.get(si.getId());
+
+ if (si != null) {
+
+ for (Entry<String, Set<Entity>> e : map.entrySet()) {
+ String role = e.getKey();
+ Set<Entity> su = e.getValue();
+
+ for (Entity u : su) {
+
+
+ if (logger.isDebugEnabled()){
+ logger.debug("removeParticipants for " + u.getUid());
+ }
+ boolean removed = si.removeLocalRole(role, u.getUid());
+
+ if (removed){
+ modified = true;
+ }
+
+ }
+
+ }
+
+ if (modified){
+
+ scenarioInstanceAbstractDAO.saveOrUpdate(si);
+
+ }
+
+ // NEEDED !!!!
+ //scenarioInstanceDAO.flush(); NON
+
+ //workflowManager.refreshAssignation(si.getProcessInstanceId());
+
+
+ }
+ return modified;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#replaceParticipant
+ * (com.pentila.evalcomp.domain.instance.ScenarioInstance, java.lang.String,
+ * java.lang.String, java.lang.String, java.lang.String)
+ */
+ public boolean replaceParticipant(ScenarioInstance si, String uidFrom,
+ String roleFrom, String uidTo, String roleTo) {
+
+
+
+ boolean present = si.removeLocalRole(roleFrom, uidFrom);
+
+ if (logger.isDebugEnabled()){
+
+ logger.debug("replaceParticipant");
+ logger.debug("removeLocalRole : " + present);
+
+
+ }
+
+
+
+ if (present) {
+ si.addLocalRole(roleTo, uidTo);
+
+ scenarioInstanceDAO.saveOrUpdate(si);
+
+ if (logger.isDebugEnabled()){
+
+ logger.debug("Assign and save with " + uidTo + " : " + roleTo);
+
+ }
+
+ // NEEDED !!!!
+ //scenarioInstanceDAO.flush();
+ workflowManager.refreshAssignation(si.getProcessInstanceId());
+ Set<String> suid = new HashSet<String>();
+ suid.add(uidTo);
+ evaluationSubjectInstanceManager.setPermission("", suid, si
+ .getEvaluationSubjectInstance());
+
+ }
+
+ return present;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#setAcquitment(java
+ * .lang.String, java.lang.String)
+ */
+ public void setAcquitment(String SIID, String activityId) {
+
+ ScenarioInstance SI = scenarioInstanceDAO.get(new Long(SIID));
+
+ Acquitment ac = new Acquitment();
+ ac.setActivityId(activityId);
+ ac.setDate(new Date());
+ ac.setScenarioInstance(SI);
+ acquitmentDAO.saveOrUpdate(ac);
+
+ }
+
+ /**
+ * Sets the acquitment dao.
+ *
+ * @param acquitmentDAO
+ * the new acquitment dao
+ */
+ public void setAcquitmentDAO(IAcquitmentDAO acquitmentDAO) {
+ this.acquitmentDAO = acquitmentDAO;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#setCompetenceInstances
+ * (java.util.Set)
+ */
+ public boolean setCompetenceInstances(List<CompetenceInstance> sci) {
+ return evaluationSubjectInstanceManager.setCompetenceInstances(sci);
+ }
+
+ public boolean setCompetenceInstances(TCompositeDataMap t) {
+ return evaluationSubjectInstanceManager.setCompetenceInstances(t);
+ }
+
+ /**
+ * Sets the definition manager.
+ *
+ * @param definitionManager
+ * the new definition manager
+ */
+ public void setDefinitionManager(IDefinitionBase definitionManager) {
+ this.definitionManager = definitionManager;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#setDelay(java
+ * .lang.Long, java.lang.String)
+ */
+ public void setDelay(Long siid, String value) {
+ // FIXME : Translation needed inside
+ ScenarioInstance SI = scenarioInstanceDAO.get(siid);
+ SI.setDelay(Integer.parseInt(value));
+ scenarioInstanceDAO.saveOrUpdate(SI);
+
+ Set<String> roleRec = new HashSet<String>();
+
+ roleRec.add(SI.getScenarioDefinition().getInitRole());
+
+ registerEvent(entityManager.getCurrentUser(), "" + siid, String.format(
+ " : Obtention d'un délai supplémentaire de %s jour(s)", value),
+ roleRec);
+
+ }
+
+ /**
+ * Sets the entity manager.
+ *
+ * @param entityManager
+ * the new entity manager
+ */
+ public void setEntityManager(IEntityBase entityManager) {
+ this.entityManager = entityManager;
+ }
+
+ /**
+ * Sets the evaluation instance dao.
+ *
+ * @param evaluationInstanceDAO
+ * the new evaluation instance dao
+ */
+ public void setEvaluationInstanceDAO(
+ IEvaluationInstanceDAO evaluationInstanceDAO) {
+ this.evaluationInstanceDAO = evaluationInstanceDAO;
+ }
+
+ /**
+ * Sets the evaluation subject instance manager.
+ *
+ * @param evaluationSubjectInstanceManager
+ * the new evaluation subject instance manager
+ */
+ public void setEvaluationSubjectInstanceManager(
+ IEvaluationSubjectInstanceBase evaluationSubjectInstanceManager) {
+ this.evaluationSubjectInstanceManager = evaluationSubjectInstanceManager;
+ }
+
+ /**
+ * /*
+ * (non-Javadoc).
+ *
+ * @param idScenarioInstance the id scenario instance
+ * @param se the se
+ * @return true, if successful
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstance#setParticipants(java
+ * .lang.Long, java.util.Map)
+ */
+ public boolean setParticipants(Long idScenarioInstance,
+ Map<String, Set<Entity>> se) {
+ ScenarioInstance SI = scenarioInstanceDAO.get(idScenarioInstance);
+
+ Set<String> lr = SI.getLocalroles();
+
+ for (Entry<String, Set<Entity>> e : se.entrySet()) {
+ String role = e.getKey();
+
+ Set<String> removeLR = new HashSet<String>();
+
+ for (String r : lr) {
+ if (r.startsWith(role)) {
+ removeLR.add(r);
+ }
+ }
+
+ lr.removeAll(removeLR);
+
+ for (Entity ent : e.getValue()) {
+ for (User u : ent.getUsers()) {
+ String localrole = role + "_" + u.getUid();
+
+ lr.add(localrole);
+
+ }
+ }
+
+ }
+ SI.setLocalroles(lr);
+
+ scenarioInstanceDAO.saveOrUpdate(SI);
+ // NEEDED !!!!
+ //scenarioInstanceDAO.flush();
+ workflowManager.refreshAssignation(SI.getProcessInstanceId());
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#autoAffectEvaluateur(java.lang.String)
+ */
+ public boolean autoAffectEvaluateur(String SIID) {
+ // FIXME : hardcored role ...
+
+ Long siid = new Long(SIID);
+ ScenarioInstance si = scenarioInstanceDAO.get(siid);
+
+ TListEObject T = definitionManager.getUsersOfProcessRoleDefinition(si.getId(), "Evaluateurs", 0, 100000, null);
+
+ List<User> su = new ArrayList<User>();
+ for (EObject eo : T.getListOfEObjects()){
+ User u = (User) eo;
+
+ su.add(u);
+ }
+
+ if (su.size() != 1) {
+ return false;
+ }
+ else {
+
+ Map<String, Set<Entity>> se = new HashMap<String, Set<Entity>>();
+ Set<Entity> ss = new HashSet<Entity>();
+
+ ss.add(su.get(0));
+ se.put("Evaluateurs", ss);
+
+ if (setParticipants(siid,se)) {
+ return true;
+ }
+
+ return false;
+ }
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#setParticipantsWithArgument(java.lang.Long, java.util.Map, java.lang.String)
+ */
+ public boolean setParticipantsWithArgument(Long idScenarioInstance,
+ Map<String, Set<Entity>> se, String argument) {
+
+ EvaluationSubjectInstance esi = scenarioInstanceDAO.get(idScenarioInstance).getEvaluationSubjectInstance();
+ esi.setEvaluateurChoiceArgument(argument);
+
+ evaluationSubjectInstanceManager.addEvaluationSubjectInstance(esi);
+
+ return setParticipants(idScenarioInstance, se);
+ }
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#setPermission
+ * (java.lang.String, java.lang.String, java.util.Set, java.lang.Object)
+ */
+ public void setPermission(String perm, String SIID, Set<String> roles,
+ Object u) {
+
+ Set<String> suid = new HashSet<String>();
+
+ // Add User
+ User issuer = null;
+ if (u != null && u instanceof User) {
+ issuer = (User) u;
+ suid.add(issuer.getUid());
+ }
+
+ Long siid = new Long(SIID);
+
+ ScenarioInstance SI = scenarioInstanceDAO.get(siid);
+
+ // Associate user of the roles
+ if (roles != null) {
+ for (String role : roles) {
+ Set<String> uids = SI.getUserForRole(role, false);
+ if (uids != null) {
+ suid.addAll(uids);
+ }
+ }
+ }
+ EvaluationSubjectInstance ESI = SI.getEvaluationSubjectInstance();
+
+ evaluationSubjectInstanceManager.setPermission(perm, suid, ESI);
+
+ }
+
+ /**
+ * Sets the scenario instance dao.
+ *
+ * @param scenarioInstanceDAO
+ * the new scenario instance dao
+ */
+ public void setScenarioInstanceDAO(IScenarioInstanceDAO scenarioInstanceDAO) {
+ this.scenarioInstanceDAO = scenarioInstanceDAO;
+ }
+
+ /**
+ * Sets the workflow manager.
+ *
+ * @param workflowManager
+ * the new workflow manager
+ */
+ public void setWorkflowManager(IWorkflowBase workflowManager) {
+ this.workflowManager = workflowManager;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#instanciateEDForUser
+ * (com.pentila.evalcomp.domain.instance.ScenarioInstance,
+ * com.pentila.evalcomp.domain.User)
+ */
+ public int instanciateEDForUser(ScenarioInstance si, User userInstanciation) {
+
+ Set<EvaluationSubject> ses = new HashSet<EvaluationSubject>();
+ ses.add(si.getEvaluationSubjectInstance().getEvaluationSubject());
+ return definitionManager.instanciateForUserEI(si
+ .getEvaluationInstance(), userInstanciation, ses);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#finish(java.lang
+ * .Object, java.lang.String)
+ */
+ public void finish(Object u, String SIID) {
+
+ User user = (User) u;
+
+ Long siid = new Long(SIID);
+
+ ScenarioInstance SI = scenarioInstanceDAO.get(siid);
+
+ evaluationSubjectInstanceManager.finish(user, SI);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * getUsersDefinitionForRole(java.lang.String, java.lang.String)
+ */
+ public Set<String> getUsersDefinitionForRole(String SIID, String role) {
+ Long idsi = new Long(SIID);
+ ScenarioInstance si = getScenarioInstance(idsi);
+ return si.getUsersDefinitionForRole(role);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IInstanceBase#
+ * getUsersDefinitionForRoleAndES(java.lang.String, java.lang.String)
+ */
+ public Set<String> getUsersDefinitionForRoleAndES(String SIID, String role) {
+ Long idsi = new Long(SIID);
+ ScenarioInstance si = getScenarioInstance(idsi);
+ return si.getUsersDefinitionForRoleAndES(role);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getUsersForRole
+ * (java.lang.String, java.lang.String)
+ */
+ public Set<String> getUsersForRole(String SIID, String role) {
+
+ Set<String> uids = new HashSet<String>();
+
+ Long idsi = new Long(SIID);
+
+ ScenarioInstance si = getScenarioInstance(idsi);
+
+ if (si != null) {
+ uids.addAll(si.getUserForRole(role, false));
+ }
+
+ return uids;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IInstanceBase#instanciateEDForUser
+ * (java.lang.String, com.pentila.evalcomp.domain.User)
+ */
+ public int instanciateEDForUser(String SIID, User userInstanciation) {
+ Long idsi = new Long(SIID);
+
+ ScenarioInstance si = getScenarioInstance(idsi);
+ return instanciateEDForUser(si, userInstanciation);
+
+ }
+
+ /**
+ * Gets the evaluation object manager.
+ *
+ * @return the evaluation object manager
+ */
+ public IEvaluationObjectBase getEvaluationObjectManager() {
+ return evaluationObjectManager;
+ }
+
+ /**
+ * Sets the evaluation object manager.
+ *
+ * @param evaluationObjectManager the new evaluation object manager
+ */
+ public void setEvaluationObjectManager(
+ IEvaluationObjectBase evaluationObjectManager) {
+ this.evaluationObjectManager = evaluationObjectManager;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#setProperties(java.lang.String, java.util.Set)
+ */
+ public void setProperties(String processuuid, Set<Property> sp) {
+
+ Map<String, Property> map = new HashMap<String, Property>();
+
+ for (Property p : sp){
+ map.put(p.getName(), p);
+ }
+
+ try{
+
+ String folder_name = null;
+ String folder_description = null;
+ if (map.containsKey("VARIABLE_folder_name") ){
+ folder_name = map.get("VARIABLE_folder_name").getValue();
+ }
+ if (map.containsKey("VARIABLE_description") ){
+ folder_description = map.get("VARIABLE_description").getValue();
+ }
+
+ if (folder_name != null || folder_description != null){
+
+ ScenarioInstance si = scenarioInstanceDAO.getScenarioInstance(processuuid);
+ if (si != null){
+ evaluationSubjectInstanceManager.setProperties(si.getEvaluationSubjectInstance(), sp);
+ }
+
+ }
+
+
+ }catch(Exception e){
+
+ }
+
+ workflowManager.setProperties(processuuid, sp);
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#populateCIs(java.lang.String, java.lang.Long, java.util.List)
+ */
+ public void populateCIs(String login, Long siid, List<CompetenceInstance> competenceIds) {
+
+ ScenarioInstance si = getScenarioInstance(siid);
+
+ User u = entityManager.getUser(login);
+
+ EvaluationSubjectInstance ESI = si.getEvaluationSubjectInstance();
+
+ // Construction d'une liste 'lc' ordonnée sur le nom comptant
+ // l'ensemble des competences de la campagne.
+ Set<EvaluationObject> seo = si.getEvaluationSubjectInstance()
+ .getEvaluationSubject().getEvaluationObjects();
+
+
+ if (logger.isDebugEnabled()){
+ logger.debug("Set Of ES (eo) in this ED :");
+ for (EvaluationObject eo : seo){
+ logger.debug("EO : " + eo.getId() + " Name : " + eo.getName());
+ }
+ }
+
+ List<Competence> lc = new ArrayList<Competence>();
+ for (EvaluationObject eo : seo) {
+ lc.addAll(eo.getAllCompetences());
+ }
+
+ Collections.sort(lc, new Comparator<Competence>() {
+
+ public int compare(Competence o1, Competence o2) {
+ return o1.getName().compareTo(o2.getName());
+ }
+
+ });
+
+ if (logger.isDebugEnabled()){
+ logger.debug("List of Competence in the ED ");
+ for (Competence c : lc){
+ logger.debug("Competence : " + c.getId() + " Name : " + c.getName());
+ }
+ }
+
+
+
+ // Liste temporaire (ll) pour la constuction de la liste ordonnée (lcc) (selon
+ // l'id de CompetenceInstance et couvrant la liste de competence (lc)
+ List<CompetenceInstance> ll = new ArrayList<CompetenceInstance>(si.getEvaluationSubjectInstance()
+ .getCompetenceInstances());
+
+ Collections.sort(ll, new Comparator<CompetenceInstance>(){
+
+ public int compare(CompetenceInstance o1, CompetenceInstance o2) {
+ // TODO Auto-generated method stub
+ return o1.getId().compareTo(o2.getId());
+ }
+
+ });
+
+ if (logger.isDebugEnabled()){
+ logger.debug("List of Competence in the ED ");
+ for (CompetenceInstance ci : ll){
+ logger.debug("CompetenceInstance : " + ci.getId() + " related Competence Name : " + ci.getCompetence().getName() + " / EI related : " + ci.getEvaluationSubjectInstance().getScenarioInstance().getEvaluationInstance().getId());
+ }
+ }
+
+
+
+ List<CompetenceInstance> lcc = new ArrayList<CompetenceInstance>();
+
+
+ // Construction de la liste lcc en fonction de la liste des competence et de ll.
+ // exemple :
+ // C1 | C1 | C2 | C3
+ // Ci1| null | Ci2 | Ci3
+ for (Competence c: lc){
+
+ // On recherche une competenceinstance correspondant à la competence c
+ CompetenceInstance ciRes = null;
+
+ int index = -1;
+ int i = 0;
+ for (CompetenceInstance ci : ll){
+ if (ci.getCompetence().getId().equals(c.getId())){
+ index = i;
+ ciRes = ci;
+ break;
+ }
+ i++;
+ }
+
+ if (index != -1){
+ // On a trouvé une competence instance : on la supprimer par position
+ // Ce mécanisme est du à la possibilté d'avoir pour une competence donnée plusieurs
+ // competence instance dans une meme campagne.
+ ll.remove(index);
+ }
+ lcc.add(ciRes);
+ }
+
+ int i = 0;
+ // Liste de competence instance à supprimer
+ List<CompetenceInstance> toDelete = new ArrayList<CompetenceInstance>();
+
+ //Liste id de competence pour lesquel il faut creer une nouvelle competenceinstance
+ List<CompetenceInstance> toBuild = new ArrayList<CompetenceInstance>();
+
+ // Liste de competence instance à éditer
+ List<CompetenceInstance> toEdit = new ArrayList<CompetenceInstance>();
+
+
+ // On parcours la liste d'identifiant "à trou" envoie par l'interface
+ // example
+ // 1 | 1 | null | 4 | 5
+ for (CompetenceInstance cid : competenceIds){
+
+ // on cherche dans la liste ordonnée si il y a une CI ou non (dans le model persisté)
+ CompetenceInstance ci = lcc.get(i);
+
+ if (cid == null && ci != null){
+
+ // l'interface envoie pour la competence à la position i qu'on a décocher la competence à la position i
+ // et que dans le modele (coté server) il existe une CI : il faut donc supprimer la CI
+
+ toDelete.add(ci);
+ }else if (cid != null && ci == null){
+
+ // L'interface indique que la competence cid est coché à la position i et
+ // qu'il n'y a pas de CI : il faut donc creer une nouvelle CI pour la competence cid
+
+ toBuild.add(cid);
+ }
+ else if (cid != null && ci != null) {
+
+ ci.setCompetenceMotivation(cid.getCompetenceMotivation());
+ toEdit.add(ci);
+ }
+
+
+ i++;
+ }
+
+ // Suppression effective des CI
+ evaluationSubjectInstanceManager.deleteCompetenceInstances(toDelete);
+
+
+ if (toEdit != null && !toEdit.isEmpty()) {
+ for (CompetenceInstance cInstance : toEdit) {
+ evaluationSubjectInstanceManager.addCompetenceInstance(cInstance);
+ }
+ }
+
+
+ if (toBuild != null && !toBuild.isEmpty()) {
+
+ List<EvaluationObject> seo2 = new ArrayList<EvaluationObject>();
+ List<String> listMotiv = new ArrayList<String>();
+
+ for (CompetenceInstance compInst : toBuild){
+ if (compInst != null){
+ EvaluationObject ee = evaluationObjectManager.get(compInst.getCompetence().getId());
+ if (ee != null){
+ seo2.add(ee);
+ listMotiv.add(compInst.getCompetenceMotivation());
+ }
+ }
+
+ }
+
+ Set<User> su = new HashSet<User>();
+ su.add(u);
+
+ Level initLevel = si.getEvaluationInstance()
+ .getEvaluationDefinition().getMethodEval()
+ .getInitialLevel();
+ // Creation effective des CI
+ buildCompetenceInstances(seo2,listMotiv, su, ESI, initLevel);
+
+ }
+
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getScenarioInstanceByPIUUID(java.lang.String)
+ */
+ public ScenarioInstance getScenarioInstanceByPIUUID(String PIUUID) {
+ ScenarioInstance si = new ScenarioInstance();
+ si.setProcessInstanceId(PIUUID);
+
+ List<ScenarioInstance> l = scenarioInstanceDAO.getByExample(si);
+ if (l != null && !l.isEmpty()){
+ return l.get(0);
+ }
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getAllAlerts()
+ */
+ public List<Event> getAllAlerts(){
+
+ List<Event> oo = new ArrayList<Event>();
+ for (Event e : scenarioInstanceDAO.getAlerts()){
+
+ if (e.getScenarioInstanceAbstract() instanceof ScenarioInstance){
+
+ ScenarioInstance si = (ScenarioInstance) e.getScenarioInstanceAbstract();
+
+ si.getEvaluationInstance().getEvaluationDefinition().getName();
+ si.getEvaluationSubjectInstance().getEvaluationSubject().getComputedName();
+
+ }
+
+
+ Set<String> uids = e.getScenarioInstanceAbstract().getRelatedUsersUid();
+ Set<User> us = entityManager.getUsersByUids(uids);
+ for (User u : us){
+ Event ee = new Event();
+ ee.setIssuer(e.getIssuer());
+ ee.setScenarioInstanceAbstract(e.getScenarioInstanceAbstract());
+ ee.setCreationDate(e.getCreationDate());
+ ee.setStartDate(e.getStartDate());
+ ee.setEndDate(e.getEndDate());
+ ee.setRecipient(u);
+ ee.setDescription(e.getDescription());
+ oo.add(ee);
+ }
+
+
+
+ }
+ return oo;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getEvents(java.lang.String, int, int)
+ */
+ public TListEObject getEvents(String login, int start, int limit) {
+ List<Event> le = this.getEvents(login, -1);
+
+ int a = 0;
+ if (start > 0){
+ a =start;
+ }
+
+ limit = start + limit;
+
+ if (limit > le.size()){
+ limit = le.size();
+ }
+
+ TListEObject tle= new TListEObject();
+ tle.setTotal(le.size());
+
+
+ List<EObject> levent = new ArrayList<EObject>();
+
+ for (int i=a; i<limit; i++){
+ levent.add(le.get(i));
+ }
+
+ tle.setListOfEObjects(levent);
+ return tle;
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getAll()
+ */
+ public Set<ScenarioInstance> getAll(){
+
+ Set<ScenarioInstance> ssi = scenarioInstanceDAO.getAll();
+
+ for (ScenarioInstance si : ssi){
+ si.getRelatedUsersUid();
+ }
+
+ return ssi;
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#fixErrors()
+ */
+ public void fixErrors(){
+
+ List<ScenarioInstance> lsi = scenarioInstanceDAO.getErrors();
+ Set<BonitaProcessLaunch> sbpl = new HashSet<BonitaProcessLaunch>();
+ for (ScenarioInstance si : lsi){
+
+
+
+ boolean wrong = workflowManager.testCoherence(si.getProcessInstanceId(), si.getRelatedUsersUid(), "vaepp_creer");
+ if (wrong){
+
+
+
+ ScenarioDefinitionAbstract sdt = si.getScenarioDefinition();
+
+ Map<String, Object> parameters = new HashMap<String, Object>();
+ parameters.put("SIID", si.getId().toString());
+ ProcessInstanciationDefinition pid = sdt.getPid();
+ for (Property prop : pid.getProperties()) {
+ if (prop.getType().equals(Property.PROPERTY)) {
+ parameters.put(prop.getName(), Utils.convertStringToType(prop.getValue(), prop.getDataTypeClassName()));
+ }
+ }
+
+ sbpl.add(new BonitaProcessLaunch(si, sdt
+ .getProcessDefinitionUUID(), parameters));
+ }
+ }
+
+
+ instanciateProcesses(sbpl);
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getTodoLists(java.lang.String, java.util.Set)
+ */
+ public Set<ContextRef> getTodoLists(String login, Set<Process> sp) {
+ Set<ContextRef> stl = new HashSet<ContextRef>();
+ if (sp == null){
+ sp = workflowManager.getProcesses(login);
+ }
+ if (!sp.isEmpty()) {
+
+ Map<Long, Process> mp = new HashMap<Long, Process>();
+ for (Process p : sp) {
+ if (logger.isDebugEnabled()){
+ logger.debug("ANY RAW Process : id : {} , SIID : {}" , p.getId(), p.getSIID());
+ }
+ if (p.getType().contains(ScenarioDefinition.EVALUATION)) {
+ if (logger.isDebugEnabled()){
+ logger.debug("RAW Process : id : {} , SIID : {}" , p.getId(), p.getSIID());
+ }
+ mp.put(p.getSIID(), p);
+ }
+
+ }
+
+ Set<ScenarioInstance> SSI = new HashSet<ScenarioInstance>();
+ if (!mp.isEmpty()) {
+ SSI = scenarioInstanceDAO.filteredTodos(mp.keySet());
+ if (logger.isDebugEnabled()){
+ logger.debug("SSI Size : {}", SSI.size());
+ }
+ }
+
+ Map<Long, ContextRef> MLT = new HashMap<Long, ContextRef>();
+
+ for (ScenarioInstance si : SSI) {
+
+ List<String> eventActivities = scenarioInstanceDAO.filteredActivity(si);
+
+
+ Process filteredProcess = mp.get(si.getId());
+
+ InstanceUtil util = new InstanceUtil();
+
+
+ filteredProcess = util.applyContext(filteredProcess, null, eventActivities);
+
+ if (filteredProcess.getActivities().size() > 0) {
+
+ EvaluationInstance EI = si.getEvaluationInstance();
+
+ EvaluationDefinition ed = EI.getEvaluationDefinition();
+
+ Plan p = ed.getPlan();
+
+ ContextRef currentContext = null;
+
+ if (!MLT.containsKey(p.getId())){
+ currentContext = new ContextRef();
+
+ TPlan pp = new TPlan();
+ pp.setId(p.getId());
+ pp.setName(p.getName());
+
+
+ currentContext.setPlan(pp);
+ MLT.put(p.getId(), currentContext);
+
+ }
+
+ currentContext = MLT.get(p.getId());
+
+ Session currentSession = null;
+
+ if (!currentContext.getSessions().containsKey(ed.getId())){
+ currentSession = new Session();
+ currentSession.setId(ed.getId());
+ currentContext.getSessions().put(ed.getId(), currentSession);
+ }
+
+ currentSession = currentContext.getSessions().get(ed.getId());
+
+
+ currentSession.setName(ed.getName());
+ currentSession.setType(ScenarioDefinition.EVALUATION);
+ currentSession.setStartDate(ed.getDateStart());
+ currentSession.setEndDate(ed.getDateEnd());
+ currentSession.setDescription(ed.getDescription());
+
+ Todo T = new Todo();
+
+ T.setProcess(filteredProcess);
+
+
+
+ T.setScenarioInstanceAbstract(si);
+
+ String ObjectName = si.getEvaluationSubjectInstance()
+ .getEvaluationSubject().getComputedName();
+
+ if (si.getEvaluationSubjectInstance()
+ .getEvaluationSubject().getEventActivities() != null){
+ si.getEvaluationSubjectInstance()
+ .getEvaluationSubject().getEventActivities().size();
+ }
+
+ Property pname = filteredProcess
+ .getPropertyByName("VARIABLE_folder_name");
+
+ if (pname != null && !pname.getValue().equals("--")) {
+ ObjectName = pname.getValue() + " (" + ObjectName + ")";
+ }
+
+ T.setActionOnObjectNameText(ObjectName);
+
+ Set<User> su = new HashSet<User>();
+ List<User> lu = new ArrayList<User>(entityManager.getUsersByUids(si
+ .getRelatedUsersUid()));
+
+
+ Collections.sort(lu, new Comparator<User>(){
+
+ public int compare(User u1, User u2) {
+ String username1 = u1.getLastname() + " " + u1.getFirstname();
+ String username2 = u2.getLastname() + " " + u2.getFirstname();
+
+ return username1.compareToIgnoreCase(username2);
+ }
+ });
+
+ if (lu.size() > SIZE_MAX){
+ int i = 0;
+ for (User u : lu){
+ su.add(u);
+ i++;
+ if (i == SIZE_MAX){
+ break;
+ }
+ }
+ }else{
+ su = new HashSet<User>(lu);
+ }
+
+ T.setRelatedUsers(su);
+ T.setTotalRelatedUsers(lu.size());
+
+ boolean own = si.getUserForRole(si.getScenarioDefinition().getInitRole(), false).contains(login);
+ if (own && si.getRelatedUsersUid().contains(login)){
+ T.setOwner(true);
+ }else{
+ T.setOwner(false);
+
+
+ }
+
+ currentSession.getTodos().add(T);
+
+ }
+ }
+
+ stl = new HashSet<ContextRef>(MLT.values());
+
+ }
+
+ return stl;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getRelatedUsers(com.pentila.evalcomp.domain.instance.ScenarioInstanceAbstract, java.lang.String)
+ */
+ public List<User> getRelatedUsers(ScenarioInstanceAbstract sia, String term) {
+
+ List<User> lu = new ArrayList<User>();
+
+ ScenarioInstance si = scenarioInstanceDAO.get(sia.getId());
+
+ Set<User> su = entityManager.getUsersByUids(si
+ .getRelatedUsersUid());
+
+ lu = new ArrayList<User>(su);
+
+
+
+ Collections.sort(lu, new Comparator<User>(){
+
+ public int compare(User u1, User u2) {
+ String username1 = u1.getLastname() + " " + u1.getFirstname();
+ String username2 = u2.getLastname() + " " + u2.getFirstname();
+
+ return username1.compareToIgnoreCase(username2);
+
+ }
+
+ });
+
+ if (term != null && !(term.equals(""))){
+
+ term = term.toLowerCase();
+
+ List<User> luu = new ArrayList<User>();
+ for (User u : lu){
+ String l = u.getLastname().toLowerCase();
+ String f = u.getFirstname().toLowerCase();
+ if (l.startsWith(term) || f.startsWith(term)){
+ luu.add(u);
+ }
+ }
+
+ lu = luu;
+ }
+
+ return lu;
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IInstanceBase#getEvaluationSubjectInstance(java.lang.Long)
+ */
+ public TEvaluationSubjectInstance getEvaluationSubjectInstance(Long idESI) {
+
+ TEvaluationSubjectInstance t = new TEvaluationSubjectInstance();
+ EvaluationSubjectInstance ESI = evaluationSubjectInstanceManager.getESIById(idESI);
+ t.setEvaluationSubjectInstance(ESI);
+ Process p = workflowManager.getProcess("", ESI.getScenarioInstance().getProcessInstanceId());
+ InstanceUtil util = new InstanceUtil();
+
+
+ p = util.applyContext(p, null, null);
+
+
+ t.setProcess(p);
+ return t;
+ }
+
+ public ScenarioInstanceAbstract getScenarioInstanceAbstract(Long SIAID) {
+ return scenarioInstanceAbstractDAO.get(SIAID);
+ }
+
+ public String checkEvaluationInitLevel(Long idESI) {
+
+ String ret = "";
+
+ Level l = this.evaluationSubjectInstanceManager.getUnSettedInitLevel(idESI);
+
+ if ( l != null){
+ ret = l.getName();
+ }
+
+ return ret;
+
+
+ }
+
+ public void buildExamenInstances(Set<User> su, EvaluationSubjectInstance ESI) {
+ evaluationSubjectInstanceManager.buildExamenInstances(su, ESI);
+
+ }
+
+ public void buildExamenInstances(EvaluationSubjectInstance ESI) {
+
+ // NEED BY Worklfow Hooks
+ Set<User> su = new HashSet<User>();
+
+ su.add(entityManager.getCurrentUser());
+
+
+ evaluationSubjectInstanceManager.buildExamenInstances(su, ESI);
+
+ }
+
+ public void finishExamen(Long siid) {
+
+ ScenarioInstance si = scenarioInstanceDAO.get(siid);
+
+ EvaluationSubjectInstance ESI = si.getEvaluationSubjectInstance();
+
+ evaluationSubjectInstanceManager.finishExamen(ESI);
+
+
+ }
+
+
+
+
+
+}