Servicio de maestros

Introducción

Un servicio de maestros permite tener centralizados en un único servicio aquellos elementos que nos sirvan para identificar otros, por ejemplo, para un candidato, la procedencia de ese candidato, si ha sido por medio de un reclutador, de un empleado, a través de Linkedin… Estas tablas serán elementos que puedan repetirse en múltiples registros y nos interesa saber que elementos tienen para poder gestionarlos. Crearemos un servicio llamado MasterService, que contenga todos los DAO que puedan pertenecer a esta categoría.

Estableciendo los DAO

Si comprobamos la tabla de CANDIDATE de la base de datos, observamos que hay varias columnas que pueden ser factibles para ser una tabla de maestros que estén asociados a este nuevo servicio.

tutorial_10.png

Crearemos la tablas asociadas a esos datos. Las tablas contendrán una estructura de 2 columnas, un identificador y una descripción.

Código SQL

CREATE TABLE EDUCATION (ID INTEGER IDENTITY PRIMARY KEY NOT NULL, DESCRIPTION NVARCHAR(255) NOT NULL);
CREATE TABLE STATUS(ID INTEGER IDENTITY PRIMARY KEY NOT NULL, DESCRIPTION NVARCHAR(255) NOT NULL);
CREATE TABLE ORIGIN (ID INTEGER IDENTITY PRIMARY KEY NOT NULL, DESCRIPTION NVARCHAR(255) NOT NULL);
CREATE TABLE EXPERIENCE_LEVEL (ID INTEGER IDENTITY PRIMARY KEY NOT NULL, DESCRIPTION NVARCHAR(255) NOT NULL);
CREATE TABLE PROFILE (ID INTEGER IDENTITY PRIMARY KEY NOT NULL, DESCRIPTION NVARCHAR(255) NOT NULL);

Añadiremos claves foráneas a la tabla de CANDIDATE asociando sus columnas con las nuevas tablas.

Código SQL

ALTER TABLE CANDIDATE ADD CONSTRAINT CANDIDATE_FK_EDUCATION FOREIGN KEY (EDUCATION) REFERENCES EDUCATION(ID);
ALTER TABLE CANDIDATE ADD CONSTRAINT CANDIDATE_FK_STATUS FOREIGN KEY (STATUS) REFERENCES STATUS(ID);
ALTER TABLE CANDIDATE ADD CONSTRAINT CANDIDATE_FK_ORIGIN FOREIGN KEY (ORIGIN) REFERENCES ORIGIN(ID);
ALTER TABLE CANDIDATE ADD CONSTRAINT CANDIDATE_FK_EXPERIENCE_LEVEL FOREIGN KEY (EXPERIENCE_LEVEL) REFERENCES EXPERIENCE_LEVEL(ID);
ALTER TABLE CANDIDATE ADD CONSTRAINT CANDIDATE_FK_PROFILE FOREIGN KEY (PROFILE) REFERENCES PROFILE(ID);

Insertamos un registro en cada una de ellas, para que puedan tener datos.

Código SQL

INSERT INTO EDUCATION (DESCRIPTION) VALUES ('Degree in Computer Science');
INSERT INTO STATUS (DESCRIPTION) VALUES ('Available');
INSERT INTO ORIGIN (DESCRIPTION) VALUES ('Recruiter');
INSERT INTO EXPERIENCE_LEVEL (DESCRIPTION) VALUES ('Junior');
INSERT INTO PROFILE (DESCRIPTION) VALUES ('Technician');

Ahora se crearán los DAO correspondientes a esas tablas, tanto los elementos las clases en Java como los ficheros *.xml

EducationDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<JdbcEntitySetup
        xmlns="http://www.ontimize.com/schema/jdbc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.ontimize.com/schema/jdbc http://www.ontimize.com/schema/jdbc/ontimize-jdbc-dao.xsd"
        catalog="" schema="${mainschema}" table="EDUCATION"
        datasource="mainDataSource" sqlhandler="dbSQLStatementHandler">
    <DeleteKeys>
        <Column>ID</Column>
    </DeleteKeys>
    <UpdateKeys>
        <Column>ID</Column>
    </UpdateKeys>
    <GeneratedKey>ID</GeneratedKey>
