mardi 19 mai 2015

project how to redesign to use unique dao

i have two projects,but they are not use the same Dao layers,and one project use url request to invoke other project method. i encounter a problem,when i create an entity,i have to create separated Dao,Service and so on.these dao is use different fragement. i want to find one method or a suit design pattern to use the unique abstract dao,and change the url invoke way to clearer and more changeable way, i need your helps,thanks very much whatever your answer in advance! the code like

public static String apiDeviceEdit(Device device, HttpSession session,
            Long operate) {
        String userId = (String) session
                .getAttribute(CommonAttributes.API_USERID_SESSION);
        String token = (String) session
                .getAttribute(CommonAttributes.API_TOKEN_SESSION);
        Assert.notNull(userId);
        Assert.notNull(token);
        String param = "user_id=" + userId + "&device_sn=" + device.getfSn()
                + "&operate=" + operate + "&token=" + token;

        String string = sendPost(setting.getApiDeviceEdit(), param,
                "DeviceEdit");

        return string;

    }

the different Dao like that:

@Component
@Scope("singleton")
public class ConductorDao extends BaseHBDao <Conductor, Long> {

    @Autowired
    public ConductorDao (@Qualifier("sessionFactory") SessionFactory session) {

        super ();
        this.setSessionFactory (session);
    }
}

public interface ConductorDao extends BaseDao<Conductor, Long> {

     final String SELECT_BY_SN = "from Conductor c where c.sn =:sn";
    Conductor findBySn(String sn) throws Exception;
    Page<Conductor> findByUserId(Long getfId, Pageable pageable);

}

@Component
@Scope("singleton")
public class ConductorDaoImpl extends BaseDaoImpl<Conductor, Long> implements ConductorDao {

    @Override
    public Conductor findBySn(String sn) throws Exception {
        List<Conductor> conductors = entityManager.createQuery(SELECT_BY_SN).setParameter("sn", sn).getResultList();
        if (conductors == null || conductors.size()== 0) {
            return new Conductor();
        }
        else {
            return conductors.get(0);
        }
    }

    @Override
    public Page<Conductor> findByUserId(Long getfId, Pageable pageable) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Conductor> criteriaQuery = criteriaBuilder.createQuery(Conductor.class);
        Root<Conductor> root = criteriaQuery.from(Conductor.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("userId"), getfId));
        criteriaQuery.where(restrictions);
        return super.findPage(criteriaQuery, pageable);
    }

}

Aucun commentaire:

Enregistrer un commentaire