Search in sources :

Example 1 with AcsJCDBFieldDoesNotExistEx

use of alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx in project ACS by ACS-Community.

the class HibernateWDAOImpl method getField.

public Object getField(String path) throws AcsJCDBFieldDoesNotExistEx {
    // backward compatibility
    final String CHARACTERISTICS_KEY = "_characteristics";
    final String ATTRIBUTES_KEY = "_attributes";
    final String ELEMENTS_KEY = "_elements";
    final String SUBNODES_KEY = "_subnodes";
    boolean subnodesRequest = false;
    boolean elementsRequest = false;
    boolean attributesRequest = false;
    if (path.endsWith(CHARACTERISTICS_KEY)) {
        path = path.substring(0, path.length() - CHARACTERISTICS_KEY.length());
        attributesRequest = elementsRequest = subnodesRequest = true;
    } else if (path.endsWith(ATTRIBUTES_KEY)) {
        path = path.substring(0, path.length() - ATTRIBUTES_KEY.length());
        attributesRequest = true;
    } else if (path.endsWith(ELEMENTS_KEY)) {
        path = path.substring(0, path.length() - ELEMENTS_KEY.length());
        elementsRequest = true;
    } else if (path.endsWith(SUBNODES_KEY)) {
        path = path.substring(0, path.length() - SUBNODES_KEY.length());
        subnodesRequest = true;
    }
    Object field = DOMJavaClassIntrospector.getNode(path, m_rootNode);
    if (field == null) {
        AcsJCDBFieldDoesNotExistEx e2 = new AcsJCDBFieldDoesNotExistEx();
        e2.setFieldName(path);
        throw e2;
    }
    // request for elements/attibutes (empty name)
    if (path.length() == 0 || path.charAt(path.length() - 1) == '/') {
        // JDAL return attributes and subnodes
        String[] attributes = attributesRequest ? DOMJavaClassIntrospector.getFields(field) : new String[0];
        String[] elements = elementsRequest ? DOMJavaClassIntrospector.getElements(field) : new String[0];
        String[] subnodes = subnodesRequest ? DOMJavaClassIntrospector.getSubnodes(field) : new String[0];
        String[] concat = new String[attributes.length + elements.length + subnodes.length];
        System.arraycopy(attributes, 0, concat, 0, attributes.length);
        System.arraycopy(elements, 0, concat, attributes.length, elements.length);
        System.arraycopy(subnodes, 0, concat, attributes.length + elements.length, subnodes.length);
        field = concat;
    }
    // automatic conversion
    if (field instanceof ConvertToPrimitiveFeature)
        field = ((ConvertToPrimitiveFeature) field).convert();
    // array support
    if (field instanceof Element) {
        NodeList childList = ((Element) field).getChildNodes();
        int childCount = childList.getLength();
        StringBuffer strignifiedArray = new StringBuffer();
        for (int i = 0; i < childCount; i++) {
            Node childNode = childList.item(i);
            if (childNode instanceof Element && childNode.getAttributes().getLength() > 0) {
                if (strignifiedArray.length() > 0)
                    strignifiedArray.append(',');
                strignifiedArray.append(childNode.getAttributes().item(0).getTextContent());
            }
        }
        field = strignifiedArray.toString();
    }
    if (!m_silent)
        m_logger.log(AcsLogLevel.NOTICE, "DAO: '" + m_name + "' returned '" + path + "' = '" + (field.getClass().isArray() ? DOMJavaClassIntrospector.stringifyArray(field) : field) + "'");
    return field;
}
Also used : ConvertToPrimitiveFeature(com.cosylab.cdb.jdal.hibernate.ConvertToPrimitiveFeature) AcsJCDBFieldDoesNotExistEx(alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 2 with AcsJCDBFieldDoesNotExistEx

use of alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx in project ACS by ACS-Community.

the class HibernateWDAOImpl method set_double.

/* (non-Javadoc)
	 * @see com.cosylab.CDB.WDAOOperations#set_double(java.lang.String, double)
	 */
@SuppressWarnings("unchecked")
public void set_double(String propertyName, double value) throws CDBFieldIsReadOnlyEx, CDBFieldDoesNotExistEx {
    NodeAndMutator nodeAndMutator = DOMJavaClassIntrospector.getRecursiveMutatorMethod(propertyName, m_rootNode);
    if (nodeAndMutator == null) {
        AcsJCDBFieldDoesNotExistEx ex = new AcsJCDBFieldDoesNotExistEx();
        ex.setFieldName(propertyName);
        throw ex.toCDBFieldDoesNotExistEx();
    }
    Transaction tr = null;
    try {
        if (nodeAndMutator.mutator.getParameterTypes().length != 1) {
            AcsJCDBFieldIsReadOnlyEx acsex = new AcsJCDBFieldIsReadOnlyEx();
            acsex.setFieldName(propertyName);
            throw acsex.toCDBFieldIsReadOnlyEx();
        }
        Object toSet;
        Class parameterClass = nodeAndMutator.mutator.getParameterTypes()[0];
        if (parameterClass.isAssignableFrom(String.class))
            toSet = String.valueOf(value);
        else if (parameterClass.isAssignableFrom(Integer.class) || parameterClass.isAssignableFrom(int.class))
            toSet = Integer.valueOf((int) value);
        else if (parameterClass.isAssignableFrom(Long.class) || parameterClass.isAssignableFrom(long.class))
            toSet = Long.valueOf((long) value);
        else if (parameterClass.isAssignableFrom(Byte.class) || parameterClass.isAssignableFrom(byte.class))
            toSet = Byte.valueOf((byte) value);
        else if (parameterClass.isAssignableFrom(Short.class) || parameterClass.isAssignableFrom(short.class))
            toSet = Short.valueOf((short) value);
        else if (parameterClass.isAssignableFrom(Double.class) || parameterClass.isAssignableFrom(double.class))
            toSet = Double.valueOf(value);
        else if (parameterClass.isAssignableFrom(Float.class) || parameterClass.isAssignableFrom(float.class))
            toSet = Float.valueOf((float) value);
        else if (parameterClass.isAssignableFrom(Boolean.class) || parameterClass.isAssignableFrom(boolean.class))
            toSet = (value != 0);
        else
            throw new NO_RESOURCES("cannot convert value");
        if (m_autoCommit)
            tr = m_session.beginTransaction();
        nodeAndMutator.mutator.invoke(nodeAndMutator.node, new Object[] { toSet });
        if (tr != null)
            tr.commit();
    } catch (Throwable th) {
        if (tr != null)
            tr.rollback();
        if (!m_silent)
            m_logger.log(AcsLogLevel.NOTICE, "Failed to set '" + value + "' to : " + (this.m_name + "/" + propertyName), th);
        throw new NO_RESOURCES(th.getMessage());
    }
}
Also used : NodeAndMutator(com.cosylab.cdb.jdal.hibernate.DOMJavaClassIntrospector.NodeAndMutator) AcsJCDBFieldDoesNotExistEx(alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx) Transaction(org.hibernate.Transaction) AcsJCDBFieldIsReadOnlyEx(alma.cdbErrType.wrappers.AcsJCDBFieldIsReadOnlyEx) NO_RESOURCES(org.omg.CORBA.NO_RESOURCES)

Example 3 with AcsJCDBFieldDoesNotExistEx

use of alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx in project ACS by ACS-Community.

the class HibernateWDAOImpl method get_double_seq.

public double[] get_double_seq(String propertyName) throws WrongCDBDataTypeEx, CDBFieldDoesNotExistEx {
    Object objectValue;
    try {
        objectValue = getField(propertyName);
    } catch (AcsJCDBFieldDoesNotExistEx e) {
        throw e.toCDBFieldDoesNotExistEx();
    }
    // no conversion needed
    if (objectValue instanceof double[])
        return (double[]) objectValue;
    Class<? extends Object> type = objectValue.getClass();
    if (objectValue instanceof String) {
        String[] tokens = ((String) objectValue).split(",");
        double[] retVal = new double[tokens.length];
        try {
            for (int i = 0; i < tokens.length; i++) retVal[i] = Double.parseDouble(tokens[i].trim());
            return retVal;
        } catch (NullPointerException npe) {
            if (!m_silent)
                m_logger.log(AcsLogLevel.NOTICE, "Failed to cast '" + objectValue + "' to dluble[].");
            AcsJWrongCDBDataTypeEx e2 = new AcsJWrongCDBDataTypeEx();
            e2.setValue(objectValue.getClass().toString());
            e2.setDataType("double[]");
            throw e2.toWrongCDBDataTypeEx();
        }
    } else if (!type.isArray() || !type.getComponentType().isAssignableFrom(Number.class)) {
        if (!m_silent)
            m_logger.log(AcsLogLevel.NOTICE, "Failed to cast '" + objectValue + "' to double[].");
        AcsJWrongCDBDataTypeEx e2 = new AcsJWrongCDBDataTypeEx();
        e2.setValue(objectValue.getClass().toString());
        e2.setDataType("double[]");
        throw e2.toWrongCDBDataTypeEx();
    }
    // do fancy conversion
    int len = Array.getLength(objectValue);
    double[] seq = new double[len];
    for (int i = 0; i < len; i++) seq[i] = ((Number) Array.get(objectValue, i)).doubleValue();
    return seq;
}
Also used : AcsJWrongCDBDataTypeEx(alma.cdbErrType.wrappers.AcsJWrongCDBDataTypeEx) AcsJCDBFieldDoesNotExistEx(alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx)

Example 4 with AcsJCDBFieldDoesNotExistEx

use of alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx in project ACS by ACS-Community.

the class HibernateWDAOImpl method get_string.

@SuppressWarnings("unchecked")
public String get_string(String propertyName) throws WrongCDBDataTypeEx, CDBFieldDoesNotExistEx {
    Object objectValue;
    try {
        objectValue = getField(propertyName);
    } catch (AcsJCDBFieldDoesNotExistEx e) {
        throw e.toCDBFieldDoesNotExistEx();
    }
    try {
        if (objectValue.getClass().isArray())
            return DOMJavaClassIntrospector.stringifyArray(objectValue);
        if (objectValue instanceof Map) {
            Object[] arr = ((Map) objectValue).keySet().toArray();
            StringBuilder sb = new StringBuilder();
            boolean first = true;
            for (Object a : arr) {
                if (first)
                    first = false;
                else
                    sb.append(',');
                sb.append((String) a);
            }
            return sb.toString();
        }
        Class<? extends Object> valueType = objectValue.getClass();
        if (!DOMJavaClassIntrospector.isPrimitive(valueType))
            throw new ClassCastException(valueType + " not a primitive/string/array");
        objectValue = DOMJavaClassIntrospector.handleInfinity(objectValue);
        return objectValue.toString();
    } catch (ClassCastException nfe) {
        if (!m_silent)
            m_logger.log(AcsLogLevel.NOTICE, "Failed to cast '" + objectValue + "' to string: " + nfe);
        AcsJWrongCDBDataTypeEx e2 = new AcsJWrongCDBDataTypeEx(nfe);
        e2.setValue(objectValue.toString());
        e2.setDataType("string");
        throw e2.toWrongCDBDataTypeEx();
    }
}
Also used : AcsJWrongCDBDataTypeEx(alma.cdbErrType.wrappers.AcsJWrongCDBDataTypeEx) AcsJCDBFieldDoesNotExistEx(alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx) Map(java.util.Map)

Example 5 with AcsJCDBFieldDoesNotExistEx

use of alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx in project ACS by ACS-Community.

the class HibernateWDAOImpl method get_string_seq.

@SuppressWarnings("unchecked")
public String[] get_string_seq(String propertyName) throws WrongCDBDataTypeEx, CDBFieldDoesNotExistEx {
    Object objectValue;
    try {
        objectValue = getField(propertyName);
    } catch (AcsJCDBFieldDoesNotExistEx e) {
        throw e.toCDBFieldDoesNotExistEx();
    }
    // no conversion needed
    if (objectValue instanceof String[])
        return (String[]) objectValue;
    // Map keys
    if (objectValue instanceof Map) {
        Map map = (Map) objectValue;
        String[] retVal = new String[map.size()];
        int i = 0;
        for (Object obj : map.keySet()) retVal[i++] = (String) obj;
        return retVal;
    }
    Class<? extends Object> type = objectValue.getClass();
    if (objectValue instanceof String) {
        String[] tokens = ((String) objectValue).split(",");
        for (int i = 0; i < tokens.length; i++) tokens[i] = tokens[i].trim();
        return tokens;
    } else if (!type.isArray() || !DOMJavaClassIntrospector.isPrimitive(type.getComponentType())) {
        if (!m_silent) {
            // TODO take out the dummy exception. Now needed for debugging an OMC/TMCDB issue.
            m_logger.log(AcsLogLevel.NOTICE, "DAO '" + m_name + "' failed to cast to String[] the property '" + propertyName + "' of type '" + type.toString() + "' with value " + objectValue + "'.", new Exception("just for stack trace"));
        }
        AcsJWrongCDBDataTypeEx e2 = new AcsJWrongCDBDataTypeEx();
        e2.setValue(objectValue.getClass().toString());
        e2.setDataType("String[]");
        throw e2.toWrongCDBDataTypeEx();
    }
    // do fancy conversion
    int len = Array.getLength(objectValue);
    String[] seq = new String[len];
    for (int i = 0; i < len; i++) seq[i] = DOMJavaClassIntrospector.handleInfinity(Array.get(objectValue, i)).toString();
    return seq;
}
Also used : AcsJWrongCDBDataTypeEx(alma.cdbErrType.wrappers.AcsJWrongCDBDataTypeEx) AcsJCDBFieldDoesNotExistEx(alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx) Map(java.util.Map)

Aggregations

AcsJCDBFieldDoesNotExistEx (alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx)18 Transaction (org.hibernate.Transaction)7 AcsJCDBFieldIsReadOnlyEx (alma.cdbErrType.wrappers.AcsJCDBFieldIsReadOnlyEx)6 AcsJWrongCDBDataTypeEx (alma.cdbErrType.wrappers.AcsJWrongCDBDataTypeEx)6 NodeAndMutator (com.cosylab.cdb.jdal.hibernate.DOMJavaClassIntrospector.NodeAndMutator)6 NO_RESOURCES (org.omg.CORBA.NO_RESOURCES)6 StringTokenizer (java.util.StringTokenizer)5 ArrayList (java.util.ArrayList)3 AcsJCDBExceptionEx (alma.cdbErrType.wrappers.AcsJCDBExceptionEx)2 AcsJCDBXMLErrorEx (alma.cdbErrType.wrappers.AcsJCDBXMLErrorEx)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 CDBRecordDoesNotExistEx (alma.cdbErrType.CDBRecordDoesNotExistEx)1 CDBRecordIsReadOnlyEx (alma.cdbErrType.CDBRecordIsReadOnlyEx)1 AcsJCDBRecordDoesNotExistEx (alma.cdbErrType.wrappers.AcsJCDBRecordDoesNotExistEx)1 ConvertToPrimitiveFeature (com.cosylab.cdb.jdal.hibernate.ConvertToPrimitiveFeature)1 XMLSaver (com.cosylab.cdb.jdal.hibernate.DOMJavaClassIntrospector.XMLSaver)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Element (org.w3c.dom.Element)1