</JdbcEntitySetup>

EducationDao.java

package com.ontimize.hr.model.core.dao;

import com.ontimize.jee.server.dao.common.ConfigurationFile;
import com.ontimize.jee.server.dao.jdbc.OntimizeJdbcDaoSupport;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

@Repository("EducationDao")
@Lazy
@ConfigurationFile(configurationFile = "dao/EducationDao.xml", configurationFilePlaceholder = "dao/placeholders.properties")
public class EducationDao extends OntimizeJdbcDaoSupport {

    public static final String ATTR_ID = "ID";
    public static final String ATTR_DESCRIPTION = "DESCRIPTION";
}

ExperienceLevelDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<JdbcEntitySetup
        xmlns="http://www.ontimize.com/schema/jdbc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.ontimize.com/schema/jdbc http://www.ontimize.com/schema/jdbc/ontimize-jdbc-dao.xsd"
        catalog="" schema="${mainschema}" table="EXPERIENCE_LEVEL"
        datasource="mainDataSource" sqlhandler="dbSQLStatementHandler">
    <DeleteKeys>
        <Column>ID</Column>
    </DeleteKeys>
    <UpdateKeys>
        <Column>ID</Column>
    </UpdateKeys>
    <GeneratedKey>ID</GeneratedKey>
</JdbcEntitySetup>

ExperienceLevelDao.java

package com.ontimize.hr.model.core.dao;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.ontimize.jee.server.dao.common.ConfigurationFile;
import com.ontimize.jee.server.dao.jdbc.OntimizeJdbcDaoSupport;

@Repository("ExperienceLevelDao")
@Lazy
@ConfigurationFile(configurationFile = "dao/ExperienceLevelDao.xml", configurationFilePlaceholder = "dao/placeholders.properties")
public class ExperienceLevelDao  extends OntimizeJdbcDaoSupport{

    public static final String ATTR_ID = "ID";
    public static final String ATTR_DESCRIPTION = "DESCRIPTION";
}

OriginDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<JdbcEntitySetup
        xmlns="http://www.ontimize.com/schema/jdbc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.ontimize.com/schema/jdbc http://www.ontimize.com/schema/jdbc/ontimize-jdbc-dao.xsd"
        catalog="" schema="${mainschema}" table="ORIGIN"
        datasource="mainDataSource" sqlhandler="dbSQLStatementHandler">
    <DeleteKeys>
        <Column>ID</Column>
    </DeleteKeys>
    <UpdateKeys>
        <Column>ID</Column>
    </UpdateKeys>
    <GeneratedKey>ID</GeneratedKey>
</JdbcEntitySetup>

OriginDao.java

package com.ontimize.hr.model.core.dao;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.ontimize.jee.server.dao.common.ConfigurationFile;
import com.ontimize.jee.server.dao.jdbc.OntimizeJdbcDaoSupport;

@Repository("OriginDao")
@Lazy
@ConfigurationFile(configurationFile = "dao/OriginDao.xml", configurationFilePlaceholder = "dao/placeholders.properties")
public class OriginDao  extends OntimizeJdbcDaoSupport{

    public static final String ATTR_ID = "ID";
    public static final String ATTR_DESCRIPTION = "DESCRIPTION";
}

ProfileDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<JdbcEntitySetup
        xmlns="http://www.ontimize.com/schema/jdbc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.ontimize.com/schema/jdbc http://www.ontimize.com/schema/jdbc/ontimize-jdbc-dao.xsd"
        catalog="" schema="${mainschema}" table="PROFILE"
        datasource="mainDataSource" sqlhandler="dbSQLStatementHandler">
    <DeleteKeys>
       <Column>ID</Column>
    </DeleteKeys>
    <UpdateKeys>
        <Column>ID</Column>
    </UpdateKeys>
    <GeneratedKey>ID</GeneratedKey>
</JdbcEntitySetup>

ProfileDao.java

package com.ontimize.hr.model.core.dao;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.ontimize.jee.server.dao.common.ConfigurationFile;
import com.ontimize.jee.server.dao.jdbc.OntimizeJdbcDaoSupport;

