|
| 1 | +/* |
| 2 | + * To change this template, choose Tools | Templates |
| 3 | + * and open the template in the editor. |
| 4 | + */ |
| 5 | +package org.nbstudio.core.cls; |
| 6 | + |
| 7 | +import com.intersys.cache.CacheObject; |
| 8 | +import com.intersys.classes.CacheRootObject; |
| 9 | +import com.intersys.classes.Dictionary.ClassDefinition; |
| 10 | +import com.intersys.classes.Dictionary.PropertyDefinition; |
| 11 | +import com.intersys.objects.CacheException; |
| 12 | +import com.intersys.objects.Database; |
| 13 | +import com.intersys.objects.Id; |
| 14 | +import java.lang.reflect.Field; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.regex.Pattern; |
| 21 | +import org.nbstudio.syntax.cls.clsParser; |
| 22 | + |
| 23 | +/** |
| 24 | + * |
| 25 | + * @author daimor |
| 26 | + */ |
| 27 | +public class CLSUtils { |
| 28 | + |
| 29 | + private static ArrayList<String> propertiesExcludeList = new ArrayList<String>() { |
| 30 | + { |
| 31 | + add("Name"); |
| 32 | + add("Description"); |
| 33 | + add("SequenceNumber"); |
| 34 | + add("Parameters"); |
| 35 | + |
| 36 | + addList("%Dictionary.PropertyDefinition", new String[]{"Aliases", "Identity", "OnDelete", "Relationship", "Collection"}); |
| 37 | + addList("%Dictionary.ParameterDefinition", new String[]{"Default", "Expression"}); |
| 38 | + addList("%Dictionary.MethodDefinition", new String[]{"ClassMethod", "ClientMethod", "ReturnType", "FormalSpec", "Implementation", "Hash"}); |
| 39 | + addList("%Dictionary.XDataDefinition", new String[]{"Data"}); |
| 40 | + addList("%Dictionary.QueryDefinition", new String[]{"SqlQuery", "FormalSpec"}); |
| 41 | + addList("%Dictionary.TriggerDefinition", new String[]{"Code"}); |
| 42 | + addList("%Dictionary.IndexDefinition", new String[]{"Properties"}); |
| 43 | + addList("%Dictionary.ForeignKeyDefinition", new String[]{"Properties", "ReferencedClass", "ReferencedKey"}); |
| 44 | + addList("%Dictionary.ProjectionDefinition", new String[]{"Type"}); |
| 45 | + } |
| 46 | + |
| 47 | + public void addList(String clsName, String[] excludeList) { |
| 48 | + for (String string : excludeList) { |
| 49 | + add(clsName + "||" + string); |
| 50 | + } |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + private static CacheObject getmInternal(CacheRootObject obj) { |
| 55 | + CacheObject mInternal = null; |
| 56 | + try { |
| 57 | + Field privateStringField = CacheRootObject.class.getDeclaredField("mInternal"); |
| 58 | + privateStringField.setAccessible(true); |
| 59 | + mInternal = (CacheObject) privateStringField.get(obj); |
| 60 | + } catch (Exception ignoredException) { |
| 61 | + } |
| 62 | + return mInternal; |
| 63 | + } |
| 64 | + |
| 65 | + public static void setParameters(CacheRootObject obj, List<clsParser.ParameterContext> parametersList) { |
| 66 | + try { |
| 67 | + Map params = CLSUtils.getParameters(obj); |
| 68 | + params.clear(); |
| 69 | + if (parametersList == null) { |
| 70 | + return; |
| 71 | + } |
| 72 | + for (Iterator<clsParser.ParameterContext> it = parametersList.iterator(); it.hasNext();) { |
| 73 | + clsParser.ParameterContext parameterContext = it.next(); |
| 74 | + String paramName = parameterContext.paramName.getText(); |
| 75 | + String paramVal = parameterContext.paramVal.getText(); |
| 76 | + paramVal = paramVal.replaceAll("\"\"", "\""); |
| 77 | + paramVal = Pattern.compile("^\"(.*)\"$").matcher(paramVal).replaceAll("$1"); |
| 78 | + params.put(paramName, paramVal); |
| 79 | + } |
| 80 | + } catch (Exception ignoredException) { |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static java.util.Map getParameters(CacheRootObject obj) throws com.intersys.objects.CacheException { |
| 85 | + CacheObject mInternal = getmInternal(obj); |
| 86 | + com.intersys.cache.Dataholder dh = mInternal.getProperty("Parameters", true); |
| 87 | + com.intersys.cache.CacheObject cobj = dh.getCacheObject(); |
| 88 | + if (cobj == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + return (java.util.Map) (cobj.newJavaInstance()); |
| 92 | + } |
| 93 | + |
| 94 | + public static void setProperty(CacheRootObject obj, String propName, boolean value) { |
| 95 | + try { |
| 96 | + CacheObject mInternal = getmInternal(obj); |
| 97 | + com.intersys.cache.Dataholder dh = new com.intersys.cache.Dataholder(value); |
| 98 | + mInternal.setProperty(propName, dh); |
| 99 | + } catch (Exception ignoredException) { |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static void setProperty(CacheRootObject obj, String propName, String value) { |
| 104 | + try { |
| 105 | + CacheObject mInternal = getmInternal(obj); |
| 106 | + value = value.replaceAll("^\"(.*)\"$", "$1"); |
| 107 | + value = ((value == null) || (value.isEmpty())) ? null : value; |
| 108 | + com.intersys.cache.Dataholder dh = new com.intersys.cache.Dataholder(value); |
| 109 | + mInternal.setProperty(propName, dh); |
| 110 | + } catch (Exception ignoredException) { |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public static String getProperty(CacheRootObject obj, String propName, String defaultValue) { |
| 115 | + try { |
| 116 | + CacheObject mInternal = getmInternal(obj); |
| 117 | + com.intersys.cache.Dataholder dh = mInternal.getProperty(propName, false); |
| 118 | + return dh.getString(); |
| 119 | + } catch (Exception ignoredException) { |
| 120 | + return defaultValue; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public static Boolean getProperty(CacheRootObject obj, String propName, Boolean defaultValue) { |
| 125 | + try { |
| 126 | + CacheObject mInternal = getmInternal(obj); |
| 127 | + com.intersys.cache.Dataholder dh = mInternal.getProperty(propName, false); |
| 128 | + return dh.getBoolean(); |
| 129 | + } catch (Exception ignoredException) { |
| 130 | + return defaultValue; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public static java.util.Map<String, Object> getProperties(CacheRootObject obj) { |
| 135 | + HashMap<String, Object> result = new java.util.HashMap<String, Object>(); |
| 136 | + try { |
| 137 | + Database db = obj.getDatabase(); |
| 138 | + String className = obj._className(true); |
| 139 | + ClassDefinition objDef = (ClassDefinition) ClassDefinition.open(db, new Id(className), 0); |
| 140 | + |
| 141 | + List objProps = objDef.getProperties().asList(); |
| 142 | + for (Iterator it = objProps.iterator(); it.hasNext();) { |
| 143 | + PropertyDefinition prop = (PropertyDefinition) it.next(); |
| 144 | + String propName = prop.getName(); |
| 145 | + String propType = prop.getType(); |
| 146 | + String defaultValue = prop.getInitialExpression(); |
| 147 | + |
| 148 | + if ((propName.startsWith("%")) |
| 149 | + || propertiesExcludeList.contains(propName) |
| 150 | + || propertiesExcludeList.contains(className + "||" + propName) |
| 151 | + || (("InitialExpression".equals(propName)) && (obj instanceof PropertyDefinition) && (((PropertyDefinition) obj).getInitialExpression().equals("\"\""))) |
| 152 | + || (!propType.matches("(%(Library.)?CacheString)|(%(Library.)?Boolean)"))) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + defaultValue = defaultValue.replaceAll("^\"(.*)\"$", "$1"); |
| 157 | + defaultValue = defaultValue.replaceAll("\"\"", "\""); |
| 158 | + if (propType.matches("%(Library.)?CacheString")) { |
| 159 | + result.put(propName, defaultValue); |
| 160 | + } else if (propType.matches("%(Library.)?Boolean")) { |
| 161 | + result.put(propName, ("1".equals(defaultValue))); |
| 162 | + } |
| 163 | + } |
| 164 | + } catch (CacheException ex) { |
| 165 | + } |
| 166 | + return result; |
| 167 | + |
| 168 | + } |
| 169 | +} |
0 commit comments