Search in sources :

Example 1 with VersionException

use of com.twinsoft.convertigo.engine.VersionException in project convertigo by convertigo.

the class DatabaseObject method read.

public static DatabaseObject read(Node node) throws EngineException {
    String objectClassName = "n/a";
    String childNodeName = "n/a";
    String propertyName = "n/a";
    String propertyValue = "n/a";
    DatabaseObject databaseObject = null;
    Element element = (Element) node;
    objectClassName = element.getAttribute("classname");
    // Migration to 4.x+ projects
    if (objectClassName.equals("com.twinsoft.convertigo.beans.core.ScreenClass")) {
        objectClassName = "com.twinsoft.convertigo.beans.screenclasses.JavelinScreenClass";
    }
    String version = element.getAttribute("version");
    if ("".equals(version)) {
        version = node.getOwnerDocument().getDocumentElement().getAttribute("beans");
        if (!"".equals(version)) {
            element.setAttribute("version", version);
        }
    }
    // Verifying product version
    if ("com.twinsoft.convertigo.beans.core.Project".equals(objectClassName) && VersionUtils.compareProductVersion(Version.productVersion, version) < 0) {
        VersionException ee = new VersionException(version);
        throw ee;
    }
    try {
        Engine.logBeans.trace("Creating object of class \"" + objectClassName + "\"");
        databaseObject = (DatabaseObject) Class.forName(objectClassName).getConstructor().newInstance();
    } catch (Exception e) {
        // XMLUtils.prettyPrintDOM(node);
        String s = node.getNodeName();
        String message = "Unable to create a new instance of the object from the serialized XML data.\n" + "Object class: '" + objectClassName + "'\n" + "Object version: " + version + "\n" + "XML data:\n" + s;
        EngineException ee = new EngineException(message, e);
        throw ee;
    }
    databaseObject.isImporting = true;
    try {
        // Performs custom configuration before object de-serialization
        databaseObject.preconfigure(element);
    } catch (Exception e) {
        String s = XMLUtils.prettyPrintDOM(node);
        String message = "Unable to configure the object from serialized XML data before its creation.\n" + "Object class: '" + objectClassName + "'\n" + "XML data:\n" + s;
        EngineException ee = new EngineException(message, e);
        throw ee;
    }
    try {
        long priority = Long.valueOf(element.getAttribute("priority")).longValue();
        databaseObject.priority = priority;
        Class<? extends DatabaseObject> databaseObjectClass = databaseObject.getClass();
        BeanInfo bi = CachedIntrospector.getBeanInfo(databaseObjectClass);
        PropertyDescriptor[] pds = bi.getPropertyDescriptors();
        NodeList childNodes = element.getChildNodes();
        int len = childNodes.getLength();
        PropertyDescriptor pd;
        Object propertyObjectValue;
        Class<?> propertyType;
        NamedNodeMap childAttributes;
        boolean maskValue = false;
        for (int i = 0; i < len; i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            Element childElement = (Element) childNode;
            childNodeName = childNode.getNodeName();
            Engine.logBeans.trace("Analyzing node '" + childNodeName + "'");
            if (childNodeName.equalsIgnoreCase("property")) {
                childAttributes = childNode.getAttributes();
                propertyName = childAttributes.getNamedItem("name").getNodeValue();
                Engine.logBeans.trace("  name = '" + propertyName + "'");
                pd = findPropertyDescriptor(pds, propertyName);
                if (pd == null) {
                    Engine.logBeans.info("Unable to find the definition of property \"" + propertyName + "\"; skipping.");
                    continue;
                }
                propertyType = pd.getPropertyType();
                propertyObjectValue = XMLUtils.readObjectFromXml((Element) XMLUtils.findChildNode(childNode, Node.ELEMENT_NODE));
                // Hides value in log trace if needed
                if ("false".equals(childElement.getAttribute("traceable"))) {
                    maskValue = true;
                }
                Engine.logBeans.trace("  value='" + (maskValue ? Visibility.maskValue(propertyObjectValue) : propertyObjectValue) + "'");
                // Decrypts value if needed
                try {
                    if ("true".equals(childElement.getAttribute("ciphered"))) {
                        propertyObjectValue = decryptPropertyValue(propertyObjectValue);
                    }
                } catch (Exception e) {
                }
                propertyObjectValue = databaseObject.compileProperty(propertyType, propertyName, propertyObjectValue);
                if (Enum.class.isAssignableFrom(propertyType)) {
                    propertyObjectValue = EnumUtils.valueOf(propertyType, propertyObjectValue);
                }
                propertyValue = propertyObjectValue.toString();
                Method setter = pd.getWriteMethod();
                Engine.logBeans.trace("  setter='" + setter.getName() + "'");
                Engine.logBeans.trace("  param type='" + propertyObjectValue.getClass().getName() + "'");
                Engine.logBeans.trace("  expected type='" + propertyType.getName() + "'");
                try {
                    setter.invoke(databaseObject, new Object[] { propertyObjectValue });
                } catch (InvocationTargetException e) {
                    Throwable targetException = e.getTargetException();
                    Engine.logBeans.warn("Unable to set the property '" + propertyName + "' for the object '" + databaseObject.getName() + "' (" + objectClassName + "): [" + targetException.getClass().getName() + "] " + targetException.getMessage());
                }
                if (Boolean.TRUE.equals(pd.getValue("nillable"))) {
                    Node nodeNull = childAttributes.getNamedItem("isNull");
                    String valNull = (nodeNull == null) ? "false" : nodeNull.getNodeValue();
                    Engine.logBeans.trace("  treats as null='" + valNull + "'");
                    try {
                        Method method = databaseObject.getClass().getMethod("setNullProperty", new Class[] { String.class, Boolean.class });
                        method.invoke(databaseObject, new Object[] { propertyName, Boolean.valueOf(valNull) });
                    } catch (Exception ex) {
                        Engine.logBeans.warn("Unable to set the 'isNull' attribute for property '" + propertyName + "' of '" + databaseObject.getName() + "' object");
                    }
                }
            }
        }
    } catch (Exception e) {
        String message = "Unable to set the object properties from the serialized XML data.\n" + "Object class: '" + objectClassName + "'\n" + "XML analyzed node: " + childNodeName + "\n" + "Property name: " + propertyName + "\n" + "Property value: " + propertyValue;
        EngineException ee = new EngineException(message, e);
        throw ee;
    }
    try {
        // Performs custom configuration
        databaseObject.configure(element);
    } catch (Exception e) {
        String s = XMLUtils.prettyPrintDOM(node);
        String message = "Unable to configure the object from serialized XML data after its creation.\n" + "Object class: '" + objectClassName + "'\n" + "XML data:\n" + s;
        EngineException ee = new EngineException(message, e);
        throw ee;
    }
    return databaseObject;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) PropertyDescriptor(java.beans.PropertyDescriptor) Element(org.w3c.dom.Element) BeanInfo(java.beans.BeanInfo) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EngineException(com.twinsoft.convertigo.engine.EngineException) Method(java.lang.reflect.Method) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EngineException(com.twinsoft.convertigo.engine.EngineException) DatabaseObjectNotFoundException(com.twinsoft.convertigo.engine.DatabaseObjectNotFoundException) VersionException(com.twinsoft.convertigo.engine.VersionException) UndefinedSymbolsException(com.twinsoft.convertigo.engine.UndefinedSymbolsException) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException) InvocationTargetException(java.lang.reflect.InvocationTargetException) VersionException(com.twinsoft.convertigo.engine.VersionException)

Aggregations

DatabaseObjectNotFoundException (com.twinsoft.convertigo.engine.DatabaseObjectNotFoundException)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 ObjectWithSameNameException (com.twinsoft.convertigo.engine.ObjectWithSameNameException)1 UndefinedSymbolsException (com.twinsoft.convertigo.engine.UndefinedSymbolsException)1 VersionException (com.twinsoft.convertigo.engine.VersionException)1 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1