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.orm.hibernate.naming;
020
021import lucee.runtime.Component;
022import lucee.runtime.engine.ThreadLocalPageContext;
023import lucee.runtime.exp.PageException;
024import lucee.runtime.exp.PageRuntimeException;
025import lucee.runtime.op.Caster;
026import lucee.runtime.orm.naming.NamingStrategy;
027import lucee.runtime.type.UDF;
028
029public class CFCNamingStrategy implements NamingStrategy {
030        
031        Component cfc;
032        
033        
034        public CFCNamingStrategy(String cfcName) throws PageException{
035                this.cfc=ThreadLocalPageContext.get().loadComponent(cfcName);
036        }
037        
038        
039        public Component getComponent() {
040                return cfc;
041        }
042
043
044        @Override
045        public String convertTableName(String tableName) {
046                return call("getTableName",tableName);
047        }
048
049        @Override
050        public String convertColumnName(String columnName) {
051                return call("getColumnName",columnName);
052        }
053
054        private String call(String functionName, String name) {
055                Object res = cfc.get(functionName,null);
056                if(!(res instanceof UDF)) return name;
057                
058                try {
059                        return Caster.toString(cfc.call(ThreadLocalPageContext.get(), functionName, new Object[]{name}));
060                } catch (PageException pe) {
061                        throw new PageRuntimeException(pe);
062                }
063        }
064
065        @Override
066        public String getType() {
067                return "cfc";
068        }
069
070}