Commit aae797e6c9fa67cfcbb5b520b21428fc8eb1e501

Authored by Pierrick
1 parent 81044ef5

Add SkillCalculator / SkillEvaluation / SkillCalculatorEndPoint

skilldr-data_model/src/main/java/eu/totoandco/data/parameter/ParameterName.java
... ... @@ -22,7 +22,8 @@ package eu.totoandco.data.parameter;
22 22 */
23 23 public enum ParameterName {
24 24 ABILITY_SIGNIN(1),
25   - ABILITY_RH_UPGRADE_USER(2);
  25 + ABILITY_RH_UPGRADE_USER(2),
  26 + BASE_EXPERIENCE_SKILL(3);
26 27 private int id;
27 28  
28 29 private ParameterName(int id) {
... ...
skilldr-properties/src/main/resources/buildScript.sql
... ... @@ -85,7 +85,9 @@ INSERT INTO job (`id`, `name`, `idDomain`) VALUES (1,"Développeur",1);
85 85 INSERT INTO skill (`id`, `name`, `idJob`) VALUES (1,"Java",1);
86 86 INSERT INTO user (`id`, `login`, `password`, `firstname`, `name`, `tag`, `url`, `creationDate`, `lastUpdate`, `idrole`, `idJob`)
87 87 VALUES (1, "toto@totoandco.eu", "toto", "Toto", "AndCo", "TOTOTAG", "www.totoandco.eu/skilldr", "2015-01-05 14:54:27","2015-01-05 14:54:27", 1, 1);
88   -INSERT INTO experienceType (`id`, `name`, `weight`) VALUES (1, "Stage", 2);
  88 +INSERT INTO experienceType (`id`, `name`, `weight`) VALUES (1, "Stage", 0.5);
  89 +INSERT INTO experienceType (`id`, `name`, `weight`) VALUES (2, "Travail", 1);
89 90 INSERT INTO userSkill (`idUser`, `idSkill`, `period`, `date`, `idType`) VALUES (1, 1, 2, "2015-01-05 14:54:27", 1);
90   -INSERT INTO parameterSkilldr (`id`, `value`, `descriptor`) VALUES (1, 0,"Ability to signin for extern user");
91   -INSERT INTO parameterSkilldr (`id`, `value`, `descriptor`) VALUES (2, 0,"Ability to upgrade an USER to RH");
92 91 \ No newline at end of file
  92 +INSERT INTO parameterSkilldr (`id`, `value`, `descriptor`) VALUES (1, "0","Ability to signin for extern user");
  93 +INSERT INTO parameterSkilldr (`id`, `value`, `descriptor`) VALUES (2, "0","Ability to upgrade an USER to RH");
  94 +INSERT INTO parameterSkilldr (`id`, `value`, `descriptor`) VALUES (3, "3650","Total skill mastery reference value");
93 95 \ No newline at end of file
... ...
skilldr-rs/src/main/java/eu/totoandco/rs/SkillCalculatorEndPoint.java 0 → 100644
  1 +package eu.totoandco.rs;
  2 +
  3 +import java.util.List;
  4 +
  5 +import javax.ejb.EJB;
  6 +import javax.ejb.Stateless;
  7 +import javax.servlet.http.HttpServletRequest;
  8 +import javax.ws.rs.FormParam;
  9 +import javax.ws.rs.GET;
  10 +import javax.ws.rs.POST;
  11 +import javax.ws.rs.Path;
  12 +import javax.ws.rs.PathParam;
  13 +import javax.ws.rs.core.Context;
  14 +
  15 +import eu.totoandco.business.SkillCalculator;
  16 +import eu.totoandco.business.SkillEvaluation;
  17 +import eu.totoandco.check.RestParameterCheck;
  18 +import eu.totoandco.exception.ExceptionLevel;
  19 +import eu.totoandco.exception.MessageCarrier;
  20 +import eu.totoandco.exception.MessageText;
  21 +import eu.totoandco.exception.RestException;
  22 +import eu.totoandco.model.User;
  23 +
  24 +/**
  25 + * WebService Class responsible for managing access to SkillCalculator
  26 + *
  27 + * @author Pierrick Knecht
  28 + */
  29 +@Path("/")
  30 +@Stateless
  31 +public class SkillCalculatorEndPoint extends EndPoint {
  32 + @EJB
  33 + SkillCalculator skillCalculator;
  34 +
  35 + /**
  36 + * Make an evaluation of skill mastery
  37 + *
  38 + * @param idSkill
  39 + * Skill's id to evaluate
  40 + * @param request
  41 + * User's context
  42 + * @return An evaluation
  43 + */
  44 + @GET
  45 + @Path("/p/makeEvaluation/{idSkill}")
  46 + public SkillEvaluation makeEvaluation(@PathParam("idSkill") int idSkill,
  47 + @Context HttpServletRequest request) {
  48 + RestParameterCheck.checkNotNegativeOrZero(idSkill);
  49 + return skillCalculator.getEvaluation(
  50 + (User) request.getAttribute(USERSESSION), idSkill);
  51 + }
  52 +
  53 + /**
  54 + * Make all evaluation of skill mastery
  55 + *
  56 + * @param request
  57 + * User's context
  58 + * @return A list of different evaluations
  59 + */
  60 + @GET
  61 + @Path("/p/makeEvaluation/")
  62 + public List<SkillEvaluation> makeEvaluation(
  63 + @Context HttpServletRequest request) {
  64 + skillCalculator.getAllEvaluation((User) request
  65 + .getAttribute(USERSESSION));
  66 + return skillCalculator.getAllEvaluation((User) request
  67 + .getAttribute(USERSESSION));
  68 + }
  69 +
  70 + /**
  71 + * Setter to modify Base_EXPERIENCE_SKILL in DB
  72 + *
  73 + * @param value
  74 + * New value to set
  75 + */
  76 + @POST
  77 + @Path("/p/setBaseExperienceSkill/")
  78 + public void setBaseExperienceSkill(@FormParam("value") double value,
  79 + @Context HttpServletRequest request) {
  80 +
  81 + if (value <= 0) {
  82 + throw new RestException(MessageCarrier
  83 + .message(MessageText.ERROR_OBJECT_NULL)
  84 + .level(ExceptionLevel.INFO).build());
  85 + }
  86 + skillCalculator.setBaseExperienceSkill(value);
  87 + }
  88 +
  89 + /**
  90 + * Setter to modify Base_EXPERIENCE_SKILL in DB
  91 + *
  92 + * @param idExperienceType
  93 + * New experience's type to affect
  94 + * @param period
  95 + * New period to affect
  96 + */
  97 + @POST
  98 + @Path("/p/setBaseExperienceSkill/")
  99 + public void setBaseExperienceSkill(
  100 + @FormParam("idExperienceType") int idExperienceType,
  101 + @FormParam("period") int period, @Context HttpServletRequest request) {
  102 +
  103 + if (idExperienceType <= 0) {
  104 + throw new RestException(MessageCarrier
  105 + .message(MessageText.ERROR_OBJECT_NULL)
  106 + .level(ExceptionLevel.INFO).build());
  107 + }
  108 +
  109 + if (period <= 0) {
  110 + throw new RestException(MessageCarrier
  111 + .message(MessageText.ERROR_OBJECT_NULL)
  112 + .level(ExceptionLevel.INFO).build());
  113 + }
  114 +
  115 + skillCalculator.setBaseExperienceSkill(idExperienceType, period);
  116 + }
  117 +}
... ...
skilldr-service/src/main/java/eu/totoandco/business/SkillCalculator.java
1 1 package eu.totoandco.business;
2 2  
  3 +import java.util.ArrayList;
  4 +import java.util.HashMap;
  5 +import java.util.List;
  6 +import java.util.Map;
  7 +
  8 +import javax.ejb.EJB;
3 9 import javax.ejb.LocalBean;
4 10 import javax.ejb.Stateless;
5 11  
  12 +import eu.totoandco.cache.ParameterCache;
  13 +import eu.totoandco.check.ParameterCheck;
  14 +import eu.totoandco.data.parameter.ParameterName;
  15 +import eu.totoandco.entitymanager.ModelAccess;
  16 +import eu.totoandco.model.ExperienceType;
  17 +import eu.totoandco.model.User;
  18 +import eu.totoandco.model.UserSkill;
  19 +
6 20 @Stateless
7 21 @LocalBean
8 22 public class SkillCalculator {
9 23  
  24 + @EJB
  25 + ModelAccess<UserSkill> modelUserSkill;
  26 +
  27 + @EJB
  28 + ModelAccess<ExperienceType> modelExperienceType;
  29 +
  30 + @EJB
  31 + ParameterCache parameterCache;
  32 +
  33 + private static int POURCENTAGE = 100;
  34 +
  35 + /**
  36 + * Make an evaluation of skill mastery
  37 + * @param idSkill Skill's id to evaluate
  38 + * @param request User's context
  39 + * @return An evaluation
  40 + */
  41 + public SkillEvaluation getEvaluation(User userSession, int idSkill){
  42 + List<UserSkill> listUserSkillModel = modelUserSkill.getAll(UserSkill.class);
  43 + List<UserSkill> listUserSkill = new ArrayList<UserSkill>();
  44 + if(!ParameterCheck.checkNotEmpty(listUserSkillModel)) {
  45 + for (UserSkill us : listUserSkillModel) {
  46 + if(us.getSkill().getId() == idSkill && us.getUser() == userSession.getId()){
  47 + listUserSkill.add(us);
  48 + }
  49 + }
  50 + }
  51 +
  52 + if(!ParameterCheck.checkNotEmpty(listUserSkill)) {
  53 + return makeEvaluation(listUserSkill);
  54 + } else {
  55 + return null;
  56 + }
  57 + }
  58 +
  59 + /**
  60 + * Make an evaluation of skill mastery
  61 + * @param listUserSkill The list to evaluate
  62 + * @return An evaluation
  63 + */
  64 + private SkillEvaluation makeEvaluation(List<UserSkill> listUserSkill){
  65 + if(ParameterCheck.checkNotEmpty(listUserSkill)) {
  66 +
  67 + double baseExperience = Double.parseDouble(parameterCache.getParameter(ParameterName.BASE_EXPERIENCE_SKILL));
  68 + double evaluation = 0;
  69 + for (UserSkill us : listUserSkill) {
  70 + evaluation += us.getExperienceType().getWeight() * us.getPeriod();
  71 + }
  72 +
  73 + return new SkillEvaluation(listUserSkill.get(0).getSkill().getName(), (int) (evaluation/baseExperience) * POURCENTAGE);
  74 + } else {
  75 + return null;
  76 + }
  77 + }
  78 +
  79 + /**
  80 + * Make all evaluation of skill mastery
  81 + * @param request User's context
  82 + * @return A list of different evaluations
  83 + */
  84 + public List<SkillEvaluation> getAllEvaluation(User userSession){
  85 + List<UserSkill> listSkillUser = modelUserSkill.getAll(UserSkill.class);
  86 + List<UserSkill> disorderList = new ArrayList<>();
  87 + for (UserSkill us : listSkillUser) {
  88 + if(us.getUser() == userSession.getId()) {
  89 + disorderList.add(us);
  90 + }
  91 + }
  92 +
  93 + if(!ParameterCheck.checkNotEmpty(disorderList)) {
  94 + return makeAllEvaluation(disorderList);
  95 + } else {
  96 + return null;
  97 + }
  98 + }
  99 +
  100 +
  101 + /**
  102 + * Make all evaluation of skill mastery
  103 + * @param A mixing list of skills to evaluate
  104 + * @return A list of different evaluations
  105 + */
  106 + private List<SkillEvaluation> makeAllEvaluation(List<UserSkill> disorderList){
  107 + Map<Integer, List<UserSkill>> orderList = new HashMap<>();
  108 +
  109 + for (UserSkill us : disorderList) {
  110 + if(orderList.get(us.getSkill().getId()) == null) {
  111 + orderList.put(us.getSkill().getId(), new ArrayList<UserSkill>());
  112 + }
  113 + orderList.get(us.getSkill().getId()).add(us);
  114 + }
  115 +
  116 + List<SkillEvaluation> listSkillEvaluation = new ArrayList<SkillEvaluation>();
  117 +
  118 + for (List<UserSkill> listUserSkill : orderList.values()) {
  119 + listSkillEvaluation.add(makeEvaluation(listUserSkill));
  120 + }
  121 + return listSkillEvaluation;
  122 + }
  123 +
  124 + /**
  125 + * Setter to modify Base_EXPERIENCE_SKILL in DB
  126 + * @param value New value to set
  127 + */
  128 + public void setBaseExperienceSkill(double value){
  129 + parameterCache.setParameter(ParameterName.BASE_EXPERIENCE_SKILL, String.valueOf(value));
  130 + }
  131 +
  132 + /**
  133 + * Setter to modify Base_EXPERIENCE_SKILL in DB
  134 + * @param idExperienceType New experience's type to affect
  135 + * @param period New period to affect
  136 + */
  137 + public void setBaseExperienceSkill(int idExperienceType, int period){
  138 + double newBaseExperience = modelExperienceType.get(idExperienceType, ExperienceType.class).getWeight() * period;
  139 + setBaseExperienceSkill(newBaseExperience);
  140 + }
10 141 }
... ...
skilldr-service/src/main/java/eu/totoandco/business/SkillEvaluation.java 0 → 100644
  1 +package eu.totoandco.business;
  2 +
  3 +public class SkillEvaluation {
  4 +
  5 + private String name;
  6 + private int evaluation;
  7 +
  8 + public SkillEvaluation(){
  9 + this.name = "";
  10 + this.evaluation = 0;
  11 + }
  12 +
  13 + public SkillEvaluation(String name, int evaluation){
  14 + this.name = name;
  15 + this.evaluation = evaluation;
  16 + }
  17 +
  18 +
  19 + public String getName(){
  20 + return name;
  21 + }
  22 +
  23 + public void setName(String s) {
  24 + this.name = s;
  25 + }
  26 +
  27 + public int getEvaluation(){
  28 + return evaluation;
  29 + }
  30 +
  31 + public void setEvaluation(int d){
  32 + this.evaluation = d;
  33 + }
  34 +}
... ...
skilldr-service/src/main/java/eu/totoandco/cache/ParameterCache.java
... ... @@ -83,6 +83,10 @@ public class ParameterCache {
83 83 * @param value the value to set
84 84 */
85 85 public void setParameter(ParameterName name, String value) {
  86 + Parameter p = new Parameter();
  87 + p.setId(name.getId());
  88 + p.setValue(value);
  89 + modelManager.update(p);
86 90 this.parameters.put(name, value);
87   - }
  91 + }
88 92 }
... ...
skilldr-service/src/test/java/eu/totoandco/business/SkillCalculatorGetGetAlllTest.java 0 → 100644
  1 +package eu.totoandco.business;
  2 +
  3 +import javax.ejb.EJB;
  4 +
  5 +import org.jboss.arquillian.container.test.api.Deployment;
  6 +import org.jboss.arquillian.junit.Arquillian;
  7 +import org.jboss.shrinkwrap.api.ShrinkWrap;
  8 +import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
  9 +import org.jboss.shrinkwrap.api.spec.WebArchive;
  10 +import org.junit.After;
  11 +import org.junit.Before;
  12 +import org.junit.Test;
  13 +import org.junit.runner.RunWith;
  14 +
  15 +import eu.totoandco.cache.ParameterCache;
  16 +import eu.totoandco.check.ParameterCheck;
  17 +import eu.totoandco.data.Role;
  18 +import eu.totoandco.data.parameter.Parameter;
  19 +import eu.totoandco.entitymanager.ModelAccess;
  20 +import eu.totoandco.exception.MessageException;
  21 +import eu.totoandco.model.Domain;
  22 +import eu.totoandco.model.ExperienceType;
  23 +import eu.totoandco.model.Job;
  24 +import eu.totoandco.model.Skill;
  25 +import eu.totoandco.model.User;
  26 +import eu.totoandco.model.UserSkill;
  27 +
  28 +/**
  29 + *
  30 + * @author Pierrick Knecht
  31 + *
  32 + */
  33 +@RunWith(Arquillian.class)
  34 +public class SkillCalculatorGetGetAlllTest {
  35 +
  36 + @Deployment
  37 + public static WebArchive createTestArchive() {
  38 + WebArchive war = ShrinkWrap
  39 + .create(WebArchive.class, "test.war")
  40 + .addClasses(ModelAccess.class, SkillCalculator.class, SkillEvaluation.class)
  41 + .addPackage(MessageException.class.getPackage())
  42 + .addPackage(Parameter.class.getPackage())
  43 + .addPackage(ParameterCheck.class.getPackage())
  44 + .addPackage(ParameterCache.class.getPackage())
  45 + .addPackage(Domain.class.getPackage())
  46 + .addPackage(Role.class.getPackage())
  47 + .addAsResource(
  48 + new ClassLoaderAsset("META-INF/persistence.xml"),
  49 + "META-INF/persistence.xml");
  50 + return war;
  51 + }
  52 +
  53 + @EJB
  54 + SkillCalculator skillCalculator;
  55 + @EJB
  56 + ModelAccess<Domain> domainModel;
  57 + @EJB
  58 + ModelAccess<Job> jobModel;
  59 + @EJB
  60 + ModelAccess<Skill> skillModel;
  61 + @EJB
  62 + ModelAccess<User> userModel;
  63 + @EJB
  64 + ModelAccess<ExperienceType> experienceTypeModel;
  65 + @EJB
  66 + ModelAccess<UserSkill> userSkillModel;
  67 +
  68 + Domain domain;
  69 + Job job;
  70 + Skill skill1, skill2;
  71 + User user;
  72 + ExperienceType experienceType;
  73 + UserSkill userSkill1, userSkill2, userSkill3;
  74 + final static int PERIOD = 40;
  75 + final static double COEFF = 4.0;
  76 +
  77 + @Before
  78 + public void beforeTest(){
  79 + domain = new Domain("TestDomain");
  80 + domainModel.save(domain);
  81 + job = new Job("TestJob", domain);
  82 + jobModel.save(job);
  83 + skill1 = new Skill("TestSkill1", job);
  84 + skillModel.save(skill1);
  85 + skill2 = new Skill("TestSkill2", job);
  86 + skillModel.save(skill2);
  87 + user = new User();
  88 + userModel.save(user);
  89 + experienceType = new ExperienceType("TestExperienceType", COEFF);
  90 + experienceTypeModel.save(experienceType);
  91 + userSkill1 = new UserSkill(user, skill1, PERIOD, experienceType);
  92 + userSkillModel.save(userSkill1);
  93 + userSkill2 = new UserSkill(user, skill2, PERIOD, experienceType);
  94 + userSkillModel.save(userSkill2);
  95 + }
  96 +
  97 + @Test
  98 + public void getEvaluationTest(){
  99 + skillCalculator.getEvaluation(user, skill1.getId());
  100 + }
  101 +
  102 + @Test
  103 + public void getAllEvaluationTest(){
  104 + skillCalculator.getAllEvaluation(user);
  105 + }
  106 +
  107 + @After
  108 + public void afterTest(){
  109 + userSkillModel.remove(userSkill1);
  110 + userSkillModel.remove(userSkill2);
  111 + userModel.remove(user);
  112 + skillModel.remove(skill1);
  113 + skillModel.remove(skill2);
  114 + jobModel.remove(job);
  115 + domainModel.remove(domain);
  116 + experienceTypeModel.remove(experienceType);
  117 + }
  118 +}
... ...