Search in sources :

Example 6 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 7 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 8 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)

Example 9 with AcsJCDBFieldDoesNotExistEx

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

the class HibernateWDAOImpl method set_string.

/*
	 * (non-Javadoc)
	 * 
	 * @see com.cosylab.CDB.WDAOOperations#set_string(java.lang.String,
	 * java.lang.String)
	 */
@SuppressWarnings("unchecked")
public void set_string(String propertyName, String 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(value);
        else if (parameterClass.isAssignableFrom(Long.class) || parameterClass.isAssignableFrom(long.class))
            toSet = Long.valueOf(value);
        else if (parameterClass.isAssignableFrom(Byte.class) || parameterClass.isAssignableFrom(byte.class))
            toSet = Byte.valueOf(value);
        else if (parameterClass.isAssignableFrom(Short.class) || parameterClass.isAssignableFrom(short.class))
            toSet = Short.valueOf(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(value);
        else if (parameterClass.isAssignableFrom(Boolean.class) || parameterClass.isAssignableFrom(boolean.class))
            toSet = Boolean.valueOf(value);
        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 10 with AcsJCDBFieldDoesNotExistEx

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

the class HibernateWDALImpl method set_DAO.

/* (non-Javadoc)
	 * @see com.cosylab.CDB.WDALOperations#set_DAO(java.lang.String, java.lang.String)
	 */
public void set_DAO(String curl, String xml) throws CDBFieldDoesNotExistEx, CDBRecordIsReadOnlyEx, CDBExceptionEx, CDBXMLErrorEx, CDBRecordDoesNotExistEx {
    checkAccess();
    m_logger.log(AcsLogLevel.INFO, "set_DAO: " + curl);
    // read given xml and iterate through its content and check if something was changed
    DAOImpl daoImp = null;
    XMLHandler daoXMLSolver = null;
    // get content of the given xml string using parser without any shemas and validation
    // since given xml string come from a client that have no shemas and it is full expanded version
    // of existing xml or it is small composed xml of few properties
    XMLHandler xmlSolver = new XMLHandler(false, m_logger);
    // TODO markArrays == 2 impl. is a mess... I think lot of code could be removed!
    //xmlSolver.setMarkArrays(2);
    parseXML(xml, xmlSolver);
    Object node = DOMJavaClassIntrospector.getNode(curl, rootNode);
    if (node == null) {
        AcsJCDBRecordDoesNotExistEx ex = new AcsJCDBRecordDoesNotExistEx();
        ex.setCurl(curl);
        throw ex.toCDBRecordDoesNotExistEx();
    }
    // node is saved as XML
    if (node instanceof XMLSaver) {
        XMLSaver saver = (XMLSaver) node;
        Transaction tr = null;
        try {
            tr = mainSession.beginTransaction();
            saver.save(xml);
            tr.commit();
        } catch (Throwable th) {
            if (tr != null)
                tr.rollback();
            m_logger.log(AcsLogLevel.NOTICE, "Failed to set DAO: " + curl, th);
            AcsJCDBExceptionEx cdbex = new AcsJCDBExceptionEx(th);
            throw cdbex.toCDBExceptionEx();
        }
        return;
    }
    // get original xml that we will use to compare
    xml = get_DAO(curl);
    daoXMLSolver = new XMLHandler(false, m_logger);
    parseXML(xml, daoXMLSolver);
    daoImp = new DAOImpl(curl, daoXMLSolver.m_rootNode, poa, m_logger);
    // iterater throuth given xml and put changed attributes in map
    LinkedHashMap map = new LinkedHashMap();
    try {
        checkforChanges("", xmlSolver.m_rootNode, map, daoImp);
        saveChanges(curl, map);
    } catch (AcsJCDBFieldDoesNotExistEx e) {
        throw e.toCDBFieldDoesNotExistEx();
    } catch (AcsJCDBXMLErrorEx e) {
        throw e.toCDBXMLErrorEx();
    }
}
Also used : AcsJCDBExceptionEx(alma.cdbErrType.wrappers.AcsJCDBExceptionEx) AcsJCDBFieldDoesNotExistEx(alma.cdbErrType.wrappers.AcsJCDBFieldDoesNotExistEx) Transaction(org.hibernate.Transaction) XMLSaver(com.cosylab.cdb.jdal.hibernate.DOMJavaClassIntrospector.XMLSaver) AcsJCDBRecordDoesNotExistEx(alma.cdbErrType.wrappers.AcsJCDBRecordDoesNotExistEx) AcsJCDBXMLErrorEx(alma.cdbErrType.wrappers.AcsJCDBXMLErrorEx) LinkedHashMap(java.util.LinkedHashMap)

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