corner.orm.tapestry.component.gain.GainPoint.java Source code

Java tutorial

Introduction

Here is the source code for corner.orm.tapestry.component.gain.GainPoint.java

Source

// Copyright 2007 the original author or authors.
// site: http://www.bjmaxinfo.com
// file: $Id: GainPoint.java 4454 2009-07-13 09:01:40Z xf $
// created at:2007-06-18
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package corner.orm.tapestry.component.gain;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.IScript;
import org.apache.tapestry.PageRenderSupport;
import org.apache.tapestry.TapestryUtils;
import org.apache.tapestry.annotations.InjectScript;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidatableField;
import org.apache.tapestry.form.ValidatableFieldSupport;
import org.apache.tapestry.json.JSONArray;
import org.apache.tapestry.json.JSONObject;
import org.apache.tapestry.valid.IValidationDelegate;
import org.apache.tapestry.valid.ValidatorException;

import corner.util.StringUtils;

/**
 * 
 * ???????tableid
 * @author <a href=mailto:xf@bjmaxinfo.com>xiafei</a>
 * @version $Revision: 4454 $
 * @since 2.3.7
 */
public abstract class GainPoint extends BaseComponent implements IFormComponent, ValidatableField {

    /**
     * js
     */
    @InjectScript("GainPoint.script")
    public abstract IScript getScript();

    /**
     * ??entity
     */
    private List<List> foregroundEntitys;

    private int foregroundLength;

    private List<Object> entitys;

    /**
     * @return Returns the entitys.
     */
    protected List<Object> getEntitys() {
        return entitys;
    }

    /**
     * @param entitys The entitys to set.
     */
    protected void setEntitys(List<Object> entitys) {
        this.entitys = entitys;
    }

    /**
     * ?
     */
    public abstract List<String> getEntityPropertys();

    public abstract void setEntityPropertys(List<String> ls);

    /**
     * ?Entitys
     */
    public abstract <T> List<T> getSaveOrUpdateEntitys();

    public abstract <T> void setSaveOrUpdateEntitys(List<T> l);

    /**
     * ?Entitys
     */
    public abstract <T> List<T> getDeleteEntitys();

    public abstract <T> void setDeleteEntitys(List<T> l);

    /**
     * ??
     */
    @Parameter(required = true)
    public abstract <T> List<T> getSource();

    /**
     * 
     */
    public abstract List<GainPoint> getGainPoints();

    public abstract void setGainPoints(List<GainPoint> l);

    /**
     * ?,?(;)
     */
    @Parameter(required = true)
    public abstract String getShowPropertys();

    /**
     * 
     */
    @Parameter(required = true)
    public abstract String getEntityClass();

    /**
     * tableId,gf
     */
    @Parameter(required = true)
    public abstract String getTableId();

    /**
     * tableId,gf
     */
    @Parameter(defaultValue = "literal:poid")
    public abstract String getPagePersistentId();

    /**
     * tableId,gf
     */
    @Parameter(defaultValue = "literal:id")
    public abstract String getPersistentId();

    @Parameter(defaultValue = "literal:tr")
    public abstract String getElement();

    @Parameter(defaultValue = "literal:")
    public abstract String getCheckBoxFields();

    @Parameter(defaultValue = "literal:{}")
    public abstract String getInitFuns();

    @Parameter(defaultValue = "literal:error-div")
    public abstract String getErrorClass();

    public abstract String getErrorMessage();

    public abstract void setErrorMessage(String s);

    /**
     * 
     */
    public abstract IForm getForm();

    public abstract void setForm(IForm form);

    /**
     * Injected.
     */
    public abstract ValidatableFieldSupport getValidatableFieldSupport();

    protected boolean getRenderBodyOnRewind() {
        return false;
    }

    protected void setName(IForm form) {
        setName(form.getElementId(this));
    }

    /**
     * @return Returns the foregroundEntitys.
     */
    protected List<List> getForegroundEntitys() {
        return foregroundEntitys;
    }

    /**
     * @param foregroundEntitys The foregroundEntitys to set.
     */
    protected void setForegroundEntitys(List<List> foregroundEntitys) {
        this.foregroundEntitys = foregroundEntitys;
    }

    /**
     * @return Returns the foregroundLength.
     */
    protected int getForegroundLength() {
        return foregroundLength;
    }

    /**
     * @param foregroundLength The foregroundLength to set.
     */
    protected void setForegroundLength(int foregroundLength) {
        this.foregroundLength = foregroundLength;
    }

