001 package railo.runtime.type.util; 002 003 import railo.commons.lang.StringUtil; 004 import railo.runtime.Component; 005 import railo.runtime.ComponentImpl; 006 import railo.runtime.component.Member; 007 import railo.runtime.component.Property; 008 import railo.runtime.exp.PageException; 009 import railo.runtime.op.Caster; 010 import railo.runtime.type.Collection; 011 import railo.runtime.type.Collection.Key; 012 import railo.runtime.type.KeyImpl; 013 import railo.runtime.type.UDF; 014 import railo.runtime.type.UDFAddProperty; 015 import railo.runtime.type.UDFGetterProperty; 016 import railo.runtime.type.UDFHasProperty; 017 import railo.runtime.type.UDFRemoveProperty; 018 import railo.runtime.type.UDFSetterProperty; 019 020 public class PropertyFactory { 021 022 public static final Collection.Key SINGULAR_NAME = KeyImpl.intern("singularName"); 023 public static final Key FIELD_TYPE = KeyImpl.intern("fieldtype"); 024 025 026 public static void createPropertyUDFs(ComponentImpl comp, Property property) throws PageException { 027 // getter 028 if(property.getGetter()){ 029 PropertyFactory.addGet(comp,property); 030 } 031 // setter 032 if(property.getSetter()){ 033 PropertyFactory.addSet(comp,property); 034 } 035 036 String fieldType = Caster.toString(property.getDynamicAttributes().get(PropertyFactory.FIELD_TYPE,null),null); 037 038 // add 039 if(fieldType!=null) { 040 if("one-to-many".equalsIgnoreCase(fieldType) || "many-to-many".equalsIgnoreCase(fieldType)) { 041 PropertyFactory.addHas(comp,property); 042 PropertyFactory.addAdd(comp,property); 043 PropertyFactory.addRemove(comp,property); 044 } 045 else if("one-to-one".equalsIgnoreCase(fieldType) || "many-to-one".equalsIgnoreCase(fieldType)) { 046 PropertyFactory.addHas(comp,property); 047 } 048 } 049 } 050 051 052 public static void addGet(ComponentImpl comp, Property prop) { 053 Member m = comp.getMember(Component.ACCESS_PRIVATE,KeyImpl.getInstance("get"+prop.getName()),true,false); 054 if(!(m instanceof UDF)){ 055 UDF udf = new UDFGetterProperty(comp,prop); 056 comp.registerUDF(udf.getFunctionName(), udf); 057 } 058 } 059 060 public static void addSet(ComponentImpl comp, Property prop) throws PageException { 061 Member m = comp.getMember(Component.ACCESS_PRIVATE,KeyImpl.getInstance("set"+prop.getName()),true,false); 062 if(!(m instanceof UDF)){ 063 UDF udf = new UDFSetterProperty(comp,prop); 064 comp.registerUDF(udf.getFunctionName(), udf); 065 } 066 } 067 068 public static void addHas(ComponentImpl comp, Property prop) { 069 Member m = comp.getMember(Component.ACCESS_PRIVATE,KeyImpl.getInstance("has"+getSingularName(prop)),true,false); 070 if(!(m instanceof UDF)){ 071 UDF udf = new UDFHasProperty(comp,prop); 072 comp.registerUDF(udf.getFunctionName(), udf); 073 } 074 } 075 076 public static void addAdd(ComponentImpl comp, Property prop) { 077 Member m = comp.getMember(ComponentImpl.ACCESS_PRIVATE,KeyImpl.getInstance("add"+getSingularName(prop)),true,false); 078 if(!(m instanceof UDF)){ 079 UDF udf = new UDFAddProperty(comp,prop); 080 comp.registerUDF(udf.getFunctionName(), udf); 081 } 082 } 083 084 public static void addRemove(ComponentImpl comp, Property prop) { 085 Member m = comp.getMember(Component.ACCESS_PRIVATE,KeyImpl.getInstance("remove"+getSingularName(prop)),true,false); 086 if(!(m instanceof UDF)){ 087 UDF udf = new UDFRemoveProperty(comp,prop); 088 comp.registerUDF(udf.getFunctionName(), udf); 089 } 090 } 091 092 public static String getSingularName(Property prop) { 093 String singularName=Caster.toString(prop.getDynamicAttributes().get(SINGULAR_NAME,null),null); 094 if(!StringUtil.isEmpty(singularName)) return singularName; 095 return prop.getName(); 096 } 097 098 public static String getType(Property prop){ 099 String type = prop.getType(); 100 if(StringUtil.isEmpty(type) || "any".equalsIgnoreCase(type) || "object".equalsIgnoreCase(type)){ 101 String fieldType = Caster.toString(prop.getDynamicAttributes().get(FIELD_TYPE,null),null); 102 if("one-to-many".equalsIgnoreCase(fieldType) || "many-to-many".equalsIgnoreCase(fieldType)){ 103 return "array"; 104 } 105 return "any"; 106 } 107 return type; 108 } 109 110 }