lundi 21 août 2017

Checkbox value not getting updated in the database

I have created a web application. In one jsp page, I have a check box, text box, and two submit button. One submit button, just retrieves the value from DB and populate in the text box. Another submit button updates the retreived value into the table in database. If I check the check box and submit the value after i retrieved from the DB, it should be stored into DB and it is storing fine. Then if I uncheck the checkbox and submit to database. The checkbox value is not updating. The same happens vice versa if i just update the textbox and submit, the value 0 is updating in database, but after checking it again and updating to DB, the value is not updating. I have used, spring and hibernate for this. Enlighten me where am doing wrong.

Model 1 CloneSafe.java

package com.consulting.data;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import com.consulting.annotation.Setting;

@Entity
@Table(name = "clonesafe_settings")
public class CloneSafe {

    @Id
    @GeneratedValue
    @Column(name = "PK1")
    private int pk1;
    @Basic
    private String cloud_safe_id;

    @Basic
    private Boolean is_clone_safe;

    public int getPk1() {
        return pk1;
    }

    public void setPk1(int pk1) {
        this.pk1 = pk1;
    }

    @Setting(key = "cloud_safe_id")
    public String getCloud_safe_id() {
        return cloud_safe_id;
    }

    public void setCloud_safe_id(String cloud_safe_id) {
        this.cloud_safe_id = cloud_safe_id;
    }

    @Setting(key = "is_clone_safe")
    public Boolean getIs_clone_safe() {
        return is_clone_safe;
    }

    public void setIs_clone_safe(Boolean is_clone_safe) {
        this.is_clone_safe = is_clone_safe;
    }

}

Model 2 RegistryCloneSettings.java

package com.consulting.data;


public class RegistryCloneDetails {

    private String cloneCloudState;
    private String cloneSafeId;

    public String getCloneCloudState() {
        return cloneCloudState;
    }
    public void setCloneCloudState(String cloneCloudState) {
        this.cloneCloudState = cloneCloudState;
    }
    public String getCloneSafeId() {
        return cloneSafeId;
    }
    public void setCloneSafeId(String cloneSafeId) {
        this.cloneSafeId = cloneSafeId;
    }
}

Form Details CloneSafe.jsp

<form:form modelAttribute="cloneSafe" method="POST" action="cloneSafe">
                <div>
                    <form:checkbox path="is_clone_safe" />
                </div>
                <div>
                    <form:input path="cloud_safe_id" />
                <input type="submit" value="Update" name="action" />
            <input title="Submit" cancelUrl="bookextract">
</form:form>

Dao Classes CloneSafeDao.java

package com.consulting.dao;

import java.sql.Connection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.consulting.data.CloneSafe;
import com.consulting.data.RegistryCloneDetails;
import com.consulting.exception.ApplicationException;
import com.consulting.exception.SystemException;
import com.consulting.settings.service.RegistryService;



@Repository("clonesafeDao")
public class CloneSafeDao {
    private static final Logger log = LoggerFactory.getLogger(CloneSafeDao.class);

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private RegistryService registryService;

    /**
     * @param s
     */
    public void save(CloneSafe s) {

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.persist(s);
        session.getTransaction().commit();
        session.close();
    }

    /**
     * @return
     */
    public CloneSafe load() {

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Query query = session.createQuery("from CloneSafe");
        // expecting only one row
        List<CloneSafe> cloneSafe = query.list();
        session.getTransaction().commit();
        session.close();
        if (cloneSafe == null || cloneSafe.size() == 0) {
            log.debug("CloneSafe null, creating new");
            return new CloneSafe();
        }
        // expecting only one row
        return cloneSafe.get(0);
    }
    /**
     * @return
     */
    public List<CloneSafe> loadList() {

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Query query = session.createQuery("from CloneSafe");
        // expecting only one row
        List<CloneSafe> cloneSafe = query.list();
        session.getTransaction().commit();
        session.close();
        return cloneSafe;
    }

