package org.pegadi.server.service.impl;
import no.dusken.common.model.Person;
import no.dusken.common.model.Role;
import no.dusken.common.service.RoleService;
import org.apache.commons.lang.NotImplementedException;
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
@Service("roleService")
public class RoleServiceImpl extends NamedParameterJdbcDaoSupport implements RoleService<Role>{
private final RoleMapper roleMapper = new RoleMapper();
@Override
public Role getByName(String name) {
try {
return getNamedParameterJdbcTemplate().queryForObject("SELECT * FROM roles where name = :name",
Collections.singletonMap("name", name), roleMapper);
} catch (DataAccessException e) {
return null;
}
}
@Override
public Role getByExternalID(Long aLong) {
throw new NotImplementedException();
}
@Override
public List<Role> findAll() {
throw new NotImplementedException();
}
@Override
public Iterable<Role> findAll(Iterable<Long> longs) {
throw new NotImplementedException();
}
@Override
public long count() {
throw new NotImplementedException();
}
@Override
public void delete(Long aLong) {
throw new NotImplementedException();
}
@Override
public void delete(Role entity) {
throw new NotImplementedException();
}
@Override
public void delete(Iterable<? extends Role> entities) {
throw new NotImplementedException();
}
@Override
public void deleteAll() {
throw new NotImplementedException();
}
@Override
public List<Role> findAll(Sort sort) {
throw new NotImplementedException();
}
@Override
public Page<Role> findAll(Pageable pageable) {
throw new NotImplementedException();
}
@Override
public <S extends Role> S save(S entity) {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("name", entity.getName());
parameters.put("ordering", entity.getOrdering());
parameters.put("timecreated", entity.getTimeCreated().getTime());
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
String sql;
if(entity.isNew()){
parameters.put("version", 0);
sql = "INSERT INTO roles (name, ordering, timecreated) values (:name, :ordering, :timecreated)";
}else {
parameters.put("id", entity.getId());
parameters.put("version", entity.getVersion() + 1);
sql = "UPDATE roles set name=:name, ordering=:ordering, WHERE id = :id";
}
getNamedParameterJdbcTemplate().update(sql, new MapSqlParameterSource(parameters), generatedKeyHolder);
return (S) findOne(generatedKeyHolder.getKey().longValue());
}
@Override
public <S extends Role> List<S> save(Iterable<S> entities) {
throw new NotImplementedException();
}
@Override
public Role findOne(Long id) {
try {
return getNamedParameterJdbcTemplate().queryForObject("SELECT * FROM roles where id = :id", Collections.singletonMap("id", id), roleMapper);
} catch (DataAccessException e) {
return null;
}
}
@Override
public boolean exists(Long aLong) {
throw new NotImplementedException();
}
@Override
public void flush() {
throw new NotImplementedException();
}
@Override
public Role saveAndFlush(Role entity) {
throw new NotImplementedException();
}
@Override
public void deleteInBatch(Iterable<Role> entities) {
throw new NotImplementedException();
}
@Override
public void deleteAllInBatch() {
throw new NotImplementedException();
}
@Override
public Role findOne(Specification spec) {
throw new NotImplementedException();
}
@Override
public List<Role> findAll(Specification spec) {
throw new NotImplementedException();
}
@Override
public Page<Role> findAll(Specification spec, Pageable pageable) {
throw new NotImplementedException();
}
@Override
public List<Role> findAll(Specification spec, Sort sort) {
throw new NotImplementedException();
}
@Override
public long count(Specification spec) {
throw new NotImplementedException();
}
@Override
public Collection<Role> findRolesForPerson(Person person) {
return getNamedParameterJdbcTemplate().query("select * from roles, person_role where roles.id=person_role.role_id and person_id = :id", Collections.singletonMap("id", person.getId()), roleMapper);
}
private class RoleMapper implements RowMapper<Role> {
@Override
public Role mapRow(ResultSet rs, int rowNum) throws SQLException {
Role role = new Role(rs.getLong("id"));
role.setName(rs.getString("name"));
Calendar timecreatedCal = Calendar.getInstance();
timecreatedCal.setTime(rs.getDate("timecreated"));
role.setTimeCreated(timecreatedCal);
role.setOrdering(rs.getInt("ordering"));
return role;
}
}
}