Search in sources :

Example 1 with Channels

use of alma.TMCDB.maci.Channels in project ACS by ACS-Community.

the class HibernateWDALImpl method updateChannelsTableMap.

public void updateChannelsTableMap(String curl) {
    m_logger.info("clear_cache(curl): ChannelsTable1");
    final String keyField = "Name";
    final Class type = alma.TMCDB.maci.EventChannel.class;
    Map<String, Object> rootMap = (Map<String, Object>) rootNode;
    Channels channels = (Channels) ((Map<String, Object>) rootMap.get("MACI")).get("Channels");
    m_logger.info("clear_cache(curl): ChannelsTable2");
    try {
        Session session = hibernateUtil.getSession();
        Method accessor = DOMJavaClassIntrospector.getAccessorMethod(type, keyField);
        Field field = null;
        if (accessor == null) {
            try {
                field = type.getField(keyField);
            } catch (Throwable th) {
                throw new IllegalStateException("failed to obtain key ");
            }
        }
        if (channels == null)
            channels = new Channels();
        Map<String, EventChannelNode> map;
        map = channels._;
        m_logger.info("clear_cache(curl): ChannelsTable3");
        alma.TMCDB.maci.NotificationServiceMapping mapping = (alma.TMCDB.maci.NotificationServiceMapping) session.createCriteria(alma.TMCDB.maci.NotificationServiceMapping.class).add(Restrictions.eq("ConfigurationId", configId)).uniqueResult();
        m_logger.info("clear_cache(curl): ChannelsTable4");
        if (mapping != null) {
            channels.setNotificationServiceMapping(mapping);
        }
        m_logger.info("clear_cache(curl): ChannelsTable5");
        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): ChannelsTable6");
        //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)));
        List list = getListForConfiguration(session, type, cr);
        m_logger.info("clear_cache(curl): ChannelsTable7");
        System.out.println("\nFound the following Containers");
        for (Object data : list) {
            System.out.println(((alma.TMCDB.maci.Container) data).Path + "/" + ((alma.TMCDB.maci.Container) data).getName());
        }
        //Remove the entries from existing maps.
        System.out.println("\nChecking maps to remove");
        m_logger.info("clear_cache(curl): ChannelsTable8");
        Map<String, EventChannelNode> rParentMap = map;
        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 alma.TMCDB.maci.EventChannel) {
                    System.out.println("Instance of EventChannel (EventChannel!).");
                } else if (data instanceof alma.TMCDB.maci.EventChannelNode) {
                    System.out.println("Instance of ContainerNode (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 = ((alma.TMCDB.maci.EventChannelNode) 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): ChannelsTable9");
        for (Object data : list) {
            String baseKey;
            if (accessor != null)
                baseKey = accessor.invoke(data, (Object[]) null).toString();
            else
                //if (field != null)
                baseKey = field.get(data).toString();
            // baseKey should not be null
            Map parentMap = map;
            alma.TMCDB.maci.EventChannel channel = (alma.TMCDB.maci.EventChannel) data;
            // now find its map
            String path = getNormalizedPath(channel.Path);
            while (path != null && path.length() > 0) {
                // remove trailing slashes, to have unique curl (used for key)
                if (path.charAt(0) == '/') {
                    path = path.substring(1);
                    continue;
                }
                int pos = path.indexOf('/');
                String parentPath = (pos > 0) ? path.substring(0, pos) : path;
                String subpath = (pos > 0) ? path.substring(pos + 1, path.length()) : null;
                alma.TMCDB.maci.EventChannelNode parentChannel = (alma.TMCDB.maci.EventChannelNode) parentMap.get(parentPath);
                if (parentChannel == null) {
                    parentChannel = new alma.TMCDB.maci.EventChannelNode();
                    parentMap.put(parentPath, parentChannel);
                }
                parentMap = parentChannel._;
                path = subpath;
            }
            // unique key generation
            int count = 0;
            String key = baseKey;
            while (parentMap.containsKey(key)) key = baseKey + String.valueOf(++count);
            parentMap.put(key, data);
            m_logger.info("clear_cache(curl): ChannelsTable10");
        }
        m_logger.info("clear_cache(curl): ChannelsTable11");
    } catch (Throwable th) {
        th.printStackTrace();
    }
    m_logger.info("clear_cache(curl): ChannelsTable12");
}
Also used : Channels(alma.TMCDB.maci.Channels) EventChannel(alma.acs.tmcdb.EventChannel) Field(java.lang.reflect.Field) Container(alma.acs.tmcdb.Container) Criterion(org.hibernate.criterion.Criterion) EventChannelNode(alma.TMCDB.maci.EventChannelNode) NotificationServiceMapping(alma.acs.tmcdb.NotificationServiceMapping) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) EventChannelNode(alma.TMCDB.maci.EventChannelNode) Map(java.util.Map) RootMap(com.cosylab.cdb.jdal.hibernate.RootMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Session(org.hibernate.Session)

Example 2 with Channels

use of alma.TMCDB.maci.Channels in project ACS by ACS-Community.

the class HibernateWDALImpl method getChannelsTableMap.

public Object getChannelsTableMap() {
    final String keyField = "Name";
    final Class type = alma.TMCDB.maci.EventChannel.class;
    try {
        Session session = hibernateUtil.getSession();
        Method accessor = DOMJavaClassIntrospector.getAccessorMethod(type, keyField);
        Field field = null;
        if (accessor == null) {
            try {
                field = type.getField(keyField);
            } catch (Throwable th) {
                throw new IllegalStateException("failed to obtain key ");
            }
        }
        boolean channelsExist = false;
        Channels channels = new Channels();
        alma.TMCDB.maci.NotificationServiceMapping mapping = (alma.TMCDB.maci.NotificationServiceMapping) session.createCriteria(alma.TMCDB.maci.NotificationServiceMapping.class).add(Restrictions.eq("ConfigurationId", configId)).uniqueResult();
        if (mapping != null) {
            channels.setNotificationServiceMapping(mapping);
            channelsExist = true;
        }
        List list = getListForConfiguration(session, type);
        if (list.size() > 0) {
            channelsExist = true;
        }
        Map<String, EventChannelNode> map;
        if (channelsExist) {
            map = channels._;
        } else {
            //map = new RootMap<String, EventChannelNode>();
            return null;
        }
        for (Object data : list) {
            String baseKey;
            if (accessor != null)
                baseKey = accessor.invoke(data, (Object[]) null).toString();
            else
                //if (field != null)
                baseKey = field.get(data).toString();
            // baseKey should not be null
            Map parentMap = map;
            alma.TMCDB.maci.EventChannel channel = (alma.TMCDB.maci.EventChannel) data;
            // now find its map
            String path = getNormalizedPath(channel.Path);
            while (path != null && path.length() > 0) {
                // remove trailing slashes, to have unique curl (used for key)
                if (path.charAt(0) == '/') {
                    path = path.substring(1);
                    continue;
                }
                int pos = path.indexOf('/');
                String parentPath = (pos > 0) ? path.substring(0, pos) : path;
                String subpath = (pos > 0) ? path.substring(pos + 1, path.length()) : null;
                alma.TMCDB.maci.EventChannelNode parentChannel = (alma.TMCDB.maci.EventChannelNode) parentMap.get(parentPath);
                if (parentChannel == null) {
                    parentChannel = new alma.TMCDB.maci.EventChannelNode();
                    parentMap.put(parentPath, parentChannel);
                }
                parentMap = parentChannel._;
                path = subpath;
            }
            // unique key generation
            int count = 0;
            String key = baseKey;
            while (parentMap.containsKey(key)) key = baseKey + String.valueOf(++count);
            parentMap.put(key, data);
        }
        return channels;
    } catch (Throwable th) {
        th.printStackTrace();
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) Channels(alma.TMCDB.maci.Channels) EventChannel(alma.acs.tmcdb.EventChannel) Field(java.lang.reflect.Field) EventChannelNode(alma.TMCDB.maci.EventChannelNode) EventChannelNode(alma.TMCDB.maci.EventChannelNode) NotificationServiceMapping(alma.acs.tmcdb.NotificationServiceMapping) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) RootMap(com.cosylab.cdb.jdal.hibernate.RootMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Session(org.hibernate.Session)

Aggregations

Channels (alma.TMCDB.maci.Channels)2 EventChannelNode (alma.TMCDB.maci.EventChannelNode)2 EventChannel (alma.acs.tmcdb.EventChannel)2 NotificationServiceMapping (alma.acs.tmcdb.NotificationServiceMapping)2 RootMap (com.cosylab.cdb.jdal.hibernate.RootMap)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 Session (org.hibernate.Session)2 Container (alma.acs.tmcdb.Container)1 Criterion (org.hibernate.criterion.Criterion)1