@Repository("ProfileDao")
@Lazy
@ConfigurationFile(configurationFile = "dao/ProfileDao.xml", configurationFilePlaceholder = "dao/placeholders.properties")
public class ProfileDao  extends OntimizeJdbcDaoSupport{

    public static final String ATTR_ID = "ID";
    public static final String ATTR_DESCRIPTION = "DESCRIPTION";
}

StatusDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<JdbcEntitySetup
        xmlns="http://www.ontimize.com/schema/jdbc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.ontimize.com/schema/jdbc http://www.ontimize.com/schema/jdbc/ontimize-jdbc-dao.xsd"
        catalog="" schema="${mainschema}" table="STATUS"
        datasource="mainDataSource" sqlhandler="dbSQLStatementHandler">
    <DeleteKeys>
        <Column>ID</Column>
    </DeleteKeys>
    <UpdateKeys>
        <Column>ID</Column>
    </UpdateKeys>
    <GeneratedKey>ID</GeneratedKey>
</JdbcEntitySetup>

StatusDao.java

package com.ontimize.hr.model.core.dao;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.ontimize.jee.server.dao.common.ConfigurationFile;
import com.ontimize.jee.server.dao.jdbc.OntimizeJdbcDaoSupport;

@Repository("StatusDao")
@Lazy
@ConfigurationFile(configurationFile = "dao/StatusDao.xml", configurationFilePlaceholder = "dao/placeholders.properties")
public class StatusDao  extends OntimizeJdbcDaoSupport{

    public static final String ATTR_ID = "ID";
    public static final String ATTR_DESCRIPTION = "DESCRIPTION";
}

Crear la interfaz del servicio de maestros

Crearemos una interfaz para el servicio de maestros, pensando en que se usarán los DAO creados en el punto anterior.

IMasterService.java

package com.ontimize.hr.api.core.service;

import com.ontimize.jee.common.dto.EntityResult;
import com.ontimize.jee.common.exceptions.OntimizeJEERuntimeException;

import java.util.List;
import java.util.Map;

public interface IMasterService {

    // EDUCATION
    EntityResult educationQuery(Map<String, Object> keyMap, List<String> attrList) throws OntimizeJEERuntimeException;

    EntityResult educationInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException;

    EntityResult educationUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    EntityResult educationDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    // EXPERIENCE_LEVEL
    EntityResult experienceLevelQuery(Map<String, Object> keyMap, List<String> attrList) throws OntimizeJEERuntimeException;

    EntityResult experienceLevelInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException;

    EntityResult experienceLevelUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    EntityResult experienceLevelDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    // ORIGIN
    EntityResult originQuery(Map<String, Object> keyMap, List<String> attrList) throws OntimizeJEERuntimeException;

    EntityResult originInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException;

    EntityResult originUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    EntityResult originDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    // PROFILE
    EntityResult profileQuery(Map<String, Object> keyMap, List<String> attrList) throws OntimizeJEERuntimeException;

    EntityResult profileInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException;

    EntityResult profileUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    EntityResult profileDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    // STATUS
    EntityResult statusQuery(Map<String, Object> keyMap, List<String> attrList) throws OntimizeJEERuntimeException;

    EntityResult statusInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException;

    EntityResult statusUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

    EntityResult statusDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException;

}

Creación del servicio de maestros

De la misma manera que hemos creado el servicio para CandidateService, vamos a crear el servicio para MasterService, que implementará la recién creada interfaz IMasterService. Es necesario anotarlo con @Service (cuyo valor será MasterService) y con @Lazy

MasterService.java

package com.ontimize.hr.model.core.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.ontimize.hr.api.core.service.IMasterService;
import com.ontimize.hr.model.core.dao.EducationDao;
import com.ontimize.hr.model.core.dao.ExperienceLevelDao;
import com.ontimize.hr.model.core.dao.OriginDao;
import com.ontimize.hr.model.core.dao.ProfileDao;
import com.ontimize.hr.model.core.dao.StatusDao;
import com.ontimize.jee.common.dto.EntityResult;
import com.ontimize.jee.common.exceptions.OntimizeJEERuntimeException;
import com.ontimize.jee.server.dao.DefaultOntimizeDaoHelper;