    /**
     * Invoked from {@link #renderComponent(IMarkupWriter, IRequestCycle)} to
     * rewind the component. If the component is
     * {@link IFormComponent#isDisabled() disabled} this will not be invoked.
     * 
     * @param writer
     * @param cycle
     */
    protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) {

        initData(); //?

        setup(cycle); //???

        entityWorkshop(); //?entity
    }

    /**
     * ???
     */
    private void setup(IRequestCycle cycle) {
        this.setForegroundEntitys(new ArrayList<List>());

        for (String s : this.getEntityPropertys()) {
            String sl[] = cycle.getParameters(s);

            if (sl != null && sl.length > 0) {//null?
                this.getForegroundEntitys().add(Arrays.asList(sl));
                this.setForegroundLength(sl.length);
            }
        }
    }

    /**
     * ?entity
     */
    private <T> void entityWorkshop() {

        Object entity = null;

        int Size = this.getForegroundLength();

        Class entityClass = null;
        try {
            entityClass = Class.forName(getEntityClass());
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }

        neatenPersistentEntity(); //??

        Object temp = null; //??

        Iterator FEList = null;

        String epname = null;

        for (int i = 0; i < Size; i++) {

            if (i < this.getPersistentSize()) {
                entity = this.getEntitys().get(i);
            } else {
                try {
                    entity = entityClass.newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            FEList = this.getForegroundEntitys().iterator();

            for (int j = 0; j < this.getForegroundEntitys().size(); j++) {

                List foregroundEntity = (List) FEList.next();

                try {
                    temp = foregroundEntity.get(i);

                    if (temp != null && !temp.equals("")) {

                        epname = this.getEntityPropertys().get(j);

                        if (getPagePersistentId().equals(epname)) { //??id????id
                            epname = getPersistentId();
                        }

                        PropertyUtils.setProperty(entity, epname, temp);
                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }

            this.getSaveOrUpdateEntitys().add(entity);
        }

        //
        delNullEntity();
    }

    /**
     * 
     */
    private void delNullEntity() {
        String prop = this.getShowPropertys().replace(this.getPagePersistentId(), getPersistentId());
        String entityPropertys[] = prop.split(",");
        int count = 0;
        Object obj = null;

        List nulllist = new ArrayList();

        for (Object bean : getSaveOrUpdateEntitys()) {
            count = 0;
            for (String name : entityPropertys) {
                try {
                    obj = PropertyUtils.getProperty(bean, name);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }

                if (obj == null || obj.toString() == "") {
                    count++;
                }

                if (count == entityPropertys.length) {
                    nulllist.add(bean);
                }
            }
        }

        for (Object bean : nulllist) {
            getSaveOrUpdateEntitys().remove(bean);
        }
    }

    /**
     * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
     */
    @Override
    protected void cleanupAfterRender(IRequestCycle cycle) {
        super.cleanupAfterRender(cycle);
        this.setEntitys(null);
        this.setForegroundEntitys(null);
    }

    /**
     * ??
     */
    private int getPersistentSize() {
        return this.getEntitys().size();
    }

    /**
     * ???entity
     */
    private <T> void neatenPersistentEntity() {

        this.setSaveOrUpdateEntitys(new ArrayList<T>());

        this.setDeleteEntitys(new ArrayList<T>());

        this.setEntitys(new ArrayList<Object>());

        String foregroundList[] = this.getPage().getRequestCycle().getParameters(this.getPagePersistentId());

        String id = null;

        for (Object entity : this.getSource()) { // ?entity
            try {
                id = (String) PropertyUtils.getProperty(entity, this.getPersistentId());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }

            this.getDeleteEntitys().add(entity); // 

            if (foregroundList != null && foregroundList.length > 0) {//null?
                for (String s : foregroundList) {
                    if (id.equals(s)) {
                        this.getDeleteEntitys().remove(this.getDeleteEntitys().size() - 1); // ??
                        this.getEntitys().add(entity);
                    }
                }
            }
        }
    }

    /**
     * ??
     */
    private void initData() {
        String entityPropertys[] = this.getShowPropertys().split(",");
        this.setEntityPropertys(Arrays.asList(entityPropertys));
    }

    /**
     * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
     *      org.apache.tapestry.IRequestCycle)
     */
    @Override
    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {

        /**
         * ?form
         */
        IForm form = TapestryUtils.getForm(cycle, this);

        setForm(form);

        if (form.wasPrerendered(writer, this))
            return;

        IValidationDelegate delegate = form.getDelegate();

        delegate.setFormComponent(this);

        setName(form);

        /**
         * ??
         */
        if (form.isRewinding()) {
            if (!isDisabled()) {
                try {
                    getValidatableFieldSupport().validate(this, writer, cycle, "");
                    rewindFormComponent(writer, cycle);

                } catch (ValidatorException e) {
                    getForm().getDelegate().record(e);
                    setErrorMessage(e.getMessage());
                }
            }

            // This is for the benefit of the couple of components (LinkSubmit)
            // that allow a body.
            // The body should render when the component rewinds.

            if (getRenderBodyOnRewind())
                renderBody(writer, cycle);
        }

        if (StringUtils.notBlank(getErrorMessage())) {
            writer.begin("span");
            writer.attribute("class", getErrorClass());
            writer.print(getErrorMessage());
            writer.end("span");
        }

        if (!cycle.isRewinding()) {

            String element = isParameterBound("element") ? getElement() : getTemplateTagName();

            writer.begin(element);
            super.renderComponent(writer, cycle);
            writer.end();

            this.initData();

            /**
             * js
             */
            PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);

            Map<String, Object> scriptParms = new HashMap<String, Object>();

            //json
            JSONObject JSONElementValues = getJSONElementValues();

            scriptParms.put("tableId", this.getTableId()); //???

            scriptParms.put("pagePersistentId", this.getPagePersistentId()); //?id

            scriptParms.put("elementSize", this.getSource().size()); //tr?

            scriptParms.put("gpid", this.getClientId()); //??gpid

            scriptParms.put("elementValues", JSONElementValues.toString()); //

            scriptParms.put("checkBoxFields", getCheckBoxs().toString()); //checkbox

            scriptParms.put("initFuns", getInitFieldFuns().toString());

            getScript().execute(this, cycle, pageRenderSupport, scriptParms);

            //TODO:js??idid?value
            //         getValidatableFieldSupport().renderContributions(this, writer, cycle);
        }
    }

    /**
     * ???js
     * {"fieldName":"funName"}
     */
    private JSONObject getInitFieldFuns() {
        JSONObject json = null;
        try {
            json = new JSONObject(this.getInitFuns());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * checkbox
     */
    private JSONArray getCheckBoxs() {
        JSONArray json = new JSONArray();
        String[] boxs = this.getCheckBoxFields().split(",");
        for (String s : boxs) {
            json.put(s);
        }
        return json;
    }

    /**
     * json
     * @param elements
     */
    private JSONObject getJSONElementValues() {

        JSONObject json = new JSONObject();

        JSONArray elementValues = null;

        String tpn = null;

        for (String propertyName : this.getEntityPropertys()) { //????

            elementValues = new JSONArray();

            for (Object entity : this.getSource()) {
                try {
                    tpn = propertyName;
                    if (getPagePersistentId().equals(propertyName)) {
                        tpn = getPersistentId();
                    }
                    elementValues.put(PropertyUtils.getProperty(entity, tpn));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }

            json.put(propertyName, elementValues);
        }

        return json;
    }

    /*
     *  ?
     * */
    private static String REQUIRED_VALIDATORS_MESSAGE = "?.";
    private static String RATE_VALIDATORS_MESSAGE = "%1$s ?100?0.";
    private static String TWENTY_FOUR_HOURS_VALIDATORS_MESSAGE = "%1$s ?24?0.";

    /**
     * ?
     * @param gp
     */
    public static void doRequiredValidators(GainPoint gp) {
        if (gp == null) {
            throw new IllegalStateException(REQUIRED_VALIDATORS_MESSAGE);
        }
        if (gp.getSaveOrUpdateEntitys() == null || gp.getSaveOrUpdateEntitys().size() == 0) {
            throw new IllegalStateException(REQUIRED_VALIDATORS_MESSAGE);
        }
    }

    /**
     * ?
     * @param detail
     */
    public static void doRateValidators(String value, String message) {
        //
        if (StringUtils.blank(value)) {
            return;
        }

        String regEx = "\\d";
        Pattern p = Pattern.compile(regEx);
        if (!p.matcher(value).find() && StringUtils.notBlank(value)) {
            throw new IllegalStateException(String.format(RATE_VALIDATORS_MESSAGE, message));
        }

        try {
            double num = Double.valueOf(value);
            if (num < 0) {
                throw new IllegalStateException(String.format(RATE_VALIDATORS_MESSAGE, message));
            }

            if (num > 100) {
                throw new IllegalStateException(String.format(RATE_VALIDATORS_MESSAGE, message));
            }
        } catch (NumberFormatException e) {
            throw new IllegalStateException(String.format(RATE_VALIDATORS_MESSAGE, message));
        }
    }

    /**
     * 24?
     * @param detail
     */
    public static void doTwentyFourHoursValidators(String value, String message) {
        //
        if (StringUtils.blank(value)) {
            return;
        }
        String regEx = "\\d";
        Pattern p = Pattern.compile(regEx);
        if (!p.matcher(value).find() && StringUtils.notBlank(value)) {
            throw new IllegalStateException(String.format(TWENTY_FOUR_HOURS_VALIDATORS_MESSAGE, message));
        }

        try {
            double num = Double.valueOf(value);
            if (num < 0) {
                throw new IllegalStateException(String.format(TWENTY_FOUR_HOURS_VALIDATORS_MESSAGE, message));
            }

            if (num > 24) {
                throw new IllegalStateException(String.format(TWENTY_FOUR_HOURS_VALIDATORS_MESSAGE, message));
            }
        } catch (NumberFormatException e) {
            throw new IllegalStateException(String.format(TWENTY_FOUR_HOURS_VALIDATORS_MESSAGE, message));
        }
    }
}