001    package railo.runtime.tag;
002    
003    import javax.servlet.jsp.tagext.Tag;
004    
005    import railo.commons.lang.StringUtil;
006    import railo.runtime.db.SQLCaster;
007    import railo.runtime.exp.ApplicationException;
008    import railo.runtime.exp.DatabaseException;
009    import railo.runtime.ext.tag.TagSupport;
010    import railo.runtime.tag.util.DeprecatedUtil;
011    
012    public class ProcParam extends TagSupport {
013    
014            private ProcParamBean param=new ProcParamBean();
015    
016            public void release() {
017                    param=new ProcParamBean();
018                    super.release();
019            }
020    
021            /**
022             * @param cfsqltype The cfsqltype to set.
023             * @throws DatabaseException 
024             */
025            public void setCfsqltype(String cfsqltype) throws DatabaseException {
026                    param.setType(SQLCaster.toIntType(cfsqltype));
027            }
028    
029            /**
030             * @param ignoreNull The ignoreNull to set.
031             */
032            public void setNull(boolean _null) {
033                    param.setNull(_null);
034            }
035    
036            /**
037             * @param maxLength The maxLength to set.
038             */
039            public void setMaxlength(double maxLength) {
040                    param.setMaxLength((int) maxLength);
041            }
042    
043            /**
044             * @param scale The scale to set.
045             */
046            public void setScale(double scale) {
047                    param.setScale((int) scale);
048            }
049    
050            /**
051             * @param type The type to set.
052             * @throws ApplicationException 
053             */
054            public void setType(String type) throws ApplicationException {
055                    type=type.trim().toLowerCase();
056                    if("in".equals(type))                   param.setDirection(ProcParamBean.DIRECTION_IN);
057                    else if("inout".equals(type))   param.setDirection(ProcParamBean.DIRECTION_INOUT);
058                    else if("in_out".equals(type))  param.setDirection(ProcParamBean.DIRECTION_INOUT);
059                    else if("outin".equals(type))   param.setDirection(ProcParamBean.DIRECTION_INOUT);
060                    else if("out_in".equals(type))  param.setDirection(ProcParamBean.DIRECTION_INOUT);
061                    else if("out".equals(type))             param.setDirection(ProcParamBean.DIRECTION_OUT);
062                    else throw new ApplicationException("attribute type of tag procparam has a invalid value ["+type+"], valid values are [in, out, inout]");
063                    
064            }
065    
066            /**
067             * @param value The value to set.
068             */
069            public void setValue(Object value) {
070                    param.setValue(value);
071            }
072    
073            /**
074             * @param variable The variable to set.
075             */
076            public void setVariable(String variable) {
077                    param.setVariable(variable);
078            }
079            public void setDbvarname(String dbvarname) {
080                    DeprecatedUtil.tagAttribute(pageContext,"procparam","dbvarname");
081            }
082            
083            
084            public int doStartTag() throws ApplicationException {
085                    // check 
086                    if(param.getDirection()!=ProcParamBean.DIRECTION_IN && StringUtil.isEmpty(param.getVariable()))
087                            throw new ApplicationException("attribute variable of tag ProcParam is required, when attribute type has value \"out\" or \"inout\"");
088                    if(param.getDirection()==ProcParamBean.DIRECTION_IN && param.getValue()==null && !param.getNull())
089                            throw new ApplicationException("attribute value of tag ProcParam is required, when attribute type has value \"in\"");
090                    if(!param.getNull() && param.getValue() == null && param.getDirection()!=ProcParamBean.DIRECTION_OUT)
091                throw new ApplicationException("required attribute value is empty");
092                    
093                    
094                    Tag parent=getParent();
095                    while(parent!=null && !(parent instanceof StoredProc)) {
096                            parent=parent.getParent();
097                    }
098                    
099                    if(parent instanceof StoredProc) {
100                            ((StoredProc)parent).addProcParam(param);
101                    }
102                    else {
103                            throw new ApplicationException("Wrong Context, tag ProcParam must be inside a StoredProc tag"); 
104                    }
105                    return SKIP_BODY;
106            }
107    }