    /**
     * @param cloneSafe
     * @return
     * @throws SystemException
     * @throws ApplicationException
     */
    public int update(CloneSafe cloneSafe) throws SystemException, ApplicationException {

        Session session = sessionFactory.openSession();
        session.beginTransaction();
        List<CloneSafe> safe = loadList();
        for (CloneSafe g : safe) {
            log.debug("Cloud Safe ? : " + g.getIs_clone_safe());
        }
        if (safe.size() >= 1) {
            int pk1 = safe.get(0).getPk1();
            log.debug("PK1 : " + pk1);
            Query query = session
                    .createQuery("update CloneSafe set cloud_safe_id = " + ":cloud_safe_id where pk1 =:pk1");
            query.setString("cloud_safe_id", cloneSafe.getCloud_safe_id());
            query.setInteger("pk1", pk1);

            int result = query.executeUpdate();
            return result;
        } else if (safe.size() == 0) {
            save(cloneSafe);
            return 1;
        }
        session.close();
        return 0;
    }

    /**
     * Load the System Registry Values
     * @return RegistryCloneDetails
     */
    public RegistryCloneDetails loadCloneRegistryValues() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        try {
            RegistryCloneDetails  cloneDetails = new RegistryCloneDetails();
            String cloudSiteID = registryService.getSystemValue("cloud_site_id", null);
            cloneDetails.setCloneSafeId(cloudSiteID);
            return cloneDetails;
        } catch (Exception e) {
            //add exception logic
        }
        finally {
            session.close();
        }
        return null;
    }

}

Controller CloneSafeController.java

package com.consulting.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.service.internal.SessionFactoryServiceRegistryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.consulting.dao.CloneSafeDao;
import com.consulting.data.CloneSafe;
import com.consulting.data.RegistryCloneDetails;
import com.consulting.exception.ApplicationException;
import com.consulting.exception.SystemException;
import com.consulting.service.impl.SystemRegistryServiceImpl;
import com.consulting.settings.service.RegistryService;

@Controller
@RequestMapping("/admin")

public class CloneSafeController {

    private static final Logger _LOG = LoggerFactory.getLogger(CloneSafeController.class);

    @Autowired
    private CloneSafeDao cloneSafeDao;

    @Qualifier("Handle")
    private String Handle;

    @Autowired
    @Qualifier("Vendor")
    private String Vendor;

    /**
     * @param request
     * @param response
     * @param cloneSafe
     * @param result
     * @return
     */
    @RequestMapping(value = { "cloneSafe" }, method = RequestMethod.GET)
    public ModelAndView getSettingsPage(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("cloneSafe") CloneSafe cloneSafe, BindingResult result) {

            ModelAndView mav = new ModelAndView();
            cloneSafe = cloneSafeDao.load();
            mav.setViewName("cloneSafe");
            mav = new ModelAndView("cloneSafe", "cloneSafe", cloneSafe);
            return mav;
    }

    /**
     * @param request
     * @param response
     * @param cloneSafe
     * @param result
     * @return
     */
    @RequestMapping(value = { "cloneSafe" }, method = RequestMethod.POST)
    public ModelAndView saveCloneSafeSettings(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute("cloneSafe") CloneSafe cloneSafe, BindingResult result) {

            ModelAndView mav = new ModelAndView();
            try {
                int i =cloneSafeDao.update(cloneSafe);
            } catch (Exception e){
                //Add Logic
                _LOG.debug("Errored....");
            }
            mav.setViewName("cloneSafe");
            mav = new ModelAndView("cloneSafe", "cloneSafe", cloneSafe);
            return mav;
    }

    /**
     * Load the System  Registry Values
     * @param request
     * @param response
     * @param cloneSafe
     * @param result
     * @param model
     * @return
     * @throws SystemException
     * @throws ApplicationException
     */
    @RequestMapping(value = { "cloneSafe" }, params = "action", method = RequestMethod.POST)
    public ModelAndView loadCloneSafeDetails(HttpServletRequest request, 
            HttpServletResponse response,@ModelAttribute("cloneSafe") CloneSafe cloneSafe, 
            BindingResult result, ModelMap model){

        ModelAndView mav = new ModelAndView();
        RegistryCloneDetails cloneDetails= cloneSafeDao.loadCloneRegistryValues();
        if(null!=cloneDetails)
        {
            if(null!=cloneDetails.getCloneSafeId())
            {
                cloneSafe.setCloud_safe_id(cloneDetails.getCloneSafeId());
            }
        }
        mav.setViewName("cloneSafe");
        mav = new ModelAndView("cloneSafe", "cloneSafe", cloneSafe);
        return mav;
    }

}

What mistake am making, why the checkbox value is not updating after it is getting updated for the first time. Where i need to change the code. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire