简体   繁体   English

在Java代码中添加布尔值以检查表状态条件

[英]Add boolean in java code to check table status conditions

I am trying to iterate through a list of records of sdid in java. 我正在尝试遍历Java中sdid的记录列表。 sdid records is set of record up to 8 records with the same sdid but need to check the status column of each sdid to see if its PEND_ADD , PEND_DEL AND ACTIVE . sdid记录是一组记录,最多可包含8条具有相同sdid的记录,但需要检查每个sdid的状态列,以查看其PEND_ADDPEND_DELACTIVE

So for the first condition: if the status is PEND_ADD or PEND_DEL . 因此,对于第一个条件:状态为PEND_ADDPEND_DEL Then delete the records. 然后删除记录。

If status is ACTIVE only, then delete records. 如果状态为ACTIVE只,然后删除记录。

If status is ACTIVE , PEND_ADD and/or PEND_DEL , then delete PEND_ADD and/or PEND_DEL and keep ACTIVE 如果状态为ACTIVEPEND_ADD和/或PEND_DEL ,然后删除PEND_ADD和/或PEND_DEL并保持ACTIVE

        private void disconnectClassOfService(List<Integer> sdidList) throws Exception {

            if (sdidList == null || sdidList.isEmpty()){
                return;
            }

            logger.info("Start deleting sdid : " + sdidList);
            for (Integer sdid : sdidList ) {

                List<CosPolicy2Interface> cosPolicy2InterfaceBySdid = getCosPolicy2InterfaceDAO().getCosPolicy2InterfaceBySdid(sdid);
                if (cosPolicy2InterfaceBySdid != null && !cosPolicy2InterfaceBySdid.isEmpty()){
                    for (CosPolicy2Interface cosPolicySdid: cosPolicy2InterfaceBySdid){
                        if (cosPolicySdid.getStatus().equals("PEND_ADD") ){
                            getPersistMgr().delete(cosPolicySdid);
                        }
                        if (cosPolicySdid.getStatus().equals("PEND_DEL") ){
                            getPersistMgr().delete(cosPolicySdid);
                        }
                        if (cosPolicySdid.getStatus().equals("ACTIVE") ){
                            getPersistMgr().delete(cosPolicySdid);
                        }
                    }
                }

my hibernate query 我的休眠查询

            protected static final String queryBySdid = " SELECT * "
                + " FROM cos_policy2interface "
                + " WHERE  connection_type = 'SDID' AND connection_id = ?";

        @Override
        public List<CosPolicy2Interface> getCosPolicy2InterfaceBySdid(
                long sdid) throws Exception {
            EntityManager em = getEntityManager();
            CosPolicy2Interface CosPolicy2Interface = null;
            Query query = em.createNativeQuery(queryBySdid, CosPolicy2Interface.class);
            query.setParameter(1, sdid);

            @SuppressWarnings("unchecked")
            List<CosPolicy2Interface> cosPolicy2InterfaceList = query.getResultList();
            return cosPolicy2InterfaceList;
        }

example of table: 表示例:

CosPolicy2Interface_id,connection_type, connection_id, status
29, 'SDID', 13538806, ACTIVE
30, 'SDID', 13538806, PEND_DEL
31, 'SDID', 13538806, ACTIVE
32, 'SDID', 13538806, PEND_ADD
33, 'SDID', 13538806, PEND_ADD
34, 'SDID', 13538806, ACTIVE

My logic will erased all of it since it will iterate one record at a time. Could I use a boolean? some thing like this how can I implemented.

1) Pen Add -> dont care delete (But set PendingFlag to true)  
2)Pend Del --> dont care delete (Set Pending Flag to true)  
3) Active --> Store them in a list to check later  
     Come out of the for-loop and check if pendingFlag is false and activeCosList is not empty --> delete them

I would like to modify your disconnectClassOfService method. 我想修改您的disconnectClassOfService方法。

private void disconnectClassOfService(List<Integer> sdidList) throws Exception {

    if (sdidList == null || sdidList.isEmpty()) {
        return;
    }

    logger.info("Start deleting sdid : " + sdidList);
    for (Integer sdid : sdidList) {

        List<CosPolicy2Interface> cosPolicy2InterfaceBySdid = getCosPolicy2InterfaceDAO().getCosPolicy2InterfaceBySdid(sdid);
        if (cosPolicy2InterfaceBySdid != null && !cosPolicy2InterfaceBySdid.isEmpty()) {

            List<CosPolicy2Interface> recordsACTIVE = new ArrayList<>();
            List<CosPolicy2Interface> recordsNonACTIVE = new ArrayList<>();

            for (CosPolicy2Interface cosPolicySdid : cosPolicy2InterfaceBySdid) {
                if (cosPolicySdid.getStatus().equals("PEND_ADD")) {
                    recordsNonACTIVE.add(cosPolicySdid);
                }
                if (cosPolicySdid.getStatus().equals("PEND_DEL")) {
                    recordsNonACTIVE.add(cosPolicySdid);
                }
                if (cosPolicySdid.getStatus().equals("ACTIVE")) {
                    recordsACTIVE.add(cosPolicySdid);
                }
            }
            if ((recordsACTIVE.size() == cosPolicy2InterfaceBySdid.size())) {
                getPersistMgr().deleteAll(cosPolicy2InterfaceBySdid);// Delete all records
            } else {
                // delete non active records
                getPersistMgr().deleteAll(recordsNonACTIVE);
            }
        }
    }
}

This way you don't have to delete records individually. 这样,您不必单独删除记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM