PerShing2014 / jmesa

Automatically exported from code.google.com/p/jmesa
0 stars 0 forks source link

Worksheet doesnot show the updated values on save #364

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. I have tried to use the Jmesa worksheet over Spring framework.
the changes are getting saved correctly in database on click of save button, 
but the view is reverted back to old values.It shows the updated values only 
after refresh

Here is code that i have used:

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    <display-name>JMESA</display-name>

<!--  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 
     -->
    <servlet-mapping>
        <servlet-name>JMESA</servlet-name>
        <url-pattern>*.dhtml</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>JMESA</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    </servlet>

<servlet>
        <servlet-name>worksheet</servlet-name>
        <servlet-class>org.jmesa.worksheet.servlet.WorksheetServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>worksheet</servlet-name>
        <url-pattern>*.wrk</url-pattern>
    </servlet-mapping>

<context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>jmesaPreferencesLocation</param-name>
        <param-value>WEB-INF/jmesa.properties</param-value>
</context-param>

</web-app> 

JMESA.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">

    <context:component-scan base-package="frontController" />
<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>jdbc.properties</value>

            </list>
        </property>

    </bean>
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix">
        <value>/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
<bean id="jmesaService" class="services.Impl.JmesaServices"></bean>
<bean id="jmesaDao" class="dao.Impl.JmesaDao"></bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>${jdbc.jindiname}</value>
        </property>

    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref = "dataSource"/>
    </bean> 

</beans>

controller:

package frontController;

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

import org.jmesa.model.TableModel;
import org.jmesa.web.HttpServletRequestWebContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import services.IJmesaService;
import table.WorksheetPresident;

@Controller
public class JmesaFinal {
    private IJmesaService jmesaService;

    @Inject
    public void setJmesaService(IJmesaService jmesaService) {
        this.jmesaService = jmesaService;
    }

    @RequestMapping(value = "/JmesaFinal.dhtml")
    public ModelAndView getFactors(Model model, HttpServletRequest request,HttpServletResponse response)
            throws Exception {

          ModelAndView mv = new ModelAndView("worksheet");
            String type = request.getParameter("type");

            String html = getTable(type, request, response); // set the Html in the request for the JSP
            // export will return null
            if (html == null) {
                return null;
            }
            request.setAttribute("presidents", html);

            return mv;
        }
    private String getTable(String id, HttpServletRequest request,
            HttpServletResponse response) {

        jmesaService.setWebcontext(new HttpServletRequestWebContext(request));  
         TableModel tableModel = new TableModel(id, request, response);

            tableModel.setItems(jmesaService.getDetails());

            WorksheetSaver2 worksheetSaver1 = null;
worksheetSaver1 = new WorksheetSaver2(jmesaService);

            tableModel.saveWorksheet(worksheetSaver1);
            tableModel.addRowObject(jmesaService.getNewBean());

            WorksheetPresident.setTableProperties(tableModel);

        // this will do all the work behind the scenes (e.g., save worksheet, add row object, render etc)
        String html = tableModel.render();

            // it has be called after render
            request.setAttribute("saveResults", worksheetSaver1.getSaveResults());

        return html;

}
}

worksheetSaver:

package frontController;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.jmesa.model.WorksheetSaver;
import org.jmesa.worksheet.Worksheet;
import org.jmesa.worksheet.WorksheetColumn;
import org.jmesa.worksheet.WorksheetRow;
import org.jmesa.worksheet.WorksheetRowStatus;
import org.jmesa.worksheet.editor.CheckboxWorksheetEditor;

import services.IJmesaService;
import bean.JmesaBean;

public class WorksheetSaver2 implements WorksheetSaver {

    private IJmesaService jmesaService;
    private StringBuffer saveResults = new StringBuffer();

    public WorksheetSaver2(IJmesaService jmesaService) {
        this.jmesaService = jmesaService;
    }

    public String getSaveResults() {
        return saveResults.toString();
    }

    public void saveWorksheet(Worksheet worksheet) {

        Iterator<WorksheetRow> worksheetRows = worksheet.getRows().iterator();
        Map<String, JmesaBean> presidentsAsMap = jmesaService.getBeansAsMap();

        while (worksheetRows.hasNext()) {
            boolean valid = true;
            WorksheetRow worksheetRow = worksheetRows.next();

            String uniqueValue = worksheetRow.getUniqueProperty().getValue();
            String message = null;

            if (worksheetRow.getRowStatus().equals(WorksheetRowStatus.ADD)) {
                JmesaBean newPresident = (JmesaBean) jmesaService.getNewBean(); 

                for (WorksheetColumn worksheetColumn : worksheetRow.getColumns()) {
                    valid = setProperty(worksheetColumn, newPresident) & valid;
                }

                if (valid) {
                    jmesaService.saveBean(newPresident, true);
                    message = getSuccessHtml("Saved new record: " + newPresident.getFirstName());
                } else {
                    message = getErrorHtml("Error: Not saving new record (uniqueValue: " + uniqueValue + ")");
                }

            } else if (worksheetRow.getRowStatus().equals(WorksheetRowStatus.MODIFY)) {
                JmesaBean president = presidentsAsMap.get(uniqueValue);

                for (WorksheetColumn worksheetColumn : worksheetRow.getColumns()) {
                    valid = setProperty(worksheetColumn, president) & valid;
                }

                if (valid) {
                    jmesaService.saveBean(president, false);
                    message = getSuccessHtml("Saved record: " + president.getFirstName());
                } else {
                    message = getErrorHtml("Error: Not saving record (uniqueValue: " + uniqueValue + ")");
                }

            } else if (worksheetRow.getRowStatus().equals(WorksheetRowStatus.REMOVE)) {
                valid = true;
                JmesaBean president = presidentsAsMap.get(uniqueValue);
                jmesaService.deleteBean(president);
                message = getSuccessHtml("Deleted record (uniqueValue: " + uniqueValue + ")");
            }

            if (valid) {
                worksheetRows.remove();
            }

            saveResults.append(message);
        }
    }

    private String getSuccessHtml(String message) {
        return "<span style=\"color:green\">" + message + "</span><br/>";
    }

    private String getErrorHtml(String message) {
        return "<span style=\"color:red\">" + message + "</span><br/>";
    }

    private boolean setProperty (WorksheetColumn worksheetColumn, JmesaBean president) {
        String property = worksheetColumn.getProperty();
        String changedValue = worksheetColumn.getChangedValue();

        // return success for dummy column "remove", which is used to show delete icon
        if (property.equals("remove")) {
            return true;
        }

        //System.out.println("Property: " + property + ", changedValue: " + changedValue);

        try {
            if (worksheetColumn.getProperty().equals("selected")) {
                if (changedValue.equals(CheckboxWorksheetEditor.CHECKED)) {
                    PropertyUtils.setProperty(president, property, "y");
                } else {
                    PropertyUtils.setProperty(president, property, "n");
                }
            } else if (worksheetColumn.getProperty().equals("career")) {
                // validate in API
                if ("foo".equals(changedValue)) {
                    worksheetColumn.setError("Enter valid Career");
                    return false;
                }

                // has to remove if validating in API
                worksheetColumn.removeError();
                PropertyUtils.setProperty(president, property, changedValue);
            } else {
                // set by javascript validation framework
                if (worksheetColumn.hasError()) {
                    return false;
                }
                PropertyUtils.setProperty(president, property, changedValue);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            worksheetColumn.setError("Some error occured");
            return false;
        }

        return true;
    }

}

I am unable to find out what I have missed here.Please help

Original issue reported on code.google.com by avinash2...@gmail.com on 28 Jan 2015 at 6:03