--- /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.text.SimpleDateFormat;
+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.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import com.pentila.evalcomp.service.ws.WsErrors;
+import com.pentila.evalcomp.utilities.MessagesI18n;
+import com.pentila.evalcomp.dao.IEntityDefinitionAbstractDAO;
+import com.pentila.evalcomp.dao.IEntityDefinitionDAO;
+import com.pentila.evalcomp.dao.IEvaluationDefinitionDAO;
+import com.pentila.evalcomp.dao.IEvaluationSubjectDAO;
+import com.pentila.evalcomp.dao.IEventActivityDAO;
+import com.pentila.evalcomp.dao.IExamenDAO;
+import com.pentila.evalcomp.dao.IExamenDefDAO;
+import com.pentila.evalcomp.dao.IPlanDAO;
+import com.pentila.evalcomp.dao.IScenarioDefinitionAbstractDAO;
+import com.pentila.evalcomp.dao.IScenarioDefinitionDAO;
+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.RessourceDefinition;
+import com.pentila.evalcomp.domain.Role;
+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.EntityDefinitionAbstract;
+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.examen.Examen;
+import com.pentila.evalcomp.domain.examen.ExamenDef;
+import com.pentila.evalcomp.domain.examen.ExamenInstance;
+import com.pentila.evalcomp.domain.examen.Question;
+import com.pentila.evalcomp.domain.examen.QuestionInstance;
+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.service.ExtendActivity;
+import com.pentila.evalcomp.domain.service.Service;
+import com.pentila.evalcomp.domain.transit.Activity;
+import com.pentila.evalcomp.domain.transit.Process;
+import com.pentila.evalcomp.domain.transit.TCompositeDataMap;
+import com.pentila.evalcomp.domain.transit.TListEObject;
+import com.pentila.evalcomp.domain.transit.TPackageDefinition;
+import com.pentila.evalcomp.domain.transit.TransitProcessDefinition;
+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.IInstanceBase;
+import com.pentila.evalcomp.manager.interfacedef.IMethodEvalBase;
+import com.pentila.evalcomp.manager.interfacedef.IProcessInstanciationDefinitionBase;
+import com.pentila.evalcomp.manager.interfacedef.IRessourceBase;
+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 DefinitionManager.
+ */
+public class DefinitionManager implements IDefinitionBase {
+
+
+ /** The logger. */
+ org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DefinitionManager.class);
+
+
+ /** The entity definition dao. */
+ IEntityDefinitionDAO entityDefinitionDAO;
+
+ /** The entity definition abstract dao. */
+ IEntityDefinitionAbstractDAO entityDefinitionAbstractDAO;
+
+ /** The entity manager. */
+ IEntityBase entityManager;
+
+ // Internal DAO
+ /** The evaluation definition dao. */
+ IEvaluationDefinitionDAO evaluationDefinitionDAO;
+
+ /** The evaluation object manager. */
+ IEvaluationObjectBase evaluationObjectManager;
+
+ /** The evaluation subject dao. */
+ public IEvaluationSubjectDAO evaluationSubjectDAO;
+
+ /** The event activity dao. */
+ IEventActivityDAO eventActivityDAO;
+
+ // Linked Manager
+
+ /** The instance manager. */
+ IInstanceBase instanceManager;
+
+ /** The method eval manager. */
+ IMethodEvalBase methodEvalManager;
+
+ /** The process instanciation definition manager. */
+ IProcessInstanciationDefinitionBase processInstanciationDefinitionManager;
+
+ /** The ressource manager. */
+ IRessourceBase ressourceManager;
+
+
+ /** The scenario definition dao. */
+ IScenarioDefinitionDAO scenarioDefinitionDAO;
+
+ /** The scenario definition abstract dao. */
+ IScenarioDefinitionAbstractDAO scenarioDefinitionAbstractDAO;
+
+ /** The workflow manager. */
+ IWorkflowBase workflowManager;
+
+ IExamenDAO examenDAO;
+
+ IExamenDefDAO examenDefDAO;
+
+ /**
+ * Gets the scenario definition abstract dao.
+ *
+ * @return the scenario definition abstract dao
+ */
+ public IScenarioDefinitionAbstractDAO getScenarioDefinitionAbstractDAO() {
+ return scenarioDefinitionAbstractDAO;
+ }
+
+ /**
+ * Sets the scenario definition abstract dao.
+ *
+ * @param scenarioDefinitionAbstractDAO the new scenario definition abstract dao
+ */
+ public void setScenarioDefinitionAbstractDAO(
+ IScenarioDefinitionAbstractDAO scenarioDefinitionAbstractDAO) {
+ this.scenarioDefinitionAbstractDAO = scenarioDefinitionAbstractDAO;
+ }
+
+ /**
+ * Gets the entity definition abstract dao.
+ *
+ * @return the entity definition abstract dao
+ */
+ public IEntityDefinitionAbstractDAO getEntityDefinitionAbstractDAO() {
+ return entityDefinitionAbstractDAO;
+ }
+
+ /**
+ * Sets the entity definition abstract dao.
+ *
+ * @param entityDefinitionAbstractDAO the new entity definition abstract dao
+ */
+ public void setEntityDefinitionAbstractDAO(
+ IEntityDefinitionAbstractDAO entityDefinitionAbstractDAO) {
+ this.entityDefinitionAbstractDAO = entityDefinitionAbstractDAO;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#addEDefsToED(java
+ * .lang.Long, java.util.Set)
+ */
+ public Set<EntityDefinition> addEDefsToED(Long idEd,
+ Set<EntityDefinition> defs) {
+
+ Set<EntityDefinition> ret = new HashSet<EntityDefinition>();
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idEd);
+
+
+ if (logger.isDebugEnabled()){
+
+ logger.debug("addEDefsToED state of ed :ed.getState() != EvaluationDefinition.FINISHED ? => " + (ed.getState() != EvaluationDefinition.FINISHED)) ;
+
+ }
+
+ if (ed.getState() != EvaluationDefinition.FINISHED) {
+ for (EntityDefinition entitydef : defs) {
+
+ // entitydef.setEvaluationDefinition(ed);
+
+ // Save and get and id
+
+ entitydef = entityDefinitionDAO.merge(entitydef);
+ if (ed.isActive()) {
+ addEDefToED(entitydef, ed.getId());
+ }
+ ret.add(entitydef);
+ }
+ }
+
+ return ret;
+ }
+
+ /**
+ * Adds the e def to ed.
+ *
+ * @param EDef
+ * the e def
+ * @param idEd
+ * the id ed
+ */
+ private void addEDefToED(EntityDefinition EDef, Long idEd) {
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idEd);
+ Set<EntityDefinition> sEDef = new HashSet<EntityDefinition>();
+ sEDef.add(EDef);
+
+
+ if (EDef.isAssociateWithInitRoleDefinition()) {
+ if (logger.isDebugEnabled()){
+ logger.debug("EDef is associate with InitRoleDef");
+ }
+ boolean doProcess = false;
+
+ Level initLevel = null;
+ if (ed.getMethodEval() != null){
+ initLevel = ed.getMethodEval().getInitialLevel();
+ }
+ Set<BonitaProcessLaunch> sbpl = new HashSet<BonitaProcessLaunch>();
+
+ for (ScenarioDefinition sdt : ed.getScenarioDefinitions()) {
+
+ List<Map<Set<User>, EntityDefinition>> map = getPartedInstanciation(
+ ed.getInstanciationMode(), sdt, sEDef);
+
+ if (!isInstanicationMode(ed, sdt,
+ EvaluationDefinition.MODE_COLLECTIVE)
+ || ed.getEvaluationInstances().isEmpty()) {
+
+ doProcess = true;
+ for (Map<Set<User>, EntityDefinition> mapOfSetOfUser : map) {
+ for (Entry<Set<User>, EntityDefinition> E : mapOfSetOfUser
+ .entrySet()) {
+
+ Set<User> SU = E.getKey();
+ EntityDefinition EDefi = E.getValue();
+
+ EvaluationInstance EI = instanceManager
+ .addEvaluationInstance(ed, EDefi);
+
+ if (logger.isDebugEnabled()){
+ logger.debug("addEDefToED : INSTANCIATE : ScenarioDefintion nb : {} for ED id {}"
+ , ed.getScenarioDefinitions()
+ .size(), ed.getId());
+
+ logger.debug("addEDefToED : INSTANCIATE : EvaluationSubject nb : {}"
+ , ed.getEvaluationSubjects().size());
+ }
+
+ for (EvaluationSubject ES : ed
+ .getEvaluationSubjects()) {
+ EvaluationSubjectInstance ESI = instanceManager
+ .addEvaluationSubjectInstance(ES, EI);
+ ScenarioInstance SI = instanceManager
+ .addScenarioInstance(EI, sdt, ESI, SU);
+
+ 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));
+ if (checkInstanciateCI(sdt)) {
+ instanceManager.buildCompetenceInstances(new ArrayList<EvaluationObject>(ES
+ .getEvaluationObjects()), SU, ESI,
+ initLevel);
+ }
+
+ /*
+ * BUILD EXAMEN INSTANCES
+ *
+ */
+ buildExamenInstances(SU, ESI);
+ }
+ }
+ }
+ } else {
+ // Add localrole on each SceanrioInstance
+ // and add CompentenceInstances
+ if (logger.isDebugEnabled()){
+ logger.debug("COLLECTIVE Instanciation mode or EIs is Empty");
+ }
+ for (EvaluationInstance ei : ed.getEvaluationInstances()) {
+ for (ScenarioInstance si : ei.getScenarioInstances()) {
+ if (si.getScenarioDefinition().getId().equals(
+ sdt.getId())) {
+
+ EvaluationSubjectInstance ESI = si
+ .getEvaluationSubjectInstance();
+ EvaluationSubject ES = ESI
+ .getEvaluationSubject();
+ for (Map<Set<User>, EntityDefinition> mapOfSetOfUser : map) {
+ for (Entry<Set<User>, EntityDefinition> e : mapOfSetOfUser
+ .entrySet()) {
+ Set<User> SU = e.getKey();
+ if (checkInstanciateCI(sdt)) {
+ instanceManager
+ .buildCompetenceInstances(
+ new ArrayList<EvaluationObject>(ES
+ .getEvaluationObjects()),
+ SU, ESI, initLevel);
+ }
+
+ /*
+ * BUILD EXAMEN INSTANCES
+ *
+ */
+ buildExamenInstances(SU, ESI);
+
+ Map<String, Set<Entity>> map2 = new HashMap<String, Set<Entity>>();
+
+ map2.put(EDef
+ .getProcessRoleDefinition()
+ .getName(), new HashSet<Entity>(SU));
+ instanceManager.addParticipants(si,
+ map2);
+ }
+ }
+ }
+ }
+ }
+
+ }
+ }
+
+ if (doProcess) {
+
+ // NEEDED !!!!
+ //evaluationDefinitionDAO.flush();
+
+ instanceManager.instanciateProcesses(sbpl);
+ }
+
+ Set<String> uids = new HashSet<String>();
+ for (User u : EDef.getUsers()) {
+ uids.add(u.getUid());
+ }
+
+ for (RessourceDefinition rd : ed.getRelatedRessourceDefinitions()) {
+ ressourceManager.setPermission(rd, uids, "View");
+ }
+
+ } else {
+ if (logger.isDebugEnabled()){
+ logger.debug("EDEf is NOT in INITROLE");
+ }
+
+ /*for (ScenarioDefinition sdt : ed.getScenarioDefinitions()) {
+
+ if (isInstanicationMode(ed, sdt, EvaluationDefinition.MODE_COLLECTIVE)) {
+
+
+ List<Map<Set<User>, EntityDefinition>> map = getPartedInstanciation(ed .getInstanciationMode(), sdt, sEDef);
+
+
+ for (EvaluationInstance EI : ed.getEvaluationInstances()) {
+ for (ScenarioInstance SI : EI.getScenarioInstances()) {
+ for (Map<Set<User>,EntityDefinition> mapOfSetOfUser : map) {
+ for (Entry<Set<User>, EntityDefinition> e : mapOfSetOfUser.entrySet()) {
+ Set<Entity> SU = new HashSet<Entity>();
+
+ SU.addAll(e.getKey());
+
+ Map<String, Set<Entity>> map2 = new HashMap<String, Set<Entity>>();
+ map2.put(EDef.getProcessRoleDefinition().getName(), SU);
+ instanceManager.addParticipants(SI, map2);
+
+ }
+ }
+ }
+ }
+
+ }
+
+ }*/
+
+
+ Set<String> uids = new HashSet<String>();
+ for (User u : EDef.getUsers()) {
+ uids.add(u.getUid());
+ }
+ for (RessourceDefinition rd : ed.getRelatedRessourceDefinitions()) {
+ ressourceManager.setPermission(rd, uids, "View");
+ }
+ }
+
+ }
+
+ public void buildExamenInstances(Set<User> su, EvaluationSubjectInstance ESI) {
+
+ instanceManager.buildExamenInstances(su, ESI);
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#checkScenario(com.pentila.evalcomp.domain.definition.ScenarioDefinitionAbstract)
+ */
+ public boolean checkInstanciateCI(ScenarioDefinitionAbstract sda) {
+ // FIXME : this is an ugly way to to that
+ // YOU MUSt FIX ME
+
+ Set<String> sname = new HashSet<String>();
+
+ // DON'T BUILD CompetenceInstance for this Scenarios
+ sname.add("Scénario VAE");
+ sname.add("Scénario VAEPP");
+ sname.add("Scenario VAEPP");
+ sname.add("Scenario_VAEPP");
+
+ // DON'T build CompetenceInstance for Examen Scenarios
+ if (sda != null && sda.getType() != null && sda.getType().contains("-EXAMEN")){
+ sname.add(sda.getName());
+ }
+
+ return !sname.contains(sda.getName());
+ }
+
+ public boolean checkInstanciateScenario(ScenarioDefinitionAbstract sda) {
+ // FIXME : this is an ugly way to to that
+ // YOU MUSt FIX ME
+
+ Set<String> sname = new HashSet<String>();
+
+ // DON'T BUILD CompetenceInstance for this Scenarios
+ sname.add("Scénario VAE");
+ sname.add("Scénario VAEPP");
+ sname.add("Scenario VAEPP");
+ sname.add("Scenario_VAEPP");
+ // DON'T build CompetenceInstance for Examen Scenarios
+ /*if (sda.getType().contains("-EXAMEN")){
+ sname.add(sda.getName());
+ }*/
+
+ return !sname.contains(sda.getName());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#addESsToED(java
+ * .lang.Long, java.util.Set)
+ */
+ public Set<EvaluationSubject> addESsToED(Long idEd,
+ Set<EvaluationSubject> ses) {
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idEd);
+
+ for (EvaluationSubject es : ses) {
+ es.setEvaluationDefinition(ed);
+ EvaluationSubject ess = evaluationSubjectDAO.saveOrUpdate(es);
+
+ es.setId(ess.getId());
+ }
+
+ return ses;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#addESToED(java
+ * .lang.Long, com.pentila.evalcomp.domain.definition.EvaluationSubject)
+ */
+ public EvaluationSubject addESToED(Long idEd, EvaluationSubject ES) {
+
+ // On Active ED
+ // If it s not a new ES (Update ES)
+ // Compare ES and Hibernate ES
+ /*
+ * New EO => Create CI and link it to ESI, EI, USER and C for all
+ * Init_role User Member
+ *
+ * Missing EO => Delete CI for each INITROLE User Member
+ *
+ * If it new ES (real ADD ES)
+ *
+ * Instanciate process for all InitRole User Member
+ */
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idEd);
+
+ Set<EvaluationObject> seo = new HashSet<EvaluationObject>();
+
+ for (EvaluationObject eo : ES.getEvaluationObjects()) {
+ seo.add(evaluationObjectManager.get(eo.getId()));
+ }
+ ES.setEvaluationObjects(seo);
+
+ if (ES.getId() != null) {
+
+ EvaluationSubject ES3 = evaluationSubjectDAO.get(ES.getId());
+
+ ES.setEntityDefinitions(ES3.getEntityDefinitions());
+ }
+
+ // Plan plan = ed.getPlan();
+
+ // Computer new EOs
+ Set<EvaluationObject> oldEOs = new HashSet<EvaluationObject>();
+ EvaluationSubject ES2 = null;
+ if (ES.getId() != null && ed.isActive()) {
+ ES2 = evaluationSubjectDAO.get(ES.getId());
+ if (ES2 != null) {
+ oldEOs.addAll(ES2.getEvaluationObjects());
+ }
+
+ }
+
+ ES.setEvaluationDefinition(ed);
+
+ ES = associateESToEDBasedOnPlan(ES);
+
+ // Merge for perform change in DB
+
+ ES = evaluationSubjectDAO.merge(ES);
+
+ if (ed.isActive()) {
+
+ Level initLevel = null;
+ if (ed.getMethodEval() != null){
+ initLevel = ed.getMethodEval().getInitialLevel();
+ }
+
+ if (ES2 != null) {
+ // ES is not New
+ // Compare
+ // get New EO
+ // get Missing EO
+
+
+ Set<User> su = new HashSet<User>();
+ for (EntityDefinition edef : ed.getInstanciationUnits()) {
+ su.addAll(edef.getUsers());
+ }
+
+ Set<EvaluationObject> newEOs = new HashSet<EvaluationObject>(ES
+ .getEvaluationObjects());
+ Set<EvaluationObject> missingEOs = new HashSet<EvaluationObject>(
+ oldEOs);
+
+ newEOs.removeAll(oldEOs);
+ missingEOs.removeAll(ES.getEvaluationObjects());
+
+ // building !!!
+ for (EvaluationSubjectInstance esi : ES
+ .getEvaluationSubjectInstances()) {
+ if (checkInstanciateCI(ed.getScenarioDefinitions().iterator()
+ .next())) {
+ instanceManager.buildCompetenceInstances(new ArrayList<EvaluationObject>(newEOs), esi
+ .getRelatedUsers(), esi, initLevel);
+ }
+ instanceManager.deleteCompetenceInstances(new ArrayList<EvaluationObject>(missingEOs), esi
+ .getRelatedUsers(), esi);
+ }
+
+ } else {
+ // ES is new
+ Set<BonitaProcessLaunch> sbpl = new HashSet<BonitaProcessLaunch>();
+ for (ScenarioDefinition sdt : ed.getScenarioDefinitions()) {
+ List<Map<Set<User>, EntityDefinition>> map = getPartedInstanciation(
+ ed.getInstanciationMode(), sdt, ed
+ .getInstanciationUnits());
+
+ for (Map<Set<User>, EntityDefinition> mapOfSetOfUser : map) {
+ for (Entry<Set<User>, EntityDefinition> E : mapOfSetOfUser
+ .entrySet()) {
+
+ Set<User> SU = E.getKey();
+ EntityDefinition EDef = E.getValue();
+
+ Long idEDef = null;
+ if (EDef != null) {
+ idEDef = EDef.getId();
+ }
+
+ Set<EvaluationInstance> SEI = null;
+ if (EDef == null) {
+ SEI = ed.getEvaluationInstances();
+ } else {
+ SEI = EDef.getEvaluationInstances();
+ }
+
+ if (SEI.isEmpty()) {
+ EvaluationInstance EI = instanceManager
+ .addEvaluationInstance(ed, EDef);
+ SEI.add(EI);
+ }
+
+ for (EvaluationInstance EI : SEI) {
+
+ // Add ScenarioInstance :
+ Long idEIEDef = null;
+ if (EI.getEntityDefinition() != null) {
+ idEIEDef = EI.getEntityDefinition().getId();
+ }
+
+ Set<String> sl = new HashSet<String>();
+
+ for (User u : SU) {
+ sl.add(u.getUid());
+ }
+
+ Set<String> logins = EI.getUsersForRole(sdt
+ .getInitRole(), false);
+
+ boolean rightInstance = logins.isEmpty();
+ if (logins.containsAll(sl)) {
+ rightInstance = true;
+ }
+
+ if (idEDef == null
+ || (idEIEDef.equals(idEDef) && rightInstance)) {
+
+ EvaluationSubjectInstance ESI = instanceManager
+ .addEvaluationSubjectInstance(ES,
+ EI);
+ ScenarioInstance SI = instanceManager
+ .addScenarioInstance(EI, sdt, ESI,
+ SU);
+
+ 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));
+ if (checkInstanciateCI(sdt)) {
+ instanceManager
+ .buildCompetenceInstances(
+ new ArrayList<EvaluationObject>(ES
+ .getEvaluationObjects()),
+ SU, ESI, initLevel);
+ }
+
+ }
+ }
+
+ }
+ }
+
+ }
+
+ // NEEDED because the hooks of Bonita work in an other
+ // transaction !!!!
+ // so their need to read uncommited data(WARN) (see
+ // IWorkflowManager)
+ //evaluationDefinitionDAO.flush();
+
+ instanceManager.instanciateProcesses(sbpl);
+
+ }
+
+ }
+
+ return ES;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#addEvaluationDefinition
+ * (com.pentila.evalcomp.domain.definition.EvaluationDefinition)
+ */
+ public EvaluationDefinition addEvaluationDefinition(EvaluationDefinition edd) {
+
+ /*
+ * MethodEval me = new MethodEval(); me.setId(new Long(1));
+ * edd.setMethodEval(me);
+ */
+ evaluationDefinitionDAO.saveOrUpdate(edd);
+ return edd;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#addEventActivityToES
+ * (java.lang.Long, com.pentila.evalcomp.domain.definition.EventActivity)
+ */
+ public EventActivity addEventActivityToES(Long idES, EventActivity ea) {
+
+ EvaluationSubject es = evaluationSubjectDAO.get(idES);
+
+ ea.setEvaluationSubject(es);
+
+ ea = eventActivityDAO.merge(ea);
+
+ return ea;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinitionBase#
+ * addRessourceDefinitionsToES(java.util.Set, java.lang.Long)
+ */
+ public Set<RessourceDefinition> addRessourceDefinitionsToES(
+ Set<RessourceDefinition> srd, Long idES) {
+
+
+ EvaluationSubject ES = evaluationSubjectDAO.get(idES);
+ EvaluationDefinition ED = ES.getEvaluationDefinition();
+
+ Set<EntityDefinition> instUnits = ED.getInstanciationUnits();
+
+ Set<String> instUnitsUids = new HashSet<String>();
+ Set<String> others = new HashSet<String>();
+
+
+ User owner = entityManager.getCurrentUser();
+
+ for (RessourceDefinition rd : srd) {
+ rd.setOwner(owner);
+ rd.setEvaluationSubject(ES);
+ rd = ressourceManager.addRessourceDefinition(rd);
+ // Here ED is launched so set perm for related users.
+
+
+ for (EntityDefinition edef : ED.getEntityDefinitions()) {
+ if (instUnits.contains(edef)) {
+ for (User u : edef.getUsers()) {
+ instUnitsUids.add(u.getUid());
+ }
+ } else {
+ for (User u : edef.getUsers()) {
+ others.add(u.getUid());
+ }
+ }
+ }
+
+
+
+
+ instUnitsUids.addAll(others);
+
+ if (logger.isDebugEnabled()){
+ for (String uid : instUnitsUids){
+ logger.debug("Setting right on RD for " + uid);
+ }
+ }
+
+
+ if (!instUnitsUids.isEmpty()) {
+ ressourceManager.setPermission(rd, instUnitsUids, "View");
+ }
+
+ }
+
+ return srd;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#addSDToED(java.
+ * lang.Long, com.pentila.evalcomp.domain.definition.ScenarioDefinition)
+ */
+ public ScenarioDefinition addSDToED(Long idEd, ScenarioDefinition SD) {
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idEd);
+
+ // Here we replace Scenario Definition because we have one Scenario for
+ // now.
+
+ if (!ed.getScenarioDefinitions().isEmpty()) {
+ for (ScenarioDefinition sd : ed.getScenarioDefinitions()) {
+
+ sd.getEvaluationDefinition().getScenarioDefinitions()
+ .remove(sd);
+
+ scenarioDefinitionDAO.delete(sd);
+ }
+
+
+ for (RessourceDefinition rd : ed.getRelatedRessourceDefinitions()){
+ rd.getEvaluationSubject().getRessourceDefinitions().remove(rd);
+ ed.getRelatedRessourceDefinitions().remove(rd);
+ ressourceManager.removeRessourceDefinition(rd);
+ }
+
+ for (EvaluationSubject es : ed.getEvaluationSubjects()){
+
+ for (EventActivity ea : es.getEventActivities()){
+ eventActivityDAO.delete(ea);
+ }
+ es.getEventActivities().clear();
+
+ }
+
+
+ evaluationDefinitionDAO.evict(ed);
+ //scenarioDefinitionDAO.flush();
+ ed = evaluationDefinitionDAO.get(ed.getId());
+
+ }
+
+
+ if (SD != null){
+
+ if ((SD.getType() == null || !SD.getType().endsWith("-EXAMEN")) && ed.getExamenDefs() != null && !ed.getExamenDefs().isEmpty()){
+ ed.setExamenDefs(new HashSet<ExamenDef>());
+
+ evaluationDefinitionDAO.saveOrUpdate(ed);
+ }
+
+ ed.setScenarioDefinitions(new HashSet<ScenarioDefinition>());
+
+ SD.setEvaluationDefinition(ed);
+
+ String defId = SD.getProcessDefinitionUUID();
+
+ TransitProcessDefinition tpd = workflowManager
+ .getProcessDefinition(defId);
+
+ String author = tpd.getAuthor();
+ String creationDate = tpd.getCreationDate();
+ String description = tpd.getDescription();
+ String initRole = tpd.getInitRole();
+ String name = tpd.getName();
+ String version = tpd.getVersion();
+
+ TPackageDefinition tpack = tpd.getPackageDefinition();
+
+ String authorpack = tpack.getAuthor();
+ String creationDatepack = tpack.getCreationDate();
+ String descriptionpack = tpack.getDescription();
+
+ String namepack = tpack.getName();
+ String versionpack = tpack.getVersion();
+
+ if ((author == null || author.equals("")) && authorpack != null) {
+ author = authorpack;
+ }
+ if ((creationDate == null || creationDate.equals(""))
+ && creationDatepack != null) {
+ creationDate = creationDatepack;
+ }
+ if ((description == null || description.equals(""))
+ && descriptionpack != null) {
+ description = descriptionpack;
+ }
+ if ((name == null || name.equals("")) && namepack != null) {
+ name = namepack;
+ }
+ if ((version == null || version.equals("")) && versionpack != null) {
+ version = versionpack;
+ }
+
+ SD.setAuthor(author);
+ SD.setCreationDate(creationDate);
+ SD.setDescription(description);
+ SD.setInitRole(initRole);
+ SD.setName(name);
+ SD.setVersion(version);
+ SD.setInstanciationMode(tpd.getInstanciationMode());
+ SD.setType(tpd.getType());
+
+ // SD.setType(tpd.getType());
+
+ ProcessInstanciationDefinition pid = new ProcessInstanciationDefinition();
+ pid = processInstanciationDefinitionManager.add(pid);
+ Set<Role> roles = tpd.getRoles();
+ for (Role role : roles) {
+
+ String roleName = role.getName();
+
+ ProcessRoleDefinition prd = new ProcessRoleDefinition();
+ prd.setInitRole(roleName.equals(tpd.getInitRole()));
+ prd.setName(roleName);
+ prd.setSingleMember(role.isSingleMember());
+ processInstanciationDefinitionManager.addProcessRoleDefinition(pid,
+ prd);
+ }
+
+ Set<Activity> sa = tpd.getActivities();
+ for (Activity a : sa) {
+
+ Property p = new Property();
+ p.setName(a.getId());
+ p.setType(Property.ACTIVITY);
+ p.setDescription(a.getDescription());
+ p.setValue(a.getId());
+ processInstanciationDefinitionManager.addProperty(pid, p);
+ }
+
+ Set<Property> properties = tpd.getProperties();
+ for (Property pro : properties) {
+ Property p = new Property();
+ p.setName(pro.getName());
+ p.setType(pro.getType());
+ p.setDescription(pro.getDescription());
+ p.setValue(pro.getValue());
+ p.setDataTypeClassName(pro.getDataTypeClassName());
+ processInstanciationDefinitionManager.addProperty(pid, p);
+ }
+
+ pid = processInstanciationDefinitionManager.getPid(pid.getId());
+
+ SD.setPid(pid);
+
+ scenarioDefinitionDAO.saveOrUpdate(SD);
+ SD = scenarioDefinitionDAO.get(SD.getId());
+ }
+ return SD;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#associateEDefToES
+ * (java.lang.Long, java.lang.Long)
+ */
+ public EntityDefinition associateEDefToES(Long idEDef, Long idES) {
+
+ EntityDefinition edef = entityDefinitionDAO.get(idEDef);
+
+ EvaluationSubject es = evaluationSubjectDAO.get(idES);
+
+ edef.getEvaluationSubjects().add(es);
+ edef = entityDefinitionDAO.merge(edef);
+
+ Set<EntityDefinition> sEDef= new HashSet<EntityDefinition>();
+
+ sEDef.add(edef);
+
+
+ // On regard ici si ED est instancié (si l'ES à des ESI)
+ if (es.getEvaluationSubjectInstances() != null && !es.getEvaluationSubjectInstances().isEmpty()){
+
+ EvaluationDefinition ed = es.getEvaluationDefinition();
+
+ for (ScenarioDefinition sdt : ed.getScenarioDefinitions()) {
+
+ // Ici théoriqueement une seul boucle car ed <-> SD = 1..1 bien que Set<SD>
+
+ if (isInstanicationMode(ed, sdt, EvaluationDefinition.MODE_COLLECTIVE)) {
+
+
+ List<Map<Set<User>, EntityDefinition>> map = getPartedInstanciation(ed .getInstanciationMode(), sdt, sEDef);
+
+
+ for (EvaluationSubjectInstance esi : es.getEvaluationSubjectInstances()){
+ ScenarioInstance SI = esi.getScenarioInstance();
+
+ for (Map<Set<User>,EntityDefinition> mapOfSetOfUser : map) {
+ for (Entry<Set<User>, EntityDefinition> e : mapOfSetOfUser.entrySet()) {
+ Set<Entity> SU = new HashSet<Entity>();
+
+ SU.addAll(e.getKey());
+
+ Map<String, Set<Entity>> map2 = new HashMap<String, Set<Entity>>();
+ map2.put(edef.getProcessRoleDefinition().getName(), SU);
+ instanceManager.addParticipants(SI, map2);
+
+ }
+ }
+ }
+
+
+ }
+
+ }
+ }
+
+
+ return edef;
+
+ }
+
+ /**
+ * Associate es to ed based on plan.
+ *
+ * @param ES
+ * the eS
+ *
+ * @return the evaluation subject
+ */
+ private EvaluationSubject associateESToEDBasedOnPlan(EvaluationSubject ES) {
+ EvaluationDefinition ed = ES.getEvaluationDefinition();
+
+ Plan plan = ed.getPlan();
+
+ Set<EvaluationObject> sc = new HashSet<EvaluationObject>();
+
+ // Get all competence touch by this ES
+ for (EvaluationObject eo : ES.getEvaluationObjects()) {
+ sc.addAll(eo.getAllCompetences());
+ }
+
+ // Get All Entity (User) which dealing who a least one competence in
+ // this ES
+ // in the plan. Entities are parted by their assigned role.
+ Map<String, Set<Entity>> planEntity = plan
+ .getRelatedRoleEntitiesMap(sc);
+
+ Map<String, Set<Entity>> mustAdd = new HashMap<String, Set<Entity>>();
+
+ // Compute a MAP for comparaison facility
+ Map<Long, User> mu = new HashMap<Long, User>();
+ for (EntityDefinition EDef : ES.getEntityDefinitions()) {
+ mu.putAll(getMapUsers(EDef.getUsers()));
+ }
+
+ // Iterate on role : Set of Entity
+ // For each role we check if a user in Set of Entity in Plan is or not
+ // is Set of Entity in EDEfs of ES
+ // If Entity of plan is in Set of Entity of EntityDef of ES : do NOTHING
+ // If not : we must add Entity of Plan
+ for (Entry<String, Set<Entity>> s : planEntity.entrySet()) {
+ String role = s.getKey();
+ Set<Entity> se = s.getValue();
+ Map<Long, Entity> me = getMapEntities(se);
+ for (Entry<Long, Entity> ll : me.entrySet()) {
+ Long idEnt = ll.getKey();
+ Entity ee = ll.getValue();
+
+ // HERE we check if the user in plan in Set of all Edefs of ES
+
+ if (!mu.containsKey(idEnt)) {
+ if (!mustAdd.containsKey(role)) {
+ mustAdd.put(role, new HashSet<Entity>());
+ }
+ mustAdd.get(role).add(ee);
+ }
+ }
+ }
+
+ // Compute a Map for ProcessRoleDef
+ Map<String, ProcessRoleDefinition> msp = new HashMap<String, ProcessRoleDefinition>();
+ for (ScenarioDefinition sd : ed.getScenarioDefinitions()) {
+ for (ProcessRoleDefinition prd : sd.getPid()
+ .getProcessRoleDefinitions()) {
+ msp.put(prd.getName(), prd);
+ }
+
+ }
+
+ for (Entry<String, Set<Entity>> mmm : mustAdd.entrySet()) {
+
+ String role = mmm.getKey();
+
+ if (msp.containsKey(role)) {
+ ProcessRoleDefinition prd = msp.get(role);
+ Set<Entity> se = mmm.getValue();
+ for (Entity ent : se) {
+
+ User u = entityManager.getUser(ent.getId());
+ EntityDefinition E = null;
+
+ // Check if a EDEF is not already present in the ED
+ for (EntityDefinitionAbstract EDA : prd.getEntityDefinitions()) {
+ // We check if this is a group of user or a single user
+
+ EntityDefinition ED = (EntityDefinition) EDA;
+
+ Set<Long> ids = new HashSet<Long>();
+ for (User uuu : ED.getUsers()) {
+ ids.add(uuu.getId());
+ }
+
+ if (ids.contains(u.getId())
+ && ED.getUsers().size() == 1
+ && ED.getEntites().iterator().next().getId()
+ .equals(u.getId())) {
+ E = ED;
+ break;
+ }
+ }
+ // EntityDefintion is not exist so we create one
+ if (E == null) {
+ E = new EntityDefinition();
+ E.getEntites().add(ent);
+ E.setProcessRoleDefinition(prd);
+ // E.setEvaluationDefinition(ed);
+ E.setName(u.getLastname() + " " + u.getFirstname());
+
+ entityDefinitionDAO.saveOrUpdate(E);
+ ed.addEDef(E);
+ }
+
+ // Associate it with the ES
+ ES.getEntityDefinitions().add(E);
+ }
+ }
+ }
+ return ES;
+ }
+
+ /**
+ * Control ed.
+ *
+ * @param ed
+ * the ed
+ *
+ * @return the int
+ */
+ private int controlED(EvaluationDefinition ed) {
+
+ // Verify Plan assignment
+ if (ed.getPlan() == null) {
+ return EvaluationDefinition.ERROR_PLAN;
+ }
+ // Verify ScenarioDefintion Assignment
+ if (ed.getScenarioDefinitions() == null
+ || ed.getScenarioDefinitions().isEmpty()) {
+ return EvaluationDefinition.ERROR_SCENARIODEFINITION;
+ }
+
+ String sType= ed.getScenarioDefinitions().iterator().next().getType();
+
+ // Verfiy Method Eval assignment
+ if ((sType == null || !sType.endsWith("-EXAMEN")) && ed.getMethodEval() == null) {
+ return EvaluationDefinition.ERROR_METHODEVALUATION;
+ }
+
+ // Verify Dates
+ if (ed.getDateStart() == null || ed.getDateEnd() == null
+ || ed.getDateStart().after(ed.getDateEnd())) {
+ return EvaluationDefinition.ERROR_DATE;
+ }
+
+ // Verify Users Assignment
+ for (ScenarioDefinition sd : ed.getScenarioDefinitions()) {
+ ProcessInstanciationDefinition pid = sd.getPid();
+ for (ProcessRoleDefinition prd : pid.getProcessRoleDefinitions()) {
+ Set<EntityDefinitionAbstract> sed = prd.getEntityDefinitions();
+ if (sed == null || sed.isEmpty()) {
+ return EvaluationDefinition.ERROR_ENTITYDEFINITION;
+ }
+ }
+ }
+
+ // Verify ES Assignemnt
+ if (ed.getEvaluationSubjects() == null
+ || ed.getEvaluationSubjects().isEmpty()) {
+ return EvaluationDefinition.ERROR_EVALUATIONSUBJECT;
+ }
+
+ // Verify EDef to ES association
+
+ for (EvaluationSubject es : ed.getEvaluationSubjects()) {
+ if (false && (es.getEntityDefinitions() == null
+ || es.getEntityDefinitions().isEmpty())) {
+ return EvaluationDefinition.ERROR_EDEF_ES;
+ }
+ if (es.getEventActivities() != null) {
+ for (EventActivity ea : es.getEventActivities()) {
+ if (ed.getDateEnd().before(ea.getEndDate())
+ || ed.getDateStart().after(ea.getEndDate())) {
+ return EvaluationDefinition.ERROR_DATE_DEADLINES;
+ }
+ }
+ }
+ }
+
+ return 0;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#deleteEDefs(java
+ * .util.Set)
+ */
+ public boolean deleteEDefs(Set<Long> idsEDef, boolean verifyAsso) {
+
+
+ List<EntityDefinition> ledefs;
+ try {
+ ledefs = destroyEDefLinkedObjects(idsEDef, verifyAsso);
+ // Effective deletation
+ // Need to be after SI deletation because of flush in
+ // removeParticipants method.
+
+ for (EntityDefinition edef : ledefs) {
+ //edef.getEvaluationDefinition().getEntityDefinitions().remove(edef);
+ edef.getProcessRoleDefinition().getEntityDefinitions().remove(edef);
+ if (logger.isDebugEnabled()){
+ logger.debug("Effective delete EDef : " + edef.getId() + " name = "+ edef.getName());
+ }
+ entityDefinitionDAO.delete(edef);
+ }
+ } catch (AssociationException e) {
+ // On ne doit pas suppirmer.
+
+ logger.debug("AssociationException");
+
+
+ return false;
+ }
+
+
+
+ return true;
+
+ }
+
+
+ /**
+ * Verify association when deleting.
+ *
+ * @param ED the eD
+ * @param sed the sed
+ * @throws AssociationException the association exception
+ */
+ private void verifyAssociationWhenDeleting(EvaluationDefinition ED, Set<EntityDefinition> sed) throws AssociationException{
+
+
+ String role = "";
+ Map<String, Set<String>> m = new HashMap<String, Set<String>>();
+ for (EntityDefinition edef : sed){
+
+
+
+ Set<String> uids = new HashSet<String>();
+ for (User u : edef.getUsers()){
+ uids.add(u.getUid());
+ }
+
+ role = edef.getProcessRoleDefinition().getName();
+
+ if (!m.containsKey(role)){
+ m.put(role, new HashSet<String>());
+ }
+
+
+ m.get(role).addAll(uids);
+ }
+
+
+ for (EvaluationInstance EI : ED.getEvaluationInstances()) {
+
+ for (ScenarioInstance si : EI.getScenarioInstances()) {
+
+
+ for (Entry<String, Set<String>> e : m.entrySet()){
+
+ role = e.getKey();
+
+
+ Set<String> ss = instanceManager.getUsersForRole(si.getId().toString(), role);
+
+ if (ss.removeAll(e.getValue())){
+ throw new AssociationException("Suppression impossible car le SI est actuellement associe.");
+ }
+
+
+ }
+ }
+ }
+
+ }
+
+
+
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#destroyEDefLinkedObjects(java.util.Set, boolean)
+ */
+ public List<EntityDefinition> destroyEDefLinkedObjects(Set<Long> idsEDef, boolean verifyAsso) throws AssociationException {
+
+ // We need to build a List (ordered) EDefs
+ // First Edef associated with initrole and then the others
+ // Edefs because the edefsInit delete some SI and EI object
+ // and EDef other update it only. So, We can't delete an object and
+ // update it after
+ // that (Hibernate consideration too : Object will be resaved by
+ // cascade).
+
+
+ Set<String> processInstanceIds = new HashSet<String>();
+
+ List<EntityDefinition> ledefs = new ArrayList<EntityDefinition>();
+
+ Set<EntityDefinition> edefsInit = new HashSet<EntityDefinition>();
+ Set<EntityDefinition> edefs = new HashSet<EntityDefinition>();
+
+
+ EvaluationDefinition ED = null;
+
+
+
+ for (Long id : idsEDef) {
+
+ if(logger.isDebugEnabled()){
+ logger.debug("destryEdefLickedObject edefid : " + id);
+ }
+ EntityDefinition EDef = entityDefinitionDAO.get(id);
+
+ if (ED == null){
+
+ ScenarioDefinition SD = scenarioDefinitionDAO.get(EDef.getProcessRoleDefinition()
+ .getPid().getScenarioDefinitionAbstract().getId());
+ ED = SD.getEvaluationDefinition();
+
+ if (logger.isDebugEnabled()){
+ logger.debug("destryEdefLickedObject ED is NULL compute after : " + ED.getName());
+ }
+ }
+
+ if (EDef.isAssociateWithInitRoleDefinition()) {
+ if (logger.isDebugEnabled()){
+ logger.debug("EDef is asso with INITROLE");
+ }
+ edefsInit.add(EDef);
+ } else {
+ if (logger.isDebugEnabled()){
+ logger.debug("EDef is NOT asso with INITROLE");
+ }
+ edefs.add(EDef);
+ }
+ }
+ ledefs.addAll(edefs);
+ ledefs.addAll(edefsInit);
+
+ if (verifyAsso){
+ if (logger.isDebugEnabled()){
+ logger.debug("Verify Asso when Deleting ....");
+ }
+ verifyAssociationWhenDeleting(ED, edefs);
+ }
+ if (logger.isDebugEnabled()){
+ logger.debug("Verify done or not Verified");
+ }
+ for (EntityDefinition edef : ledefs) {
+
+ if (edef != null) {
+
+
+
+ if (edef.isAssociateWithInitRoleDefinition()) {
+
+ if (logger.isDebugEnabled()){
+ logger.debug("Edef is Associate with a INITROLE");
+ }
+ Long edId = ED.getId();
+
+ Set<String> suid = new HashSet<String>();
+ for (User user : edef.getUsers()) {
+ if (logger.isDebugEnabled()){
+ logger.debug("Edef users are : " + user.getUid() + " " + user.getLastname() + " " + user.getFirstname());
+ }
+
+ suid.add(user.getUid());
+ }
+
+ if (edef.getEvaluationInstances().isEmpty()) {
+ if (logger.isDebugEnabled()){
+ logger.debug("Deleting SI for Users because EIs is Empty");
+ }
+ instanceManager.deleteScenarioInstancesForUsers(suid,
+ edId, edef.getId());
+ } else {
+ if (logger.isDebugEnabled()){
+ logger.debug("Deleting EI for Edef.getEvalautionInstances");
+ }
+ instanceManager.deleteEvaluationInstances(edef
+ .getEvaluationInstances());
+ }
+ for (RessourceDefinition rd : ED
+ .getRelatedRessourceDefinitions()) {
+
+
+ ressourceManager.removePermission(rd, suid, "");
+ }
+ } else {
+ if (logger.isDebugEnabled()){
+ logger.debug("Edef is NOT Associate with a INITROLE");
+ }
+ Set<String> suid = new HashSet<String>();
+ for (User user : edef.getUsers()) {
+ if (logger.isDebugEnabled()){
+ logger.debug("Edef users are : " + user.getUid() + " " + user.getLastname() + " " + user.getFirstname());
+ }
+ suid.add(user.getUid());
+ }
+
+ Map<String, Set<Entity>> m = new HashMap<String, Set<Entity>>();
+
+ m.put(edef.getProcessRoleDefinition().getName(), new HashSet<Entity>(edef
+ .getUsers()));
+
+ for (EvaluationInstance EI : ED.getEvaluationInstances()) {
+
+ for (ScenarioInstance si : EI.getScenarioInstances()) {
+ String piid = si.getProcessInstanceId();
+ boolean ok = instanceManager.removeParticipants(si, m);
+ if (ok){
+ if (logger.isDebugEnabled()){
+ logger.debug("ProcessInstanceId must be refreshing Assignation is : " + piid);
+ }
+ processInstanceIds.add(piid);
+ }
+ }
+ }
+
+ for (RessourceDefinition rd : ED
+ .getRelatedRessourceDefinitions()) {
+ ressourceManager.removePermission(rd, suid, "Eval");
+ }
+ }
+
+ }
+ }
+
+ //evaluationDefinitionDAO.flush();
+ for (String piid : processInstanceIds){
+ workflowManager.refreshAssignation(piid);
+ }
+
+ return ledefs;
+
+ }
+
+
+
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#deleteESs(java.
+ * util.Set)
+ */
+ public Set<Long> deleteESs(Set<Long> idsES) {
+
+ Set<Long> EDefIds = new HashSet<Long>();
+
+ for (Long id : idsES) {
+
+ EvaluationSubject es = evaluationSubjectDAO.get(id);
+
+ boolean entityLock = es.getEvaluationDefinition().getPlan()
+ .getEntityLock();
+
+ es.getEvaluationDefinition().getEvaluationSubjects().remove(es);
+
+ Set<ScenarioInstance> ssi = new HashSet<ScenarioInstance>();
+ for (EvaluationSubjectInstance ESI : es
+ .getEvaluationSubjectInstances()) {
+ ssi.add(ESI.getScenarioInstance());
+ }
+
+ instanceManager.deleteScenarioInstances(ssi);
+
+ Set<String> inituids = new HashSet<String>();
+ Set<String> others = new HashSet<String>();
+
+ for (EntityDefinition edef : es.getEvaluationDefinition()
+ .getEntityDefinitions()) {
+ for (User u : edef.getUsers()) {
+ if (edef.isAssociateWithInitRoleDefinition()) {
+ inituids.add(u.getUid());
+ } else {
+ others.add(u.getUid());
+ }
+ }
+ }
+
+ Set<Long> ids = new HashSet<Long>();
+
+ for (EntityDefinition EDef : es.getEntityDefinitions()) {
+ if (EDef.getEvaluationSubjects().size() == 1) {
+ EDefIds.add(EDef.getId());
+
+ }
+ EDef.getEvaluationSubjects().remove(es);
+ ids.add(EDef.getId());
+
+ }
+
+
+
+ for (RessourceDefinition rd : es.getRessourceDefinitions()){
+ ressourceManager.removeRessourceDefinition(rd);
+ }
+
+
+
+ evaluationSubjectDAO.delete(es);
+
+ if (entityLock) {
+ deleteEDefs(ids, false);
+ } else {
+
+ try {
+ this.destroyEDefLinkedObjects(ids, false);
+ } catch (AssociationException e) {
+
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+ return EDefIds;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinition#
+ * deleteEvaluationDefinitions(java.util.Set)
+ */
+ public void deleteEvaluationDefinitions(Set<Long> defid) {
+
+ for (Long did : defid) {
+ // EvaluationDefinition ed = new EvaluationDefinition();
+ // ed.setId(did);
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(did);
+
+
+ Set<Long> edefIds = new HashSet<Long>();
+ new HashSet<Long>();
+ for (EntityDefinition edef : ed.getEntityDefinitions()) {
+
+ edefIds.add(edef.getId());
+
+ }
+ this.deleteEDefs(edefIds, false);
+ // this.deleteEDefs(edefIdsInitRole);
+ ed = evaluationDefinitionDAO.get(ed.getId());
+
+ // Delete left potential ghost SIs
+ for (EvaluationInstance ei : ed.getEvaluationInstances()) {
+ instanceManager.deleteScenarioInstances(ei
+ .getScenarioInstances());
+ }
+
+ evaluationDefinitionDAO.delete(ed);
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#deleteSD(java.lang
+ * .Long)
+ */
+ public boolean deleteSD(Long idSD) {
+
+ ScenarioDefinition sd = scenarioDefinitionDAO.get(idSD);
+
+ scenarioDefinitionDAO.delete(sd);
+ return true;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinitionBase#
+ * editAssociationTable(java.util.Map)
+ */
+ public void editAssociationTable(Map<Long, Map<Long, Boolean>> map) {
+
+ Set<Long> idESs = new HashSet<Long>();
+
+ for (Entry<Long, Map<Long, Boolean>> m : map.entrySet()) {
+ Long idEDef = m.getKey();
+ Map<Long, Boolean> mm = m.getValue();
+ for (Entry<Long, Boolean> mmm : mm.entrySet()) {
+ Long idES = mmm.getKey();
+ Boolean associted = mmm.getValue();
+
+ idESs.add(idES);
+
+ if (associted) {
+ associateEDefToES(idEDef, idES);
+ } else {
+ removeEDefToES(idEDef, idES);
+ }
+ }
+ }
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinition#
+ * editEvaluationDefinition
+ * (com.pentila.evalcomp.domain.definition.EvaluationDefinition)
+ */
+ public EvaluationDefinition editEvaluationDefinition(
+ EvaluationDefinition edd) {
+ // TODO : Must translate this method
+ evaluationDefinitionDAO.evict(edd);
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(edd.getId());
+
+ boolean changeDE = !ed.getDateEnd().equals(edd.getDateEnd());
+ boolean changeDS = !ed.getDateStart().equals(edd.getDateStart());
+
+ Date now = new Date();
+ boolean dok = edd.getDateStart().before(now)
+ && now.before(edd.getDateEnd());
+
+ ed.setDateEnd(edd.getDateEnd());
+ ed.setDateStart(edd.getDateStart());
+ ed.setDescription(edd.getDescription());
+ ed.setName(edd.getName());
+ ed.setNotifyDate(edd.getNotifyDate());
+ ed.setState(edd.getState());
+ ed.setMethodEval(edd.getMethodEval());
+ ed.setPlan(edd.getPlan());
+
+ if (ed.isActive() && dok && (changeDE || changeDS)) {
+
+ SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
+
+ String subject = "Modification de période d'évaluation";
+ String body = "";
+
+ if (changeDE && changeDS) {
+ body = "La période d'évaluation de la campagne " + ed.getName()
+ + " a été modifiée du " + sdf.format(ed.getDateStart())
+ + " au " + sdf.format(ed.getDateEnd());
+ } else if (changeDE) {
+ body = "La date de fin de la campagne " + ed.getName()
+ + " a été modifiée au " + sdf.format(ed.getDateEnd());
+ } else if (changeDS) {
+ body = "La date de début de la campagne " + ed.getName()
+ + " a été modifiée au " + sdf.format(ed.getDateStart());
+ }
+ // Add Evalauteurs
+ Set<User> recipients = new HashSet<User>();
+
+ for (EntityDefinition edef : ed.getOtherUnits()) {
+ recipients.addAll(edef.getUsers());
+ }
+ // Add GP
+ recipients.add(ed.getOwner());
+
+ User sender = entityManager.getCurrentUser();
+ getEntityManager().getMessageManager().notifyByMessage(sender,
+ subject, body, recipients);
+
+ }
+
+ evaluationDefinitionDAO.saveOrUpdate(ed);
+ return ed;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinitionBase#
+ * editEvaluationSubject
+ * (com.pentila.evalcomp.domain.definition.EvaluationSubject)
+ */
+ public EvaluationSubject editEvaluationSubject(EvaluationSubject ES) {
+ // TODO Auto-generated method stub
+ evaluationSubjectDAO.merge(ES);
+ return ES;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#editProperties(
+ * java.util.Set)
+ */
+ public void editProperties(Set<Property> properties) {
+ processInstanciationDefinitionManager.editProperties(properties);
+ }
+
+ /**
+ * Gets the entity definition dao.
+ *
+ * @return the entity definition dao
+ */
+ public IEntityDefinitionDAO getEntityDefinitionDAO() {
+ return entityDefinitionDAO;
+ }
+
+ /**
+ * Gets the entity manager.
+ *
+ * @return the entity manager
+ */
+ public IEntityBase getEntityManager() {
+ return entityManager;
+ }
+
+ /**
+ * Gets the evaluation definition dao.
+ *
+ * @return the evaluation definition dao
+ */
+ public IEvaluationDefinitionDAO getEvaluationDefinitionDAO() {
+ return evaluationDefinitionDAO;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinition#
+ * getEvaluationDefinitions()
+ */
+ public Set<EvaluationDefinition> getEvaluationDefinitions() {
+
+ return evaluationDefinitionDAO.getAll();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinition#
+ * getEvaluationDefinitions(java.lang.String)
+ */
+ public Set<EvaluationDefinition> getEvaluationDefinitions(String login) {
+
+ Set<Long> sid = (entityManager).getEntitiesOfUser(login);
+
+ return evaluationDefinitionDAO.getEvaluationDefinitions(sid);
+
+ }
+
+ /**
+ * Gets the evaluation object manager.
+ *
+ * @return the evaluation object manager
+ */
+ public IEvaluationObjectBase getEvaluationObjectManager() {
+ return evaluationObjectManager;
+ }
+
+ /**
+ * Gets the evaluation subject dao.
+ *
+ * @return the evaluation subject dao
+ */
+ public IEvaluationSubjectDAO getEvaluationSubjectDAO() {
+ return evaluationSubjectDAO;
+ }
+
+ /**
+ * Gets the event activity dao.
+ *
+ * @return the event activity dao
+ */
+ public IEventActivityDAO getEventActivityDAO() {
+ return eventActivityDAO;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#getEvents(java
+ * .lang.String)
+ */
+ public List<Event> getEvents(String login) {
+ Set<Long> sid = entityManager.getEntitiesOfUser(login);
+
+ return evaluationDefinitionDAO.getEvents(sid);
+ }
+
+ /**
+ * Gets the instance manager.
+ *
+ * @return the instance manager
+ */
+ public IInstanceBase getInstanceManager() {
+ return instanceManager;
+ }
+
+ /**
+ * Gets the instances table for csv export.
+ *
+ * @param idED
+ * the EvaluationDefinition id
+ *
+ * @return the instances table for csv export.
+ */
+ public TCompositeDataMap getInstancesTableExport(Long idED){
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idED);
+ if (ed == null){
+ return null;
+
+ }
+
+ SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
+
+ // Définition des comparateurs qui servirons à trier les listes
+ Comparator<User> userComparator = 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);
+ }
+ };
+
+ Comparator<Competence> competenceComparator = new Comparator<Competence>() {
+ public int compare(Competence c1, Competence c2) {
+ return c1.getName().compareToIgnoreCase(c2.getName());
+ }
+ };
+
+ Comparator<EvaluationObject> evaluationObjectComparator = new Comparator<EvaluationObject>() {
+ public int compare(EvaluationObject eo1, EvaluationObject eo2) {
+ return eo1.getName().compareToIgnoreCase(eo2.getName());
+ }
+ };
+
+ // on récupère la liste des utilisateurs en rapport avec la campagne dans un SET
+ // (garanti qu'on aura pas de doublon)
+ Set<User> userSet = new HashSet<User>();
+
+ for (EntityDefinition entityDef : ed.getEntityDefinitions() ){
+
+ if (entityDef.isAssociateWithInitRoleDefinition()){
+ for(Entity entity : entityDef.getEntites() ){
+ for(User user : entity.getUsers()){
+ userSet.add(user);
+ }
+ }
+ }
+ }
+
+ // On passe les users dans un List pour pouvoir les trier par ordre alphabétique
+ List<User> users = new ArrayList<User>();
+ for(User u : userSet){
+ users.add(u);
+ }
+ Collections.sort(users, userComparator);
+
+ // HashMap à 3 clef -> une valeur. La valeur correspond à la ligne dans la table de sortie
+ // pour chaque triplet user/es/competence
+ // <userId, <esId, <competenceId, index>>>
+ Map<Long, Map<Long, Map<Long, Integer>>> map = new HashMap<Long, Map<Long, Map<Long, Integer>>>();
+
+ Integer numligne = 0;
+
+ // peuplement du hashmap
+ for(User u : users){
+
+ // création d'une clef pour l'user courant
+ map.put(u.getId(), new HashMap<Long, Map<Long, Integer>>());
+
+ for(EvaluationSubject es : ed.getEvaluationSubjects()){
+
+ // création d'une clef pour l'ES courant
+ map.get(u.getId()).put(es.getId(), new HashMap<Long, Integer>());
+
+ // tri des evaluationSubject par ordre alphabétique
+ List<EvaluationObject> esSorted= new ArrayList<EvaluationObject>();
+ for(EvaluationObject eo : es.getEvaluationObjects()){
+ esSorted.add(eo);
+ }
+ Collections.sort(esSorted, evaluationObjectComparator);
+
+
+ for(EvaluationObject eo : esSorted){
+
+ // tri des compétences par ordre alphabétique
+ List<Competence> eoSorted= new ArrayList<Competence>();
+ for(Competence c : eo.getAllCompetences()){
+ eoSorted.add(c);
+ }
+ Collections.sort(eoSorted, competenceComparator);
+
+ // parcours de la liste trié
+ for(Competence c : eoSorted){
+
+ // on peuple le map pour le bon user et le bon ES, pour la competence c
+ map.get(u.getId()).get(es.getId()).put(c.getId(), numligne);
+
+ if (logger.isDebugEnabled()){
+ logger.debug(u.getId() + "/" + es.getId() + "/" + c.getId()
+ + "=" + numligne + " (" + c.getName() + ")");
+ }
+
+ numligne++;
+
+ }
+
+ }
+
+ }
+
+ }
+
+
+ // on compte le nombre de compétences évaluées dans la campagne
+ Integer nbCompetence = 0;
+ for(EvaluationSubject es : ed.getEvaluationSubjects()){
+ for(EvaluationObject eo : es.getEvaluationObjects()){
+ nbCompetence += eo.getAllCompetences().size();
+ }
+ }
+
+
+ String[] header = { "uid", "nom candidat", "prenom candidat", "idDossier", "dossier",
+ "activities", "competence", "domaine", "referentiel", "uidEvaluateur",
+ "evaluateur", "level", "idExamen", "examen", "idsQuestions",
+ "questions", "scores questions" };
+
+ // initialisation partielle de la table
+ String[][] table = new String[users.size()*nbCompetence][header.length];
+
+ for(User u : users){
+
+ for(EvaluationSubject es : ed.getEvaluationSubjects()){
+
+ for(EvaluationObject eo : es.getEvaluationObjects()){
+
+ for(Competence c : eo.getAllCompetences()){
+
+ Integer index = map.get(u.getId()).get(es.getId()).get(c.getId());
+
+ table[index][0] = u.getUid().toString();
+ table[index][1] = u.getLastname();
+ table[index][2] = u.getFirstname();
+ table[index][3] = ""; //idDossier
+ table[index][4] = ""; //dossier
+ table[index][5] = ""; //activities
+ table[index][6] = c.getName();
+ table[index][7] = c.getDomain().getName();
+ table[index][8] = c.getDomain().getReferentiel().getName();
+ table[index][9] = ""; //uidEvaluateur
+ table[index][10] = ""; //evaluateur
+ table[index][11] = ""; //level
+ table[index][12] = ""; //idExamen
+ table[index][13] = ""; //examen
+ table[index][14] = ""; //idQuestion
+ table[index][15] = ""; //question
+ table[index][16] = ""; //score
+ index++;
+
+ }
+
+ }
+
+ }
+
+ }
+
+ // Remplissage du reste de la table
+ for(EvaluationSubject es : ed.getEvaluationSubjects()){
+ for(EvaluationSubjectInstance esi : es.getEvaluationSubjectInstances()){
+
+ // Recuperation des étapes actives du scénario en utilisant l'api bonita
+ Process p = workflowManager.getProcess("", esi.getScenarioInstance().getProcessInstanceId());
+ InstanceUtil util = new InstanceUtil();
+
+ p = util.applyContext(p, null, null);
+
+ int j = 0;
+ String currentActivities = "";
+
+ for(Activity act : p.getActivities()){
+
+ ExtendActivity ea = act.getExtActivity();
+ Date lastModif = act.getReadyDate();
+
+ Service serv = ea.getService();
+ currentActivities += MessagesI18n.getString(serv.getTitlei18n())
+ + " (" + sdf.format(lastModif) + ")";
+
+ j++;
+ if (j < p.getActivities().size()){
+ currentActivities += " | ";
+ }
+ }
+
+ if (currentActivities == null || currentActivities.equals("")){
+ currentActivities = MessagesI18n.getString("Aucune")
+ + " (" + MessagesI18n.getString("Dossier_transmis")+")";
+ }
+
+
+ // pour chaque compétence instance
+ for(CompetenceInstance CI : esi.getCompetenceInstances()){
+
+ Long ownerID = CI.getOwner().getId();
+ Long competenceID = CI.getCompetence().getId();
+ Long esID = es.getId();
+
+ // on récupère l'index (la ligne du fichier) correspondant à la compétence courante
+ Integer i = map.get(ownerID).get(esID).get(competenceID);
+
+ if (logger.isDebugEnabled()){
+ logger.debug("ownerID=" + ownerID + ", competenceID=" + competenceID
+ + ", esID=" + esID + " -> ligne = " + i
+ + "(" + CI.getCompetence().getName() + ")");
+ }
+
+ String level = "";
+ if (CI.getLevel().getInitial()){
+ level = "-1";
+ }
+ else if(CI.getLevel().isGreaterThanThreshold() || CI.getLevel().isThreshold()){
+ level = "1";
+ }
+ else{
+ level = "0";
+ }
+
+ //{"uidUser", "username", "idDossier", "dossier", "activities", "competence", "domaine", "référentiel", "uidEvaluateur", "evaluateur", "level", "idExamen", "examen", "idQuestion", "question", "score" }
+
+ // id du dossier (evaluationSubjectInstance)
+ table[i][3] = esi.getId().toString();
+
+ //nom du dossier (evaluationSubjectInstance)
+ if (esi.getName() != null){
+ table[i][4] = esi.getName();
+ }
+
+ table[i][5] = currentActivities;
+
+ // Il est possible qu'une CI n'ai pas encore d'évaluateur
+ if (CI.getEvaluateur() != null){
+ table[i][9] = CI.getEvaluateur().getUid().toString();
+ table[i][10] = CI.getEvaluateur().getLastname() + " " + CI.getEvaluateur().getFirstname();
+ }
+
+ table[i][11] = level;
+ }
+
+
+ // Dans le cas où le scénario est un examen, on remplit les colonnes supplémentaires
+ ExamenInstance examenInstance = esi.getExamenInstance();
+
+ if (examenInstance != null){
+
+ for(QuestionInstance qi : examenInstance.getQuestionInstances()){
+
+
+ Long ownerID = examenInstance.getOwner().getId();
+ Long esID = es.getId();
+ Long competenceID = qi.getQuestion().getCompetence().getId();
+
+ /* on récupère l'index (la ligne du fichier) correspondant à la compétence relative
+ * à la question instance */
+ Integer i = map.get(ownerID).get(esID).get(competenceID);
+
+ if (logger.isDebugEnabled()){
+ logger.debug("ownerID=" + ownerID + ", competenceID=" + competenceID
+ + ", esID=" + esID + " -> ligne = " + i + "(" +
+ qi.getQuestion().getCompetence().getName() + ")");
+ }
+
+ // id de l'examen
+ table[i][12] = String.valueOf(examenInstance.getExamenDef().getExamen().getId_examen());
+
+ // nom de l'examen
+ table[i][13] = examenInstance.getExamenDef().getName();
+
+ /* Il peut y avoir plusieurs questions portant sur la même compétence,
+ * dans ce cas on concatennes les idQuestions / question / résultat dans le même ordre */
+ table[i][14] += qi.getQuestion().getId().toString() + " ";
+ table[i][15] += qi.getQuestion().getTitle() + " ";
+ table[i][16] += qi.getScore().toString() + " ";
+
+ }
+
+ }
+
+ }
+ }
+
+ if (logger.isDebugEnabled()){
+ for(int i=0; i < table.length; i++){
+ String s = "";
+ for(int j=0; j < table[i].length; j++){
+ s += table[i][j] + ";";
+ }
+ logger.debug(s);
+ }
+ }
+
+ TCompositeDataMap tc = new TCompositeDataMap();
+
+ // On utilise le String header pour remplir le header du tc
+ for(int i = 0; i< header.length; i++){
+ tc.addItemInfo(header[i], header[i], null);
+ }
+
+ tc.setDataMap(table);
+
+ return tc;
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#getInstancesTable
+ * (java.lang.Long)
+ */
+ public TCompositeDataMap getInstancesTable(Long idED) {
+
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idED);
+
+ ScenarioDefinition sd = ed.getScenarioDefinitions().iterator().next();
+
+ String INIT_ROLE = sd.getInitRole();
+
+ boolean isExamen = sd.getType() != null && sd.getType().endsWith("-EXAMEN");
+
+ int size = 0;
+
+ for (EvaluationInstance EI : ed.getEvaluationInstances()) {
+ size = size + EI.getScenarioInstances().size();
+ }
+
+ Comparator<User> usercomp = new Comparator<User>() {
+ public int compare(User u1, User u2) {
+
+ String username1 = u1.getLastname() + " " + u1.getFirstname();
+ String username2 = u2.getLastname() + " " + u2.getFirstname();
+ return username1.compareTo(username2);
+ }
+ };
+
+ String[][] table = new String[size][];
+
+ int i = 0;
+
+
+ for (EvaluationInstance EI : ed.getEvaluationInstances()) {
+ for (ScenarioInstance SI : EI.getScenarioInstances()) {
+ String SIID = SI.getId().toString();
+
+ EvaluationSubject ES = SI.getEvaluationSubjectInstance()
+ .getEvaluationSubject();
+
+ // Si l'ESI n'a pas encore de CompetenceInstance : on prend le
+ // prochain SI
+ float state = -1;
+ if (!isExamen && !SI.getEvaluationSubjectInstance().getCompetenceInstances()
+ .isEmpty()) {
+
+
+ int nb_transmis = 0;
+ int total = SI.getEvaluationSubjectInstance()
+ .getCompetenceInstances().size();
+ for (CompetenceInstance ci : SI.getEvaluationSubjectInstance()
+ .getCompetenceInstances()) {
+ if (ci.getState() != 0) {
+ nb_transmis++;
+ }
+
+ }
+
+ state = (float)(nb_transmis / total) * 100;
+ }else if (isExamen && SI.getEvaluationSubjectInstance().getExamenInstance() != null && SI.getEvaluationSubjectInstance().getExamenInstance().getFinished()!=null ){
+ if (SI.getEvaluationSubjectInstance().getExamenInstance().getFinished()){
+ state = 100.0f;
+ }else{
+ state = 0.0f;
+ }
+ }
+ String nameES = getName(ES);
+
+ int delay = SI.getDelay();
+
+ // getUserForRole role = INIT_ROLE , not = false
+ Set<String> suid_INIT = SI.getUserForRole(INIT_ROLE, false);
+
+ // getUserForRole role = INIT_ROLE , not = true
+ // given users with other roles than INIT_ROLE
+ Set<String> suid_OTHER = SI.getUserForRole(INIT_ROLE, true);
+
+ String cs = "";
+ String es = "";
+ String uids = "";
+ String evalIds = "";
+
+ String idRefs = "";
+ String nameRefs = "";
+
+ Map<Long, String> mapRef = new HashMap<Long, String>();
+
+ for (EvaluationObject ces : ES.getEvaluationObjects()){
+
+ Referentiel referentiel = evaluationObjectManager.getReferentiel(ces.getIdRef());
+
+
+ mapRef.put(referentiel.getId(), referentiel.getName());
+ }
+
+ Iterator<Entry<Long, String>> it = mapRef.entrySet().iterator();
+
+ while (it.hasNext()){
+
+ Entry<Long, String> entry = it.next();
+
+ Long idRef = entry.getKey();
+ String nameRef = entry.getValue();
+
+ idRefs = idRefs + idRef;
+ nameRefs = nameRefs + nameRef;
+ if (it.hasNext()){
+ idRefs = idRefs + ":";
+ nameRefs = nameRefs + ":";
+ }
+
+
+ }
+
+
+ Set<User> su1 = entityManager.getUsersByUids(suid_INIT);
+
+ List<User> lcs = new ArrayList<User>();
+
+ lcs.addAll(su1);
+
+ List<User> les = new ArrayList<User>();
+
+ Set<User> su2 = entityManager.getUsersByUids(suid_OTHER);
+ les.addAll(su2);
+
+ Collections.sort(lcs, usercomp);
+ Collections.sort(les, usercomp);
+
+ Iterator<User> iu1 = lcs.iterator();
+ while (iu1.hasNext()) {
+ User u = iu1.next();
+ cs = cs + u.getLastname() + " " + u.getFirstname();
+
+ uids = uids + u.getUid();
+
+ if (iu1.hasNext()) {
+ cs = cs + ":";
+ uids = uids + ":";
+ }
+ }
+
+ Iterator<User> iu2 = les.iterator();
+ while (iu2.hasNext()) {
+ User u = iu2.next();
+ es = es + u.getLastname() + " " + u.getFirstname();
+ evalIds = evalIds + u.getId().toString();
+ if (iu2.hasNext()) {
+ es = es + ":";
+ evalIds = evalIds + ":";
+ }
+ }
+
+ String folderName = SI.getEvaluationSubjectInstance().getName();
+ String folderDescription = SI.getEvaluationSubjectInstance().getDescription();
+ String ESIID = SI.getEvaluationSubjectInstance().getId().toString();
+
+ String examInstanceId = "";
+
+ if (SI.getEvaluationSubjectInstance().getExamenInstance() != null){
+ examInstanceId = SI.getEvaluationSubjectInstance().getExamenInstance().getId().toString();
+ }
+
+ table[i] = new String[] { cs, es, "" + delay, "" + state,
+ nameES, SIID, ESIID , folderName, folderDescription, uids, evalIds, idRefs, nameRefs, examInstanceId};
+ i++;
+ }
+ }
+
+
+ TCompositeDataMap tc = new TCompositeDataMap();
+
+ tc.addItemInfo("username", "username", null);
+ tc.addItemInfo("evaluateurName", "evaluateurName", null);
+ tc.addItemInfo("delay", "delay", null);
+ tc.addItemInfo("state", "state", null);
+ tc.addItemInfo("ES", "ES", null);
+ tc.addItemInfo("SIID", "SIID", null);
+ tc.addItemInfo("ESIID", "ESIID", null);
+ tc.addItemInfo("folderName", "folderName", null);
+ tc.addItemInfo("folderDescription", "folderDescription", null);
+ tc.addItemInfo("uids", "uids", null);
+ tc.addItemInfo("evalIds", "evalIds", null);
+ tc.addItemInfo("idRefs", "idRefs", null);
+ tc.addItemInfo("nameRefs", "nameRefs", null);
+ tc.addItemInfo("examInstanceId", "examInstanceId", null);
+ tc.setDataMap(table);
+ return tc;
+ }
+
+ /**
+ * Gets the map entities.
+ *
+ * @param se
+ * the se
+ *
+ * @return the map entities
+ */
+ private Map<Long, Entity> getMapEntities(Set<Entity> se) {
+ Map<Long, Entity> mp = new HashMap<Long, Entity>();
+
+ for (Entity e : se) {
+ mp.put(e.getId(), e);
+ }
+ return mp;
+ }
+
+ /**
+ * Gets the map users.
+ *
+ * @param se
+ * the se
+ *
+ * @return the map users
+ */
+ private Map<Long, User> getMapUsers(Set<User> se) {
+ Map<Long, User> mp = new HashMap<Long, User>();
+
+ for (User e : se) {
+ mp.put(e.getId(), e);
+ }
+ return mp;
+ }
+
+ /**
+ * Gets the method eval manager.
+ *
+ * @return the method eval manager
+ */
+ public IMethodEvalBase getMethodEvalManager() {
+ return methodEvalManager;
+ }
+
+ /**
+ * Gets the name.
+ *
+ * @param es
+ * the es
+ *
+ * @return the name
+ */
+ private String getName(EvaluationSubject es) {
+
+ String name = ""; //$NON-NLS-1$
+
+ if (es.getName() != null) {
+ name = es.getName();
+ } else {
+ List<EvaluationObject> listEO = new ArrayList<EvaluationObject>(es
+ .getEvaluationObjects());
+ Collections.sort(listEO, new Comparator<EvaluationObject>() {
+ public int compare(EvaluationObject arg0, EvaluationObject arg1) {
+
+ return arg0.getName().compareTo(arg1.getName());
+ }
+ });
+
+ for (EvaluationObject obj : listEO) {
+ if (!name.equals("")) {
+ name = name + "/";
+ }
+
+ name = name + obj.getName();
+ }
+ }
+ return name;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinition#
+ * getOwnedEvaluationDefinitions(java.lang.String)
+ */
+ public Set<EvaluationDefinition> getOwnedEvaluationDefinitions(String login) {
+ return evaluationDefinitionDAO.getOwnedEvaluationDefinitions(login);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinitionBase#
+ * getPartedInstanciation(java.lang.Integer,
+ * com.pentila.evalcomp.domain.definition.ScenarioDefinition, java.util.Set)
+ */
+ public List<Map<Set<User>, EntityDefinition>> getPartedInstanciation(
+ Integer instanciationMode, ScenarioDefinition sdt,
+ Set<EntityDefinition> sEDef) {
+
+ List<Map<Set<User>, EntityDefinition>> map = new ArrayList<Map<Set<User>, EntityDefinition>>();
+
+ Map<Set<User>, EntityDefinition> mapOfSetOfUser = null;
+
+ int instMode = EvaluationDefinition.MODE_INDIVIDUAL;
+
+ if (instanciationMode != null) {
+ instMode = instanciationMode;
+ }
+
+ Integer sdInstMode = sdt.getInstanciationMode();
+
+ if (sdInstMode != null && sdInstMode != EvaluationDefinition.MODE_NONE) {
+ instMode = sdt.getInstanciationMode();
+ }
+
+ Set<User> collective = new HashSet<User>();
+ for (EntityDefinition EDef : sEDef) {
+
+ mapOfSetOfUser = new HashMap<Set<User>, EntityDefinition>();
+
+ switch (instMode) {
+ case EvaluationDefinition.MODE_INDIVIDUAL:
+ for (User U : EDef.getUsers()) {
+
+ Set<User> s = new HashSet<User>();
+ s.add(U);
+ mapOfSetOfUser.put(s, EDef);
+ }
+ map.add(mapOfSetOfUser);
+ break;
+ case EvaluationDefinition.MODE_ENTITYDEFINITION:
+ mapOfSetOfUser.put(EDef.getUsers(), EDef);
+ map.add(mapOfSetOfUser);
+ break;
+ case EvaluationDefinition.MODE_COLLECTIVE:
+ collective.addAll(EDef.getUsers());
+ break;
+ }
+ }
+
+ if (instMode == EvaluationDefinition.MODE_COLLECTIVE) {
+ mapOfSetOfUser.put(collective, null);
+ map.add(mapOfSetOfUser);
+ }
+
+ return map;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#getProcessDefinitions
+ * ()
+ */
+ public Set<TransitProcessDefinition> getProcessDefinitions() {
+
+ User currentUser = entityManager.getCurrentUser();
+
+ boolean manager = currentUser.hasRole("ROLE_MANAGER");
+
+ boolean peda = currentUser.hasRole("ROLE_PEDA");
+
+ Set<String> types = new HashSet<String>();
+
+ if (peda) {
+ types.add(ScenarioDefinition.EVALUATION);
+ types.add(ScenarioDefinition.EVALUATION_EXAMEN);
+ }
+
+ if (manager) {
+
+ // All types are returned so set values to null
+ types = null;
+ }
+
+ return workflowManager.getProcessDefinitions(types);
+
+ }
+
+ /**
+ * Gets the process instanciation definition manager.
+ *
+ * @return the process instanciation definition manager
+ */
+ public IProcessInstanciationDefinitionBase getProcessInstanciationDefinitionManager() {
+ return processInstanciationDefinitionManager;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#getReferentiel
+ * (java.lang.Long)
+ */
+ public Referentiel getReferentiel(Long idref) {
+
+ return evaluationObjectManager.getReferentiel(idref);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinitionBase#
+ * getRelatedEntityDefinitions(java.util.Set)
+ */
+ public Set<EntityDefinition> getRelatedEntityDefinitions(
+ Set<Long> sidEntities) {
+ return entityDefinitionDAO.getRelatedEntityDefinitions(sidEntities);
+ }
+
+ /**
+ * Gets the ressource manager.
+ *
+ * @return the ressource manager
+ */
+ public IRessourceBase getRessourceManager() {
+ return ressourceManager;
+ }
+
+ /**
+ * Gets the scenario definition dao.
+ *
+ * @return the scenario definition dao
+ */
+ public IScenarioDefinitionDAO getScenarioDefinitionDAO() {
+ return scenarioDefinitionDAO;
+ }
+
+ /**
+ * Gets the workflow manager.
+ *
+ * @return the workflow manager
+ */
+ public IWorkflowBase getWorkflowManager() {
+ return workflowManager;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#instanciateED(java
+ * .lang.Long)
+ */
+
+ public int instanciateForUserEI(EvaluationInstance EII,
+ User userInstantiation, Set<EvaluationSubject> ses) {
+ if (logger.isDebugEnabled()){
+ logger.debug("User instanciate : " + userInstantiation.getUid());
+ }
+ Set<BonitaProcessLaunch> sbpl = new HashSet<BonitaProcessLaunch>();
+
+ Set<String> inituids = new HashSet<String>();
+
+ EvaluationDefinition ed = EII.getEvaluationDefinition();
+
+ EvaluationInstance EI = EII;//instanceManager.addEvaluationInstance(ed, EII.getEntityDefinition());
+
+ ScenarioDefinition sdt = ed.getScenarioDefinitions().iterator().next();
+
+ Set<User> SU = new HashSet<User>();
+
+ SU.add(userInstantiation);
+
+ Set<EvaluationSubject> sses = null;
+
+ if (ses != null) {
+ sses = ses;
+ } else {
+ sses = ed.getEvaluationSubjects();
+ }
+
+ for (EvaluationSubject ES : sses) {
+ EvaluationSubjectInstance ESI = instanceManager
+ .addEvaluationSubjectInstance(ES, EI);
+ ScenarioInstance SI = instanceManager.addScenarioInstance(EI, sdt,
+ ESI, SU);
+
+ 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));
+
+
+ for (RessourceDefinition rd : ES.getRessourceDefinitions()) {
+ ressourceManager.setPermission(rd, inituids, "View");
+ // ressourceManager.setPermission(rd, others, "Eval");
+ }
+ }
+
+ // NEEDED FOR BONITA INSTANCIATION PROCESS !
+ //evaluationDefinitionDAO.flush();
+
+ instanceManager.instanciateProcesses(sbpl);
+ return 0;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#instanciateED
+ * (java.lang.Long)
+ */
+
+ public int instanciateED(Long idED) {
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idED);
+
+ int control = controlED(ed);
+ if (control != 0) {
+ return control;
+ }
+
+ ed.setState(EvaluationDefinition.RUNNING);
+
+ if (ed.getState() != EvaluationDefinition.RUNNING) {
+ return EvaluationDefinition.ERROR_STATE;
+ }
+
+ evaluationDefinitionDAO.saveOrUpdate(ed);
+
+ Level initLevel = null;
+ if (ed.getMethodEval() != null){
+ initLevel = ed.getMethodEval().getInitialLevel();
+ }
+ Set<BonitaProcessLaunch> sbpl = new HashSet<BonitaProcessLaunch>();
+
+ Set<String> inituids = new HashSet<String>();
+ Set<String> others = new HashSet<String>();
+ for (EntityDefinition edef : ed.getEntityDefinitions()) {
+ for (User u : edef.getUsers()) {
+ if (edef.isAssociateWithInitRoleDefinition()) {
+ inituids.add(u.getUid());
+ } else {
+ others.add(u.getUid());
+ }
+ }
+ }
+
+ for (ScenarioDefinition sdt : ed.getScenarioDefinitions()) {
+
+ List<Map<Set<User>, EntityDefinition>> map = getPartedInstanciation(
+ ed.getInstanciationMode(), sdt, ed.getInstanciationUnits());
+
+
+ for (Map<Set<User>, EntityDefinition> mapOfSetOfUser : map) {
+ for (Entry<Set<User>, EntityDefinition> E : mapOfSetOfUser
+ .entrySet()) {
+ Set<User> SU = E.getKey();
+ EntityDefinition EDef = E.getValue();
+ if (EDef != null) {
+ EDef.getId();
+ }
+
+ EvaluationInstance EI = instanceManager
+ .addEvaluationInstance(ed, EDef);
+
+ for (EvaluationSubject ES : ed.getEvaluationSubjects()) {
+ EvaluationSubjectInstance ESI = instanceManager
+ .addEvaluationSubjectInstance(ES, EI);
+ ScenarioInstance SI = instanceManager
+ .addScenarioInstance(EI, sdt, ESI, SU);
+
+ 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));
+ if (checkInstanciateCI(sdt)) {
+ instanceManager
+ .buildCompetenceInstances(new ArrayList<EvaluationObject>(ES
+ .getEvaluationObjects()), SU, ESI,
+ initLevel);
+ }
+
+ /*
+ * BUILD EXAMEN INSTANCES
+ *
+ */
+ buildExamenInstances(SU, ESI);
+
+ inituids.addAll(others);
+
+ for (RessourceDefinition rd : ES
+ .getRessourceDefinitions()) {
+ ressourceManager.setPermission(rd, inituids, "View");
+ //ressourceManager.setPermission(rd, others, "Eval");
+ }
+ }
+ }
+ }
+ }
+
+ // NEEDED FOR BONITA INSTANCIATION PROCESS !
+ //evaluationDefinitionDAO.flush();
+
+ instanceManager.instanciateProcesses(sbpl);
+
+ return 0;
+
+ }
+
+ /**
+ * Checks if is instanication mode.
+ *
+ * @param ed
+ * the ed
+ * @param sdt
+ * the sdt
+ * @param mode
+ * the mode
+ *
+ * @return true, if is instanication mode
+ */
+ private boolean isInstanicationMode(EvaluationDefinition ed,
+ ScenarioDefinition sdt, int mode) {
+
+ int m = -100;
+
+ if (ed.getInstanciationMode() != null) {
+ m = ed.getInstanciationMode();
+ }
+
+ Integer sdInstMode = sdt.getInstanciationMode();
+
+ if (sdInstMode != null && sdInstMode != EvaluationDefinition.MODE_NONE) {
+ m = sdt.getInstanciationMode();
+ }
+
+ if (m == mode) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinition#removeEDefToES(
+ * java.lang.Long, java.lang.Long)
+ */
+ public EntityDefinition removeEDefToES(Long idEDef, Long idES) {
+
+ Set<EvaluationSubject> ses = new HashSet<EvaluationSubject>();
+ EntityDefinition EDef = entityDefinitionDAO.get(idEDef);
+
+ System.out.println("EDef = " + EDef);
+ Set<EvaluationSubject> removedES = new HashSet<EvaluationSubject>();
+
+ for (EvaluationSubject es : EDef.getEvaluationSubjects()) {
+ if (!es.getId().equals(idES)) {
+ ses.add(es);
+ } else {
+ removedES.add(es);
+
+ }
+ }
+
+ System.out.println("xx 1");
+
+ EDef.setEvaluationSubjects(ses);
+ entityDefinitionDAO.saveOrUpdate(EDef);
+
+ EDef = entityDefinitionDAO.get(idEDef);
+
+ System.out.println("xx 2 EDef = " + EDef);
+
+ Set<EntityDefinition> sEDef = new HashSet<EntityDefinition>();
+ sEDef.add(EDef);
+
+ System.out.println("xx 3");
+
+ for (EvaluationSubject es : removedES){
+
+ if (es.getEvaluationSubjectInstances() != null && !es.getEvaluationSubjectInstances().isEmpty()){
+
+ EvaluationDefinition ed = es.getEvaluationDefinition();
+
+ for (ScenarioDefinition sdt : ed.getScenarioDefinitions()) {
+ System.out.println("xx 4");
+
+ // Ici théoriqueement une seul boucle car ed <-> SD = 1..1 bien que Set<SD>
+
+ if (isInstanicationMode(ed, sdt, EvaluationDefinition.MODE_COLLECTIVE)) {
+
+
+ List<Map<Set<User>, EntityDefinition>> map = getPartedInstanciation(ed .getInstanciationMode(), sdt, sEDef);
+
+
+ for (EvaluationSubjectInstance esi : es.getEvaluationSubjectInstances()){
+ ScenarioInstance SI = esi.getScenarioInstance();
+
+ for (Map<Set<User>,EntityDefinition> mapOfSetOfUser : map) {
+ for (Entry<Set<User>, EntityDefinition> e : mapOfSetOfUser.entrySet()) {
+ Set<Entity> SU = new HashSet<Entity>();
+
+ SU.addAll(e.getKey());
+
+ Map<String, Set<Entity>> map2 = new HashMap<String, Set<Entity>>();
+ map2.put(EDef.getProcessRoleDefinition().getName(), SU);
+
+ instanceManager.removeParticipants(SI, map2);
+
+
+
+ }
+ }
+ }
+
+
+ }
+
+ }
+ }
+ }
+
+
+ return EDef;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#removeEOFromES
+ * (java.lang.Long, java.lang.Long)
+ */
+ public Map<String, Set<Long>> removeEOFromES(Long ESId, Long EOId) {
+
+ Set<Long> EDefIds = new HashSet<Long>();
+ Set<Long> disableIds = new HashSet<Long>();
+
+ EvaluationSubject ES = evaluationSubjectDAO.get(ESId);
+ EvaluationDefinition ed = ES.getEvaluationDefinition();
+ Plan plan = ed.getPlan();
+
+ if (plan.getEntityLock()) {
+
+ // A Set without the delete EO
+ Set<EvaluationObject> seo = new HashSet<EvaluationObject>();
+
+ Set<Entity> r = new HashSet<Entity>();
+
+ // Compute a Set without the delete EO from ES
+ for (EvaluationObject eo : ES.getEvaluationObjects()) {
+ if (!eo.getId().equals(EOId)) {
+ seo.add(eo);
+ }
+ }
+
+ // Get Role/Users association from Plan for All EO in ES
+ Map<String, Set<Entity>> planEntity = plan
+ .getRelatedRoleEntitiesMap(ES.getEvaluationObjects());
+
+ // Get Role/Users association From Plan for All EO without the
+ // deleted EO in ES
+ // We get for each role the must deleted Entity Set (because Plab is
+ // EntityLock)
+ // by difference between each Set
+ Map<String, Set<Entity>> newPlanEntity = plan
+ .getRelatedRoleEntitiesMap(seo);
+
+ // Make the difference of Sets between planEntity and newPlanEntity
+ for (Entry<String, Set<Entity>> p1 : planEntity.entrySet()) {
+ String role = p1.getKey();
+ Set<Entity> se = p1.getValue();
+
+ if (newPlanEntity.containsKey(role)) {
+ se.removeAll(newPlanEntity.get(role));
+ if (se.size() > 0) {
+ r.addAll(se);
+ }
+ } else {
+ r.addAll(se);
+ }
+ }
+
+ // Compute the EntityDefinition corresponding to the Set of Entity
+ // that
+ // we must delete (if present).
+ Set<Long> sed = new HashSet<Long>();
+
+ //
+
+ Set<EntityDefinition> disable = new HashSet<EntityDefinition>();
+ for (EntityDefinition EDef : ES.getEntityDefinitions()) {
+
+ // Only one Entity
+ if (EDef.getEntites().size() != 1) {
+ continue;
+ }
+
+ // Here we want only one Entity of one User
+ if (EDef.getUsers().size() != 1) {
+ continue;
+ }
+
+ for (Entity e : EDef.getEntites()) {
+ if (r.contains(e)) {
+
+ if (EDef.getEvaluationSubjects().size() > 1) {
+
+ // We must desassociate these EDefs
+ disable.add(EDef);
+ disableIds.add(EDef.getId());
+ } else {
+ // We must delete these EDefs
+ sed.add(EDef.getId());
+ }
+ }
+ }
+
+ }
+
+ // Deassociate
+ ES.getEntityDefinitions().removeAll(disable);
+
+ }
+
+ // Compute the EO to delete from ES source
+ Set<EvaluationObject> ss = new HashSet<EvaluationObject>();
+ for (EvaluationObject ee : ES.getEvaluationObjects()) {
+ if (ee.getId().equals(EOId)) {
+ ss.add(ee);
+ break;
+ }
+ }
+
+ // If Active we need to delete CompetenceInstance
+
+ if (ed.isActive()) {
+
+ // building !!!
+ for (EvaluationSubjectInstance esi : ES
+ .getEvaluationSubjectInstances()) {
+
+ // Remove all CompetenceInstances for this ES
+ instanceManager.deleteCompetenceInstances(new ArrayList<EvaluationObject>(ss), esi
+ .getRelatedUsers(), esi);
+ }
+
+ }
+
+ // finally Effective EO association suppression.
+ ES.getEvaluationObjects().removeAll(ss);
+ evaluationSubjectDAO.merge(ES);
+
+ Map<String, Set<Long>> ret = new HashMap<String, Set<Long>>();
+ ret.put("delete", EDefIds);
+ ret.put("disable", disableIds);
+
+ return ret;
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seecom.pentila.evalcomp.manager.interfacedef.IDefinitionBase#
+ * removeRessourceDefinitions(java.util.Set)
+ */
+ public void removeRessourceDefinitions(Set<RessourceDefinition> srd) {
+ // TODO Auto-generated method stub
+ for (RessourceDefinition rd : srd) {
+ ressourceManager.removeRessourceDefinition(rd);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#replaceEDefByEDef
+ * (com.pentila.evalcomp.domain.definition.EntityDefinition,
+ * com.pentila.evalcomp.domain.definition.EntityDefinition)
+ */
+ public Set<EntityDefinition> replaceEDefByEDef(EntityDefinition from,
+ EntityDefinition to) {
+ // TODO : add message transation sysytem on server side.
+
+ // On NO INIT ROLE AT First
+
+ from = entityDefinitionDAO.get(from.getId());
+
+
+ if (logger.isDebugEnabled()){
+
+ logger.debug("Debugging replaceEDefByEDEF");
+ logger.debug("EDef from id is : " + from.getId() + " Name = " + from.getName());
+ }
+
+
+ if (from.getUsers().size() == 1 && to.getUsers().size() == 1
+ && !from.isAssociateWithInitRoleDefinition()) {
+
+ User f = from.getUsers().iterator().next();
+
+ String uidFrom = f.getUid();
+ String roleFrom = from.getProcessRoleDefinition().getName();
+
+ User t = to.getUsers().iterator().next();
+ String uidTo = t.getUid();
+ String roleTo = roleFrom;
+
+ User objissuer = Beans.getEntityManagerBean().getCurrentUser();
+ String subject = "Remplacement";
+ String body = "L'evaluateur " + f.getLastname() + " "
+ + f.getFirstname() + " a été remplacé par l'evaluateur "
+ + t.getLastname() + " " + t.getFirstname();
+
+
+ if (logger.isDebugEnabled()){
+
+ logger.debug("Form user uid : " + f.getUid());
+ logger.debug("To user uid : " + uidTo);
+ logger.debug("Builded sended message is : " + body);
+
+ }
+
+
+
+ ScenarioDefinition SD = scenarioDefinitionDAO.get(from.getProcessRoleDefinition().getPid().getScenarioDefinitionAbstract().getId());
+
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(SD.getEvaluationDefinition().getId());
+
+ Set<String> rolesRecipient = new HashSet<String>();
+
+ for (ScenarioDefinition sd : ed.getScenarioDefinitions()) {
+ rolesRecipient.add(sd.getInitRole());
+ }
+
+
+ if (logger.isDebugEnabled()){
+
+ logger.debug("roleRecipents are : ");
+
+ for (String r : rolesRecipient){
+ logger.debug(r);
+ }
+
+ }
+
+
+ boolean present = false;
+
+ for (EvaluationInstance EI : ed.getEvaluationInstances()) {
+
+ if (logger.isDebugEnabled()){
+ logger.debug("EI is : " + EI.getId());
+ }
+
+
+ for (ScenarioInstance SI : EI.getScenarioInstances()) {
+
+ if (logger.isDebugEnabled()){
+ logger.debug("SI is : " + SI.getId());
+ }
+
+
+ present = instanceManager.replaceParticipant(SI, uidFrom,
+ roleFrom, uidTo, roleTo);
+ if (present) {
+ instanceManager.notifyByMessage(objissuer, SI.getId()
+ .toString(), subject, body, rolesRecipient);
+ }
+ }
+ }
+
+ Set<User> recipients = new HashSet<User>();
+ recipients.add(f);
+
+ body = "Votre remplacement a été effectué dans la campagne "
+ + ed.getName();
+ entityManager.getMessageManager().notifyByMessage(objissuer,
+ subject, body, recipients);
+
+ if (logger.isDebugEnabled()){
+ logger.debug("Message sended " + body + " to " + f.getUid());
+ }
+
+ body = "Vous remplacez " + f.getLastname() + " " + f.getFirstname()
+ + " dans la campagne " + ed.getName();
+ recipients.clear();
+ recipients.add(t);
+ entityManager.getMessageManager().notifyByMessage(objissuer,
+ subject, body, recipients);
+
+ if (logger.isDebugEnabled()){
+ logger.debug("Message sended " + body + " to " + t.getUid());
+ }
+
+ Entity ee = to.getEntites().iterator().next();
+
+ for (EntityDefinition EDef : ed.getEntityDefinitions()) {
+ if (EDef.getEntites().size() != 1) {
+ continue;
+ }
+ if (EDef.getUsers().size() != 1) {
+ continue;
+ }
+
+ Entity e = EDef.getEntites().iterator().next();
+
+ if (e.getId().equals(ee.getId())
+ && EDef.getProcessRoleDefinition().getId().equals(
+ to.getProcessRoleDefinition().getId())) {
+ to = EDef;
+ break;
+ }
+
+ }
+
+
+ if (logger.isDebugEnabled()){
+ logger.debug("To id is : " + to.getId());
+ }
+
+
+ if (to.getId() == null) {
+ // to.setEvaluationDefinition(ed);
+
+ // to.setEvaluationSubjects(from.getEvaluationSubjects());
+ Set<EntityDefinition> defs = new HashSet<EntityDefinition>();
+ defs.add(to);
+ defs = addEDefsToED(ed.getId(), defs);
+
+ to = defs.iterator().next();
+ to.setProcessRoleDefinition(from.getProcessRoleDefinition());
+ }
+
+ Set<Long> idsEDef = new HashSet<Long>();
+ idsEDef.add(from.getId());
+ deleteEDefs(idsEDef, false);
+
+ for (EvaluationSubject ES : from.getEvaluationSubjects()) {
+ to = associateEDefToES(to.getId(), ES.getId());
+ }
+ Set<EntityDefinition> ret = new HashSet<EntityDefinition>();
+ ret.add(to);
+ return ret;
+
+ }
+ return null;
+ }
+
+ /**
+ * Sets the entity definition dao.
+ *
+ * @param entityDefinitionDAO
+ * the new entity definition dao
+ */
+ public void setEntityDefinitionDAO(IEntityDefinitionDAO entityDefinitionDAO) {
+ this.entityDefinitionDAO = entityDefinitionDAO;
+ }
+
+ /**
+ * Sets the entity manager.
+ *
+ * @param entityManager
+ * the new entity manager
+ */
+ public void setEntityManager(IEntityBase entityManager) {
+ this.entityManager = entityManager;
+ }
+
+ /**
+ * Sets the evaluation definition dao.
+ *
+ * @param evaluationDefinitionDAO
+ * the new evaluation definition dao
+ */
+ public void setEvaluationDefinitionDAO(
+ IEvaluationDefinitionDAO evaluationDefinitionDAO) {
+ this.evaluationDefinitionDAO = evaluationDefinitionDAO;
+ }
+
+ /**
+ * Sets the evaluation object manager.
+ *
+ * @param evaluationObjectManager
+ * the new evaluation object manager
+ */
+ public void setEvaluationObjectManager(
+ IEvaluationObjectBase evaluationObjectManager) {
+ this.evaluationObjectManager = evaluationObjectManager;
+ }
+
+ /**
+ * Sets the evaluation subject dao.
+ *
+ * @param evaluationSubjectDAO
+ * the new evaluation subject dao
+ */
+ public void setEvaluationSubjectDAO(
+ IEvaluationSubjectDAO evaluationSubjectDAO) {
+ this.evaluationSubjectDAO = evaluationSubjectDAO;
+ }
+
+ /**
+ * Sets the event activity dao.
+ *
+ * @param eventActivityDAO
+ * the new event activity dao
+ */
+ public void setEventActivityDAO(IEventActivityDAO eventActivityDAO) {
+ this.eventActivityDAO = eventActivityDAO;
+ }
+
+ /**
+ * Sets the instance manager.
+ *
+ * @param instanceManager
+ * the new instance manager
+ */
+ public void setInstanceManager(IInstanceBase instanceManager) {
+ this.instanceManager = instanceManager;
+ }
+
+ /**
+ * Sets the method eval manager.
+ *
+ * @param methodEvalManager
+ * the new method eval manager
+ */
+ public void setMethodEvalManager(IMethodEvalBase methodEvalManager) {
+ this.methodEvalManager = methodEvalManager;
+ }
+
+ /**
+ * Sets the process instanciation definition manager.
+ *
+ * @param processInstanciationDefinitionManager
+ * the new process instanciation definition manager
+ */
+ public void setProcessInstanciationDefinitionManager(
+ IProcessInstanciationDefinitionBase processInstanciationDefinitionManager) {
+ this.processInstanciationDefinitionManager = processInstanciationDefinitionManager;
+ }
+
+ /**
+ * Sets the ressource manager.
+ *
+ * @param ressourceManager
+ * the new ressource manager
+ */
+ public void setRessourceManager(IRessourceBase ressourceManager) {
+ this.ressourceManager = ressourceManager;
+ }
+
+ /**
+ * Sets the scenario definition dao.
+ *
+ * @param scenarioDefinitionDAO
+ * the new scenario definition dao
+ */
+ public void setScenarioDefinitionDAO(
+ IScenarioDefinitionDAO scenarioDefinitionDAO) {
+ this.scenarioDefinitionDAO = scenarioDefinitionDAO;
+ }
+
+ /**
+ * 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.IDefinitionBase#updateEDef(
+ * com.pentila.evalcomp.domain.definition.EntityDefinition, java.util.Set,
+ * java.util.Set)
+ */
+ public void updateEDef(EntityDefinition EDef, Set<User> toAdd,
+ Set<User> toDelete) {
+
+ if (logger.isDebugEnabled()){
+ logger.debug("updateEDef EDef id : {}, toADD : {}, toDelete : {}", new Object[]{EDef.getId(), toAdd, toDelete});
+ }
+
+ Set<String> processInstanceIds = new HashSet<String>();
+
+ ScenarioDefinition SD = scenarioDefinitionDAO.get(EDef.getProcessRoleDefinition().getPid().getScenarioDefinitionAbstract().getId());
+ EvaluationDefinition ed = SD.getEvaluationDefinition();
+
+ if (ed.isActive()) {
+
+ if (!toAdd.isEmpty()) {
+
+ if (EDef.isAssociateWithInitRoleDefinition()) {
+
+ Set<Set<User>> ssu = new HashSet<Set<User>>();
+
+ boolean doProcess = false;
+
+ Level initLevel = null;
+ if (ed.getMethodEval() != null){
+ initLevel = ed.getMethodEval().getInitialLevel();
+ }
+
+ Set<BonitaProcessLaunch> sbpl = new HashSet<BonitaProcessLaunch>();
+
+ for (ScenarioDefinition sdt : ed.getScenarioDefinitions()) {
+
+ if (isInstanicationMode(ed, sdt,
+ EvaluationDefinition.MODE_INDIVIDUAL)) {
+
+ for (User u : toAdd) {
+ Set<User> su = new HashSet<User>();
+ su.add(u);
+ ssu.add(su);
+
+ }
+
+ doProcess = true;
+
+ for (Set<User> SU : ssu) {
+
+ EvaluationInstance EI = instanceManager
+ .addEvaluationInstance(ed, EDef);
+
+ for (EvaluationSubject ES : ed
+ .getEvaluationSubjects()) {
+ EvaluationSubjectInstance ESI = instanceManager
+ .addEvaluationSubjectInstance(ES,
+ EI);
+ ScenarioInstance SI = instanceManager
+ .addScenarioInstance(EI, sdt, ESI,
+ SU);
+
+ 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));
+ if (checkInstanciateCI(sdt)) {
+ instanceManager
+ .buildCompetenceInstances(
+ new ArrayList<EvaluationObject>(ES
+ .getEvaluationObjects()),
+ SU, ESI, initLevel);
+ }
+ }
+ }
+
+ } else {
+ // Add localrole on each SceanrioInstance
+ // and add CompentenceInstances
+
+ Set<EvaluationInstance> SEI = new HashSet<EvaluationInstance>();
+
+ if (EDef.getEvaluationInstances().isEmpty()) {
+
+ SEI = ed.getEvaluationInstances();
+
+ } else {
+
+ for (EvaluationInstance EI : EDef
+ .getEvaluationInstances()) {
+
+ if (EI.getEntityDefinition().getId()
+ .equals(EDef.getId())) {
+ SEI.add(EI);
+
+ }
+
+ }
+ }
+
+ for (EvaluationInstance ei : SEI) {
+ for (ScenarioInstance si : ei
+ .getScenarioInstances()) {
+ if (si.getScenarioDefinition().getId()
+ .equals(sdt.getId())) {
+
+ EvaluationSubjectInstance ESI = si
+ .getEvaluationSubjectInstance();
+ EvaluationSubject ES = ESI
+ .getEvaluationSubject();
+
+ Set<User> SU = toAdd;
+ if (checkInstanciateCI(sdt)) {
+ instanceManager
+ .buildCompetenceInstances(
+ new ArrayList<EvaluationObject>(ES
+ .getEvaluationObjects()),
+ SU, ESI, initLevel);
+ }
+ Map<String, Set<Entity>> map = new HashMap<String, Set<Entity>>();
+
+ map.put(EDef.getProcessRoleDefinition()
+ .getName(), new HashSet<Entity>(SU));
+
+ //processInstanceIds.add(si.getProcessInstanceId());
+
+ instanceManager
+ .addParticipants(si, map);
+
+ }
+ }
+ }
+
+ }
+ }
+
+ if (doProcess) {
+
+ // NEEDED !!!!
+ //evaluationDefinitionDAO.flush();
+
+ instanceManager.instanciateProcesses(sbpl);
+ }
+
+ Set<String> uids = new HashSet<String>();
+ for (User u : toAdd) {
+ uids.add(u.getUid());
+ }
+
+ for (RessourceDefinition rd : ed
+ .getRelatedRessourceDefinitions()) {
+ ressourceManager.setPermission(rd, uids, "View");
+ }
+
+ } else {
+
+ for (ScenarioDefinition sdt : ed.getScenarioDefinitions()) {
+
+ if (isInstanicationMode(ed, sdt,
+ EvaluationDefinition.MODE_COLLECTIVE)) {
+
+ for (EvaluationInstance EI : ed
+ .getEvaluationInstances()) {
+ for (ScenarioInstance SI : EI
+ .getScenarioInstances()) {
+
+ Set<User> SU = toAdd;
+ Map<String, Set<Entity>> map = new HashMap<String, Set<Entity>>();
+ map.put(EDef.getProcessRoleDefinition()
+ .getName(), new HashSet<Entity>(SU));
+ instanceManager.addParticipants(SI, map);
+ }
+
+ }
+ }
+ }
+
+ Set<String> uids = new HashSet<String>();
+ for (User u : toAdd) {
+ uids.add(u.getUid());
+ }
+ for (RessourceDefinition rd : ed
+ .getRelatedRessourceDefinitions()) {
+ ressourceManager.setPermission(rd, uids, "View");
+ }
+ }
+ }
+
+ // DELETE PART !!!
+
+ if (!toDelete.isEmpty()) {
+ if (EDef.isAssociateWithInitRoleDefinition()) {
+
+ Long edId = ed.getId();
+
+ Set<String> suid = new HashSet<String>();
+ for (User user : toDelete) {
+ suid.add(user.getUid());
+ }
+
+ instanceManager.deleteScenarioInstancesForUsers(suid, edId,
+ EDef.getId());
+
+ for (RessourceDefinition rd : ed
+ .getRelatedRessourceDefinitions()) {
+ ressourceManager.removePermission(rd, suid, "");
+ }
+ } else {
+ Set<String> suid = new HashSet<String>();
+ for (User user : toDelete) {
+ suid.add(user.getUid());
+ }
+
+ Map<String, Set<Entity>> m = new HashMap<String, Set<Entity>>();
+
+ m.put(EDef.getProcessRoleDefinition().getName(), new HashSet<Entity>(toDelete));
+
+ for (EvaluationInstance EI : ed.getEvaluationInstances()) {
+ for (ScenarioInstance si : EI.getScenarioInstances()) {
+ String piid = si.getProcessInstanceId();
+ boolean ok = instanceManager.removeParticipants(si, m);
+ if (ok){
+ processInstanceIds.add(piid);
+ }
+ }
+ }
+
+ for (RessourceDefinition rd : ed
+ .getRelatedRessourceDefinitions()) {
+ ressourceManager.removePermission(rd, suid, "Eval");
+ }
+ }
+
+ }
+
+ }
+
+ //evaluationDefinitionDAO.flush();
+
+ for (String piid : processInstanceIds){
+ workflowManager.refreshAssignation(piid);
+ }
+
+ }
+
+
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#getUsersOfProcessRoleDefinition(java.lang.Long, java.lang.String, int, int, java.lang.String)
+ */
+ public TListEObject getUsersOfProcessRoleDefinition(Long scenarioInstanceId,
+ String prdName, int start, int limit, String searchTerm) {
+
+ List<EntityDefinition> sed = new ArrayList<EntityDefinition>();
+ ScenarioInstance si = instanceManager
+ .getScenarioInstance(scenarioInstanceId);
+
+
+
+ Set<EntityDefinition> sedef = si.getEvaluationSubjectInstance().getEvaluationSubject().getEntityDefinitions();
+
+ for (EntityDefinition edef : sedef) {
+
+ if (edef.getProcessRoleDefinition().getName().equals(prdName)){
+ sed.add(edef);
+ }
+ }
+
+
+ Set<User> su = new HashSet<User>();
+ for (EntityDefinition edef : sed){
+ su.addAll(edef.getUsers());
+ }
+
+ List<User> lu = new ArrayList<User>();
+
+ if (searchTerm !=null && !searchTerm.equals("")){
+
+ searchTerm = searchTerm.toLowerCase();
+
+ for (User u : su){
+ String last = u.getLastname().toLowerCase();
+ String first = u.getFirstname().toLowerCase();
+
+ if (last.startsWith(searchTerm) || first.startsWith(searchTerm)){
+ lu.add(u);
+ }
+ }
+
+
+ }else{
+ lu = new ArrayList<User>(su);
+ }
+
+
+ Collections.sort(lu, new Comparator<User>(){
+
+ public int compare(User o1, User o2) {
+
+ String name1 = o1.getLastname() + " " + o1.getFirstname();
+ String name2 = o2.getLastname() + " " + o2.getFirstname();
+
+ name1 = name1.toLowerCase();
+ name2 = name2.toLowerCase();
+ return name1.compareTo(name2);
+
+
+ }
+
+ });
+
+ int total = lu.size();
+
+ int a = 0;
+ if (start > 0){
+ a =start;
+ }
+
+ limit = start + limit;
+
+ if (limit > lu.size()){
+ limit = lu.size();
+ }
+
+ TListEObject tlm = new TListEObject();
+ tlm.setTotal(total);
+
+
+ List<EObject> res = new ArrayList<EObject>();
+
+ for (int i=a; i<limit; i++){
+ res.add(lu.get(i));
+ }
+
+ tlm.setListOfEObjects(res);
+
+ return tlm;
+
+ }
+
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#getEvaluationDefinition(java.lang.Long)
+ */
+ public EvaluationDefinition getEvaluationDefinition(Long id) {
+ return evaluationDefinitionDAO.get(id);
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#getAssoEvalTable(java.lang.Long)
+ */
+ public TCompositeDataMap getAssoEvalTable(Long edId) {
+
+ TCompositeDataMap tcdm = new TCompositeDataMap();
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(edId);
+
+ List<EvaluationSubject> les = new ArrayList<EvaluationSubject>(ed
+ .getEvaluationSubjects());
+
+ Collections.sort(les, new Comparator<EvaluationSubject>() {
+
+ public int compare(EvaluationSubject o1, EvaluationSubject o2) {
+ return o1.getComputedName().compareTo(o2.getComputedName());
+ }
+
+ });
+
+ for (EvaluationSubject es : les) {
+ tcdm.addHeader(es.getComputedName());
+ tcdm.getHeadersIds().add(es.getId());
+ }
+
+ List<EntityDefinition> led = new ArrayList<EntityDefinition>();
+ for (EntityDefinition edef : ed.getEntityDefinitions()) {
+ if (!edef.isAssociateWithInitRoleDefinition()) {
+ led.add(edef);
+ }
+ }
+
+ Collections.sort(led, new Comparator<EntityDefinition>() {
+
+ public int compare(EntityDefinition o1, EntityDefinition o2) {
+ return o1.getName().compareTo(o2.getName());
+ }
+
+ });
+
+ String[][] map = new String[led.size()][];
+
+ int i = 0;
+
+ for (EntityDefinition edef : led) {
+
+ String[] line = new String[les.size() + 2];
+
+ line[0] = edef.getName();
+
+ int j = 1;
+ for (EvaluationSubject ES : les) {
+ if (ES.getEntityDefinitions().contains(edef)) {
+ line[j] = "1";
+ } else {
+ line[j] = "0";
+ }
+
+ j++;
+
+ }
+ line[j] = edef.getId().toString();
+ map[i] = line;
+ i++;
+
+ }
+
+ tcdm.setDataMap(map);
+
+ return tcdm;
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#getEndEvaluationDefinitions()
+ */
+ public Set<EvaluationDefinition> getEndEvaluationDefinitions(){
+
+ Set<EvaluationDefinition> sed = evaluationDefinitionDAO.getEndEvaluationDefinitions();
+
+ for (EvaluationDefinition ED : sed){
+ for (EntityDefinition EDef : ED.getEntityDefinitions()){
+ EDef.getUsers();
+ }
+ }
+ return sed;
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#copyEvaluationDefinition(java.lang.Long)
+ */
+ public boolean copyEvaluationDefinition(Long idED) {
+
+ Map<Long, EvaluationSubject> map = new HashMap<Long, EvaluationSubject>();
+
+ EvaluationDefinition ED = evaluationDefinitionDAO.get(idED);
+
+ EvaluationDefinition copy = new EvaluationDefinition();
+
+ copy.setName("Copie de " + ED.getName());
+ copy.setInstanciationMode(ED.getInstanciationMode());
+ copy.setDescription(ED.getDescription());
+ copy.setMethodEval(ED.getMethodEval());
+ copy.setNotifyDate(ED.getNotifyDate());
+ copy.setDateStart(ED.getDateStart());
+ copy.setDateEnd(ED.getDateEnd());
+ copy.setPlan(ED.getPlan());
+ copy.setState(EvaluationDefinition.INITIAL);
+ copy.setOwner(entityManager.getCurrentUser());
+
+ evaluationDefinitionDAO.saveOrUpdate(copy);
+
+ for (ScenarioDefinition sd : ED.getScenarioDefinitions()){
+ ScenarioDefinition copySD = new ScenarioDefinition();
+
+ copySD.setAuthor(sd.getAuthor());
+ copySD.setCreationDate(new Date().toString());
+ copySD.setDescription(sd.getDescription());
+ copySD.setInitRole(sd.getInitRole());
+ copySD.setInstancesCount(sd.getInstancesCount());
+ copySD.setPackageId(sd.getPackageId());
+ copySD.setInstanciationMode(sd.getInstanciationMode());
+ copySD.setName(sd.getName());
+ copySD.setProcessDefinitionId(sd.getProcessDefinitionId());
+ copySD.setProcessDefinitionUUID(sd.getProcessDefinitionUUID());
+ copySD.setVersion(sd.getVersion());
+ copySD.setType(sd.getType());
+
+ copySD.setEvaluationDefinition(copy);
+
+ scenarioDefinitionDAO.saveOrUpdate(copySD);
+
+ ProcessInstanciationDefinition PID = new ProcessInstanciationDefinition();
+ PID.setScenarioDefinitionAbstract(copySD);
+
+ PID = processInstanciationDefinitionManager.add(PID);
+
+ for (Property p : sd.getPid().getProperties()){
+ Property copyP = new Property();
+ copyP.setDescription(p.getDescription());
+ copyP.setName(p.getName());
+ copyP.setPid(PID);
+ copyP.setType(p.getType());
+ copyP.setValue(p.getValue());
+ copyP.setDataTypeClassName(p.getDataTypeClassName());
+
+ processInstanciationDefinitionManager.addProperty(PID, copyP);
+
+ PID.getProperties().add(copyP);
+
+ }
+
+ for (ProcessRoleDefinition prd : sd.getPid().getProcessRoleDefinitions()){
+ ProcessRoleDefinition copyPRD = new ProcessRoleDefinition();
+ copyPRD.setInitRole(prd.getInitRole());
+ copyPRD.setName(prd.getName());
+ copyPRD.setPid(PID);
+
+ processInstanciationDefinitionManager.addProcessRoleDefinition(PID, copyPRD);
+
+ PID.getProcessRoleDefinitions().add(copyPRD);
+
+ for (EntityDefinitionAbstract EDefA : prd.getEntityDefinitions()){
+
+ EntityDefinition edef = (EntityDefinition)EDefA;
+
+ EntityDefinition copyEDef = new EntityDefinition();
+ copyEDef.setDescription(edef.getDescription());
+ copyEDef.setName(edef.getName());
+ copyEDef.setProcessRoleDefinition(copyPRD);
+
+ Set<Entity> se = new HashSet<Entity>(edef.getEntites());
+
+ copyEDef.setEntites(se);
+
+ entityDefinitionDAO.saveOrUpdate(copyEDef);
+
+ copyPRD.getEntityDefinitions().add(copyEDef);
+
+ for (EvaluationSubject es : edef.getEvaluationSubjects()){
+
+ EvaluationSubject copyES = null;
+
+ if (!map.containsKey(es.getId())){
+
+ EvaluationSubject c = new EvaluationSubject();
+ c.setEvaluationDefinition(copy);
+ Set<EvaluationObject> seo = new HashSet<EvaluationObject>(es.getEvaluationObjects());
+ c.setEvaluationObjects(seo);
+ copy.getEvaluationSubjects().add(c);
+ map.put(es.getId(), c);
+ }
+
+ copyES = map.get(es.getId());
+ copyES.setName(es.getName());
+ copyES.getEntityDefinitions().add(copyEDef);
+
+ evaluationSubjectDAO.saveOrUpdate(copyES);
+
+ }
+ }
+
+ }
+
+ copySD.setPid(PID);
+
+ scenarioDefinitionDAO.saveOrUpdate(copySD);
+
+ }
+ for (EvaluationSubject es : ED.getEvaluationSubjects()){
+ if (!map.containsKey(es.getId())){
+
+
+ EvaluationSubject c = new EvaluationSubject();
+ c.setEvaluationDefinition(copy);
+ Set<EvaluationObject> seo = new HashSet<EvaluationObject>(es.getEvaluationObjects());
+ c.setEvaluationObjects(seo);
+
+ evaluationSubjectDAO.saveOrUpdate(c);
+
+ if (es.getExamenDef() != null){
+
+ ExamenDef examDef = new ExamenDef();
+ examDef.setName(es.getExamenDef().getName());
+ examDef.setEvaluationSubject(c);
+ examDef.setEvaluationDefinition(copy);
+ examDef.setExamen(es.getExamenDef().getExamen());
+ examenDefDAO.saveOrUpdate(examDef);
+ c.setExamenDef(examDef);
+
+ if (copy.getExamenDefs() == null){
+ copy.setExamenDefs(new HashSet<ExamenDef>());
+ }
+ copy.getExamenDefs().add(examDef);
+ }
+
+ copy.getEvaluationSubjects().add(c);
+
+ map.put(es.getId(), c);
+
+ }
+
+ EvaluationSubject copyES = map.get(es.getId());
+
+ for (EventActivity EA : es.getEventActivities()){
+ EventActivity copyEA = new EventActivity();
+ copyEA.setActivityId(EA.getActivityId());
+ copyEA.setEndDate(EA.getEndDate());
+ copyEA.setStartDate(EA.getStartDate());
+ copyEA.setEvaluationSubject(copyES);
+ copyES.getEventActivities().add(copyEA);
+
+ eventActivityDAO.saveOrUpdate(copyEA);
+ //evaluationSubjectDAO.saveOrUpdate(copyES);
+
+ }
+
+
+ for (RessourceDefinition rd : es.getRessourceDefinitions()){
+
+ RessourceDefinition newRd = new RessourceDefinition();
+
+ newRd.setActivityId(rd.getActivityId());
+ newRd.setDescription(rd.getDescription());
+ newRd.setEvaluationSubject(copyES);
+ newRd.setName(rd.getName());
+ newRd.setPluginKey(rd.getPluginKey());
+ newRd.setPath(rd.getPath());
+ newRd.setType(rd.getType());
+ newRd.setOwner(entityManager.getCurrentUser());
+
+ ressourceManager.addRessourceDefinition(newRd);
+
+ }
+
+ }
+
+ evaluationDefinitionDAO.saveOrUpdate(copy);
+
+ return true;
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#getUserRolesByReferentiel(java.util.Set, java.lang.Long)
+ */
+ public Set<ProcessRoleDefinition> getUserRolesByReferentiel(
+ Set<Long> entityIds, Long idRef) {
+
+ return this.processInstanciationDefinitionManager.getUserRolesByReferentiel(entityIds, idRef);
+
+ }
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#addParticipantsToSDA(java.lang.Long, java.lang.Long, java.util.Set)
+ */
+ public Set<EntityDefinitionAbstract> addParticipantsToSDA(Long SDAId, Long PRDId, Set<EntityDefinitionAbstract> seda){
+
+ ScenarioDefinitionAbstract SDA = scenarioDefinitionAbstractDAO.get(SDAId);
+
+ Set<EntityDefinitionAbstract> sedaBis = new HashSet<EntityDefinitionAbstract>();
+
+ for (ProcessRoleDefinition prd : SDA.getPid().getProcessRoleDefinitions()){
+ if (prd.getId().equals(PRDId)){
+
+ Set<Entity> se = new HashSet<Entity>();
+
+ for (EntityDefinitionAbstract EDA : seda){
+
+ String name = "";
+ for(Entity e : EDA.getEntites()){
+
+ if (e instanceof User){
+ User u = (User)e;
+ name = name + u.getLastname() + " " + u.getFirstname();
+ }else{
+ name = name + e.getUid();
+ }
+ }
+
+ EDA.setName(name);
+ EDA.setProcessRoleDefinition(prd);
+ EDA = entityDefinitionAbstractDAO.merge(EDA);
+
+ sedaBis.add(EDA);
+
+ se.addAll(EDA.getEntites());
+
+ }
+
+ Map<String, Set<Entity>> map =new HashMap<String, Set<Entity>>();
+
+ map.put(prd.getName(), se);
+
+ for (ScenarioInstanceAbstract SIA : SDA.getScenarioInstances()){
+ instanceManager.addParticipants(SIA, map);
+ }
+
+ }
+ }
+
+ return sedaBis;
+ }
+
+
+ /* (non-Javadoc)
+ * @see com.pentila.evalcomp.manager.interfacedef.IDefinitionBase#removeParticipantsToSDA(java.lang.Long, java.lang.Long, java.util.Set)
+ */
+ public Set<Long> removeParticipantsToSDA(Long SDAId, Long PRDId, Set<EntityDefinitionAbstract> seda){
+
+ Set<Long> EDAIds = new HashSet<Long>();
+
+ ScenarioDefinitionAbstract SDA = scenarioDefinitionAbstractDAO.get(SDAId);
+
+ for (ProcessRoleDefinition prd : SDA.getPid().getProcessRoleDefinitions()){
+ if (prd.getId().equals(PRDId)){
+
+ Set<Entity> se = new HashSet<Entity>();
+
+ for (EntityDefinitionAbstract EDA : seda){
+
+ entityDefinitionAbstractDAO.evict(EDA);
+ EDA = entityDefinitionAbstractDAO.get(EDA.getId());
+
+ logger.debug("Size of EDA : " + EDA.getEntites().size());
+
+ se.addAll(EDA.getEntites());
+
+ entityDefinitionAbstractDAO.delete(EDA);
+
+ }
+
+
+ Map<String, Set<Entity>> map =new HashMap<String, Set<Entity>>();
+
+ map.put(prd.getName(), se);
+
+ for (ScenarioInstanceAbstract SIA : SDA.getScenarioInstances()){
+ instanceManager.removeParticipants(SIA, map);
+ workflowManager.refreshAssignation(SIA.getProcessInstanceId());
+ }
+
+
+ }
+ }
+
+ return EDAIds;
+ }
+
+ public void replacePlanInED(EvaluationDefinition currentEval, Plan plan){
+
+
+ // Suppression des EvaluationSubjects
+ Set<Long> sseId = new HashSet<Long>();
+
+ for(EvaluationSubject es : currentEval.getEvaluationSubjects()){
+ sseId.add(es.getId());
+ }
+ deleteESs(sseId);
+
+ // Suppression du scenario
+ addSDToED(currentEval.getId(), null);
+
+ // Suppression de MethodEval
+ currentEval.setMethodEval(null);
+ editEvaluationDefinition(currentEval);
+
+ // Mise en place du nouveau plan
+ currentEval.setPlan(plan);
+ editEvaluationDefinition(currentEval);
+
+ }
+
+ public Set<ExamenDef> addExamensToED(Long idED, Set<Long> examenIds) {
+
+ EvaluationDefinition ed = evaluationDefinitionDAO.get(idED);
+
+ Set<ExamenDef> se = new HashSet<ExamenDef>();
+
+ for (Long id : examenIds){
+ Examen examen = examenDAO.get(id);
+
+ ExamenDef examDef = new ExamenDef();
+ examDef.setEvaluationDefinition(ed);
+ examDef.setExamen(examen);
+ examDef.setName(examen.getNom_examen());
+
+ //examenDefDAO.saveOrUpdate(examDef);
+
+ if (examen.getQuestions() != null){
+
+
+ EvaluationSubject es = new EvaluationSubject();
+ Set<EvaluationObject> seo = new HashSet<EvaluationObject>();
+ for (Question q : examen.getQuestions()){
+
+ Competence c = q.getCompetence();
+
+ seo.add(c);
+
+ }
+
+ es.setEvaluationObjects(seo);
+ es.setEvaluationDefinition(ed);
+
+
+ evaluationSubjectDAO.saveOrUpdate(es);
+
+ examDef.setEvaluationSubject(es);
+
+ es.setExamenDef(examDef);
+ }
+
+ se.add(examDef);
+ }
+
+ if (ed.getExamenDefs() == null){
+ ed.setExamenDefs(new HashSet<ExamenDef>());
+ }
+
+ this.examenDefDAO.saveOrUpdateAll(se);
+
+ return se;
+
+
+
+
+ }
+
+ public void removeExamensFromED(Long idED, Set<Long> idExamenDefs) {
+
+ Set<ExamenDef> sed = new HashSet<ExamenDef>();
+ for(Long idExamenDef : idExamenDefs ){
+ ExamenDef examenDef = examenDefDAO.get(idExamenDef);
+ sed.add(examenDef);
+ }
+
+ this.examenDefDAO.deleteAll(sed);
+
+ }
+
+ public IExamenDAO getExamenDAO() {
+ return examenDAO;
+ }
+
+ public void setExamenDAO(IExamenDAO examenDAO) {
+ this.examenDAO = examenDAO;
+ }
+
+ public IExamenDefDAO getExamenDefDAO() {
+ return examenDefDAO;
+ }
+
+ public void setExamenDefDAO(IExamenDefDAO examenDefDAO) {
+ this.examenDefDAO = examenDefDAO;
+ }
+
+
+ public boolean replaceOwnerED(Long idED, User oldUser, User newUser) {
+
+ EvaluationDefinition ed = this.evaluationDefinitionDAO.get(idED);
+
+ User objissuer = Beans.getEntityManagerBean().getCurrentUser();
+ String subject = "Remplacement";
+
+ String body = "";
+ Set<User> recipients = new HashSet<User>();
+ if (oldUser != null){
+
+
+ recipients.add(oldUser);
+
+ body = "Votre remplacement a été effectué dans la campagne "
+ + ed.getName();
+ entityManager.getMessageManager().notifyByMessage(objissuer,
+ subject, body, recipients);
+
+ if (logger.isDebugEnabled()){
+ logger.debug("Message sended " + body + " to " + oldUser.getUid());
+ }
+ body = "Vous remplacez " + oldUser.getLastname() + " " + oldUser.getFirstname()
+ + " dans la campagne " + ed.getName();
+ }else{
+ body = "Vous etes affecté à la campagne " + ed.getName() + " en tant que gestionnaire.";
+ }
+
+ recipients.clear();
+ recipients.add(newUser);
+ entityManager.getMessageManager().notifyByMessage(objissuer,
+ subject, body, recipients);
+
+ ed.setOwner(newUser);
+
+ evaluationDefinitionDAO.saveOrUpdate(ed);
+
+
+ return true;
+
+
+ }
+
+ @Override
+ public void editProperties(ProcessInstanciationDefinition pid) {
+ this.processInstanciationDefinitionManager.editProperties(pid);
+ }
+
+
+
+
+
+
+ public Integer campaignUpdateEntities(EvaluationDefinition ed, String entities, String role,
+ String action){
+
+ Set<EntityDefinition> entitySetToDelet = new HashSet<EntityDefinition>();
+
+ // récupération des users/group
+ String[] entitiesUid = entities.split(",");
+
+ ScenarioDefinition SD = ed.getScenarioDefinitions().iterator().next();
+ ProcessRoleDefinition prd = null;
+
+ for (ProcessRoleDefinition PRD : SD.getPid().getProcessRoleDefinitions()){
+ if(PRD.getName().equals(role)){
+ prd = PRD;
+ }
+ }
+
+ if (null == prd){
+ return WsErrors.ERROR_ROLE;
+ }
+
+ if("add".equalsIgnoreCase(action)){
+
+
+ Set<EntityDefinition> edefSet = new HashSet<EntityDefinition>();
+ for(String login : entitiesUid){
+ EntityDefinition edef = new EntityDefinition();
+ edef.setProcessRoleDefinition(prd);
+
+ User u = entityManager.getUser(login, (String[]) null);
+ Group g = entityManager.getGroup(login);
+
+ if(null != u){
+ edef.getEntites().add(u);
+ edef.setName(u.getFirstname() + " " + u.getLastname());
+ entityDefinitionDAO.saveOrUpdate(edef);
+ edefSet.add(edef);
+ }
+ else if(null != g){
+ edef.getEntites().add(g);
+ edef.setName(g.getUid());
+ entityDefinitionDAO.saveOrUpdate(edef);
+ edefSet.add(edef);
+ }
+ else{
+ // ni user ni group
+ }
+ }
+
+
+ addEDefsToED(ed.getId(), edefSet);
+ }
+ else if("remove".equalsIgnoreCase(action)){
+
+ Set<Long> sl = new HashSet<Long>();
+
+ for (String s : entitiesUid){
+ for(EntityDefinition e : ed.getEntityDefinitions()){
+ for(User u : e.getUsers()){
+ if(u.getUid().equals(s)){
+ sl.add(e.getId());
+ }
+ }
+ }
+ }
+
+ System.out.println("deleteEDefs");
+ this.deleteEDefs(sl, true);
+
+ }
+ else{
+ return WsErrors.ERROR_UNKNOWN_COMMAND;
+ }
+
+ return WsErrors.WS_OK;
+ }
+
+
+
+ public Long createCampaign(String uidGroup, Date dateStart, Date dateEnd,
+ String uidEvaluateur, String name, String uuidScenario, Long idMethode,
+ Long idPlan, String idsCompetences) {
+
+ EvaluationDefinition ed = new EvaluationDefinition();
+ evaluationDefinitionDAO.saveOrUpdate(ed);
+
+ IPlanDAO iplandao = (IPlanDAO) Beans.getApplicationContext().getBean("planDAO");
+ Plan plan = iplandao.getPlan(idPlan);
+ ed.setPlan(plan);
+
+ MethodEval me = methodEvalManager.get(idMethode);
+ ed.setMethodEval(me);
+
+ ScenarioDefinition SD = new ScenarioDefinition();
+ SD.setProcessDefinitionUUID(uuidScenario);
+ SD = addSDToED(ed.getId(), SD);
+
+ ed.setDateStart(dateStart);
+ ed.setDateEnd(dateEnd);
+
+ ed.setName(name);
+
+ EntityDefinition candidats = new EntityDefinition();
+ EntityDefinition evaluateur = new EntityDefinition();
+
+
+ for (ProcessRoleDefinition prd : SD.getPid().getProcessRoleDefinitions()){
+ if (prd.getInitRole()){
+ candidats.setProcessRoleDefinition(prd);
+ }else{
+ evaluateur.setProcessRoleDefinition(prd);
+ }
+ }
+
+ Group gr = this.getEntityManager().getGroup(uidGroup);
+ Set<Entity> e1 = new HashSet<Entity>();
+ e1.add(gr);
+ candidats.setEntites(e1);
+ candidats.setName(gr.getUid());
+
+ User userEvaluateur = this.getEntityManager().getUser(uidEvaluateur);
+ Set<Entity> e2 = new HashSet<Entity>();
+ e2.add(userEvaluateur);
+ evaluateur.setEntites(e2);
+ evaluateur.setName(userEvaluateur.getLastname() + " " + userEvaluateur.getFirstname());
+
+ this.entityDefinitionDAO.saveOrUpdate(candidats);
+ this.entityDefinitionDAO.saveOrUpdate(evaluateur);
+
+ ed.setOwner(userEvaluateur);
+
+ Set<EntityDefinition> sed = new HashSet<EntityDefinition>();
+
+ sed.add(candidats);
+ sed.add(evaluateur);
+ sed = addEDefsToED(ed.getId(), sed);
+
+ String[] idString = idsCompetences.split(",");
+
+ Set<Long> idLong = new HashSet<Long>();
+
+ for (String s : idString){
+ idLong.add(Long.parseLong(s));
+ }
+
+ EvaluationSubject es = new EvaluationSubject();
+
+ for (Long l : idLong){
+ EvaluationObject myEvalObj = evaluationObjectManager.get(l);
+ es.getEvaluationObjects().add(myEvalObj);
+ }
+
+ this.evaluationSubjectDAO.saveOrUpdate(es);
+
+ this.addESToED(ed.getId(), es);
+
+ this.associateEDefToES(evaluateur.getId(), es.getId());
+
+ evaluationDefinitionDAO.saveOrUpdate(ed);
+
+ return ed.getId();
+
+ }
+
+}