@Service("MasterService")
@Lazy
public class MasterService implements IMasterService {

    @Autowired
    private EducationDao educationDao;
    @Autowired
    private ExperienceLevelDao experienceLevelDao;
    @Autowired
    private OriginDao originDao;
    @Autowired
    private ProfileDao profileDao;
    @Autowired
    private StatusDao statusDao;
    @Autowired
    private DefaultOntimizeDaoHelper daoHelper;

    @Override
    public EntityResult educationQuery(Map<String, Object> keyMap, List<String> attrList)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.query(this.educationDao, keyMap, attrList);
    }

    @Override
    public EntityResult educationInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.insert(this.educationDao, attrMap);
    }

    @Override
    public EntityResult educationUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.update(this.educationDao, attrMap, keyMap);
    }

    @Override
    public EntityResult educationDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.delete(this.educationDao, keyMap);
    }

    @Override
    public EntityResult experienceLevelQuery(Map<String, Object> keyMap, List<String> attrList)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.query(this.experienceLevelDao, keyMap, attrList);
    }

    @Override
    public EntityResult experienceLevelInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.insert(this.experienceLevelDao, attrMap);
    }

    @Override
    public EntityResult experienceLevelUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.update(this.experienceLevelDao, attrMap, keyMap);
    }

    @Override
    public EntityResult experienceLevelDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.delete(this.experienceLevelDao, keyMap);
    }

    @Override
    public EntityResult originQuery(Map<String, Object> keyMap, List<String> attrList)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.query(this.originDao, keyMap, attrList);
    }

    @Override
    public EntityResult originInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.insert(this.originDao, attrMap);
    }

    @Override
    public EntityResult originUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.update(this.originDao, attrMap, keyMap);
    }

    @Override
    public EntityResult originDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.delete(this.originDao, keyMap);
    }

    @Override
    public EntityResult profileQuery(Map<String, Object> keyMap, List<String> attrList)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.query(this.profileDao, keyMap, attrList);
    }

    @Override
    public EntityResult profileInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.insert(this.profileDao, attrMap);
    }

    @Override
    public EntityResult profileUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.update(this.profileDao, attrMap, keyMap);
    }

    @Override
    public EntityResult profileDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.delete(this.profileDao, keyMap);
    }

    @Override
    public EntityResult statusQuery(Map<String, Object> keyMap, List<String> attrList)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.query(this.statusDao, keyMap, attrList);
    }

    @Override
    public EntityResult statusInsert(Map<String, Object> attrMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.insert(this.statusDao, attrMap);
    }

    @Override
    public EntityResult statusUpdate(Map<String, Object> attrMap, Map<String, Object> keyMap)
            throws OntimizeJEERuntimeException {
        return this.daoHelper.update(this.statusDao, attrMap, keyMap);
    }

    @Override
    public EntityResult statusDelete(Map<String, Object> keyMap) throws OntimizeJEERuntimeException {
        return this.daoHelper.delete(this.statusDao, keyMap);
    }

}

Creación del controlador de maestros

Ahora crearemos el controlador REST para el servicio de maestros. Tenemos que crear una clase nueva que extienda de ORestController, cuyo tipo de dato será la interfaz creada para el servicio, IMasterService. Esta clase contendrá las anotaciones @RestController, que indicará que esta clase servirá de controlador REST y @RequestMapping("/master") que establecerá la url del controlador.

MasterRestController

package com.ontimize.hr.ws.core.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ontimize.hr.api.core.service.IMasterService;
import com.ontimize.jee.server.rest.ORestController;

@RestController
@RequestMapping("/master")
public class MasterRestController extends ORestController<IMasterService> {

    @Autowired
    private IMasterService masterService;

    @Override
    public IMasterService getService() {
        return this.masterService;
    }

}

Arrancamos la aplicación y ya podemos hacer nuevas peticiones desde Postman.

arrow_back Tutorial anterior Próximo tutorial arrow_forward