001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.tag;
020
021import javax.servlet.jsp.tagext.Tag;
022
023import lucee.commons.lang.StringUtil;
024import lucee.runtime.db.SQLCaster;
025import lucee.runtime.exp.ApplicationException;
026import lucee.runtime.exp.DatabaseException;
027import lucee.runtime.ext.tag.TagSupport;
028import lucee.runtime.tag.util.DeprecatedUtil;
029
030public class ProcParam extends TagSupport {
031
032        private ProcParamBean param=new ProcParamBean();
033
034        public void release() {
035                param=new ProcParamBean();
036                super.release();
037        }
038
039        /**
040         * @param cfsqltype The cfsqltype to set.
041         * @throws DatabaseException 
042         */
043        public void setCfsqltype(String cfsqltype) throws DatabaseException {
044                param.setType(SQLCaster.toSQLType(cfsqltype));
045        }
046
047        public void setSqltype(String type) throws DatabaseException    {
048                param.setType(SQLCaster.toSQLType(type));
049        }
050
051        /**
052         * @param ignoreNull The ignoreNull to set.
053         */
054        public void setNull(boolean _null) {
055                param.setNull(_null);
056        }
057
058        /**
059         * @param maxLength The maxLength to set.
060         */
061        public void setMaxlength(double maxLength) {
062                param.setMaxLength((int) maxLength);
063        }
064
065        /**
066         * @param scale The scale to set.
067         */
068        public void setScale(double scale) {
069                param.setScale((int) scale);
070        }
071
072        /**
073         * @param type The type to set.
074         * @throws ApplicationException 
075         */
076        public void setType(String type) throws ApplicationException {
077                type=type.trim().toLowerCase();
078                if("in".equals(type))                   param.setDirection(ProcParamBean.DIRECTION_IN);
079                else if("inout".equals(type))   param.setDirection(ProcParamBean.DIRECTION_INOUT);
080                else if("in_out".equals(type))  param.setDirection(ProcParamBean.DIRECTION_INOUT);
081                else if("outin".equals(type))   param.setDirection(ProcParamBean.DIRECTION_INOUT);
082                else if("out_in".equals(type))  param.setDirection(ProcParamBean.DIRECTION_INOUT);
083                else if("out".equals(type))             param.setDirection(ProcParamBean.DIRECTION_OUT);
084                else throw new ApplicationException("attribute type of tag procparam has an invalid value ["+type+"], valid values are [in, out, inout]");
085                
086        }
087
088        /**
089         * @param value The value to set.
090         */
091        public void setValue(Object value) {
092                param.setValue(value);
093        }
094
095        /**
096         * @param variable The variable to set.
097         */
098        public void setVariable(String variable) {
099                param.setVariable(variable);
100        }
101        public void setDbvarname(String dbvarname) {
102                DeprecatedUtil.tagAttribute(pageContext,"procparam","dbvarname");
103        }
104        
105        
106        public int doStartTag() throws ApplicationException {
107                // check 
108                if(param.getDirection()!=ProcParamBean.DIRECTION_IN && StringUtil.isEmpty(param.getVariable()))
109                        throw new ApplicationException("attribute variable of tag ProcParam is required, when attribute type has value \"out\" or \"inout\"");
110                if(param.getDirection()==ProcParamBean.DIRECTION_IN && param.getValue()==null && !param.getNull())
111                        throw new ApplicationException("attribute value of tag ProcParam is required, when attribute type has value \"in\"");
112                if(!param.getNull() && param.getValue() == null && param.getDirection()!=ProcParamBean.DIRECTION_OUT)
113            throw new ApplicationException("required attribute value is empty");
114                
115                
116                Tag parent=getParent();
117                while(parent!=null && !(parent instanceof StoredProc)) {
118                        parent=parent.getParent();
119                }
120                
121                if(parent instanceof StoredProc) {
122                        ((StoredProc)parent).addProcParam(param);
123                }
124                else {
125                        throw new ApplicationException("Wrong Context, tag ProcParam must be inside a StoredProc tag"); 
126                }
127                return SKIP_BODY;
128        }
129}