Search in sources :

Example 1 with EmptyStringHandlerBACIPropertyType

use of alma.TMCDB.baci.EmptyStringHandlerBACIPropertyType in project ACS by ACS-Community.

the class HibernateWDALImpl method getAlmaBranch.

protected Map<String, Object> getAlmaBranch() {
    final Map<String, Object> almaRoot = new RootMap<String, Object>();
    try {
        Session session = hibernateUtil.getSession();
        schemaResourceResolverLoader.setSession(session);
        final Set<String> loadedComponents = new HashSet<String>();
        //
        if (plugin != null) {
            final HibernateWDALPlugin.ControlDeviceBindCallback bindCallback = new HibernateWDALPlugin.ControlDeviceBindCallback() {

                public void bindToComponentBranch(String name, String path, Object objectToBind) {
                    bindToAlmaBranch(almaRoot, name, path, objectToBind);
                    if (!loadedComponents.contains(path + "/" + name))
                        loadedComponents.add(path + "/" + name);
                }

                public void bindNonExpandedXMLToComponentBranch(Session session, Component component) {
                    alma.TMCDB.maci.Component comp = (alma.TMCDB.maci.Component) session.createCriteria(alma.TMCDB.maci.Component.class).add(Restrictions.eq("ComponentId", component.getComponentId())).uniqueResult();
                    if (comp == null)
                        throw new RuntimeException("Component with ID " + component.getComponentId() + " does not exist.");
                    bindNonExpandedComponentXMLToAlmaBranch(session, almaRoot, comp);
                // TODO why do not add to already loaded components list??!!!
                }
            };
            plugin.loadControlDevices(session, config, bindCallback);
        }
        //
        // add devices
        //
        Iterator componentList = session.createCriteria(alma.TMCDB.maci.Component.class).add(Restrictions.eq("Control", false)).add(Restrictions.eq("ConfigurationId", configId)).list().iterator();
        while (componentList.hasNext()) {
            alma.TMCDB.maci.Component component = (alma.TMCDB.maci.Component) componentList.next();
            // already loaded by plugins, skip
            if (loadedComponents.contains(component.Path + "/" + component.getName()))
                continue;
            String query = "FROM alma.TMCDB.baci.BACIPropertyType WHERE ComponentId = " + component.ComponentId;
            List propertyList = session.createQuery(query).list();
            if (propertyList.size() > 0) {
                ComponentData componentData = new ComponentData();
                try {
                    componentData.setData(component.XMLDoc);
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // add properties
                for (Iterator iter = propertyList.iterator(); iter.hasNext(); ) {
                    BACIPropertyType baciProperty = (BACIPropertyType) iter.next();
                    //componentData._.put(baciProperty.PropertyName, baciProperty);
                    componentData._.put(baciProperty.PropertyName, new EmptyStringHandlerBACIPropertyType(baciProperty));
                }
                // bind object to map tree
                bindToAlmaBranch(almaRoot, component.getName(), component.Path, componentData);
            } else if (component.XMLDoc != null) {
                bindNonExpandedComponentXMLToAlmaBranch(session, almaRoot, component);
            }
        }
    } catch (Throwable th) {
        th.printStackTrace();
    }
    return almaRoot;
}
Also used : RootMap(com.cosylab.cdb.jdal.hibernate.RootMap) ComponentData(alma.TMCDB.baci.ComponentData) EmptyStringHandlerBACIPropertyType(alma.TMCDB.baci.EmptyStringHandlerBACIPropertyType) BACIPropertyType(alma.TMCDB.baci.BACIPropertyType) HibernateWDALPlugin(com.cosylab.cdb.jdal.hibernate.plugin.HibernateWDALPlugin) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Component(alma.acs.tmcdb.Component) EmptyStringHandlerBACIPropertyType(alma.TMCDB.baci.EmptyStringHandlerBACIPropertyType) Session(org.hibernate.Session) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 2 with EmptyStringHandlerBACIPropertyType

use of alma.TMCDB.baci.EmptyStringHandlerBACIPropertyType in project ACS by ACS-Community.

the class HibernateWDALImpl method updateAlmaBranch.

protected void updateAlmaBranch(String curl) {
    m_logger.info("clear_cache(curl): AlmaBranch1");
    Map<String, Object> rootMap = (Map<String, Object>) rootNode;
    final Map<String, Object> almaRoot = (Map<String, Object>) rootMap.get(COMPONENT_TREE_NAME);
    m_logger.info("clear_cache(curl): AlmaBranch2");
    try {
        Session session = hibernateUtil.getSession();
        schemaResourceResolverLoader.setSession(session);
        String[] els = curl.split("/");
        String rpath = "^/*";
        String rsubpath = "^/*";
        String rcpath = "^/*";
        String rcname = els[els.length - 1];
        for (int i = 0; i < els.length; i++) {
            rpath += els[i];
            rsubpath += els[i];
            if (i < els.length - 1) {
                rpath += "/+";
                rsubpath += "/+";
                rcpath += els[i];
                if (i < els.length - 2)
                    rcpath += "/+";
            }
        }
        rpath += "/*$";
        rsubpath += "/+.*";
        rcpath += "/*$";
        System.out.println(rpath);
        System.out.println(rsubpath);
        System.out.println(rcpath + "|" + rcname);
        m_logger.info("clear_cache(curl): AlmaBranch3");
        //Consider the cases where the curl matches exactly the Path, where
        //it is part of the path and when it matches exactly the path and
        //the component name.
        Criterion cr = Restrictions.disjunction().add(getRegularExpressionRestriction("Path", rpath)).add(getRegularExpressionRestriction("Path", rsubpath)).add(Restrictions.and(getRegularExpressionRestriction("Path", rcpath), Restrictions.eq("Name", rcname)));
        //Remove the entries from existing maps.
        System.out.println("\nChecking maps to remove");
        m_logger.info("clear_cache(curl): AlmaBranch4");
        Map rParentMap = almaRoot;
        for (int i = 0; i < els.length; i++) {
            System.out.println("Checking path " + els[i] + ".");
            System.out.println("Parent keys: " + rParentMap.keySet().toString());
            Object data = rParentMap.get(els[i]);
            if (data == null) {
                System.out.println("No element found with the given curl");
                break;
            } else {
                if (data instanceof ComponentDAOImplSaver) {
                    System.out.println("Instance of ComponentDAOImplSaver (Component!).");
                } else if (data instanceof ComponentData) {
                    System.out.println("Instance of ComponentData (Path!).");
                } else {
                    System.out.println("Unknown type! Details: " + data.toString());
                }
                if (i < els.length - 1) {
                    System.out.println("There are elements remaining, so we proceed to next element in path.");
                    rParentMap = ((ComponentData) data)._;
                } else {
                    System.out.println("There are no elements remaining, we remove all entries from this element in path and on.");
                    rParentMap.remove(els[i]);
                }
            }
        }
        m_logger.info("clear_cache(curl): AlmaBranch5");
        final Set<String> loadedComponents = new HashSet<String>();
        //
        // add control devices
        //
        m_logger.info("clear_cache(curl): AlmaBranch6");
        if (plugin != null) {
            final HibernateWDALPlugin.ControlDeviceBindCallback bindCallback = new HibernateWDALPlugin.ControlDeviceBindCallback() {

                public void bindToComponentBranch(String name, String path, Object objectToBind) {
                    bindToAlmaBranch(almaRoot, name, path, objectToBind);
                    if (!loadedComponents.contains(path + "/" + name))
                        loadedComponents.add(path + "/" + name);
                }

                public void bindNonExpandedXMLToComponentBranch(Session session, Component component) {
                    alma.TMCDB.maci.Component comp = (alma.TMCDB.maci.Component) session.createCriteria(alma.TMCDB.maci.Component.class).add(Restrictions.eq("ComponentId", component.getComponentId())).uniqueResult();
                    if (comp == null)
                        throw new RuntimeException("Component with ID " + component.getComponentId() + " does not exist.");
                    bindNonExpandedComponentXMLToAlmaBranch(session, almaRoot, comp);
                }
            };
            plugin.updateControlDevices(session, config, bindCallback, curl);
        }
        m_logger.info("clear_cache(curl): AlmaBranch7");
        //
        // add devices
        //
        Iterator componentList = session.createCriteria(alma.TMCDB.maci.Component.class).add(Restrictions.eq("Control", false)).add(Restrictions.eq("ConfigurationId", configId)).add(cr).list().iterator();
        System.out.println("\nFound the following Components");
        m_logger.info("clear_cache(curl): AlmaBranch8");
        while (componentList.hasNext()) {
            alma.TMCDB.maci.Component component = (alma.TMCDB.maci.Component) componentList.next();
            System.out.println(component.Path + "/" + component.getName());
            // already loaded by plugins, skip
            if (loadedComponents.contains(component.Path + "/" + component.getName()))
                continue;
            String query = "FROM alma.TMCDB.baci.BACIPropertyType WHERE ComponentId = " + component.ComponentId;
            List propertyList = session.createQuery(query).list();
            System.out.println("Size: " + propertyList.size());
            if (propertyList.size() > 0) {
                ComponentData componentData = new ComponentData();
                try {
                    componentData.setData(component.XMLDoc);
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // add properties
                for (Iterator iter = propertyList.iterator(); iter.hasNext(); ) {
                    BACIPropertyType baciProperty = (BACIPropertyType) iter.next();
                    //componentData._.put(baciProperty.PropertyName, baciProperty);
                    componentData._.put(baciProperty.PropertyName, new EmptyStringHandlerBACIPropertyType(baciProperty));
                }
                // bind object to map tree
                bindToAlmaBranch(almaRoot, component.getName(), component.Path, componentData);
            } else if (component.XMLDoc != null) {
                bindNonExpandedComponentXMLToAlmaBranch(session, almaRoot, component);
            }
            m_logger.info("clear_cache(curl): AlmaBranch9");
        }
        m_logger.info("clear_cache(curl): AlmaBranch10");
    } catch (Throwable th) {
        th.printStackTrace();
    }
    m_logger.info("clear_cache(curl): AlmaBranch11");
}
Also used : EmptyStringHandlerBACIPropertyType(alma.TMCDB.baci.EmptyStringHandlerBACIPropertyType) BACIPropertyType(alma.TMCDB.baci.BACIPropertyType) Criterion(org.hibernate.criterion.Criterion) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Component(alma.acs.tmcdb.Component) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) ComponentData(alma.TMCDB.baci.ComponentData) HibernateWDALPlugin(com.cosylab.cdb.jdal.hibernate.plugin.HibernateWDALPlugin) Map(java.util.Map) RootMap(com.cosylab.cdb.jdal.hibernate.RootMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) EmptyStringHandlerBACIPropertyType(alma.TMCDB.baci.EmptyStringHandlerBACIPropertyType) Session(org.hibernate.Session)

Aggregations

BACIPropertyType (alma.TMCDB.baci.BACIPropertyType)2 ComponentData (alma.TMCDB.baci.ComponentData)2 EmptyStringHandlerBACIPropertyType (alma.TMCDB.baci.EmptyStringHandlerBACIPropertyType)2 Component (alma.acs.tmcdb.Component)2 RootMap (com.cosylab.cdb.jdal.hibernate.RootMap)2 HibernateWDALPlugin (com.cosylab.cdb.jdal.hibernate.plugin.HibernateWDALPlugin)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedHashSet (java.util.LinkedHashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Session (org.hibernate.Session)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Criterion (org.hibernate.criterion.Criterion)1