001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
018 */
019package org.nuxeo.ecm.platform.sessioninspector.jsf;
020
021import java.lang.reflect.Field;
022import java.util.Arrays;
023import java.util.List;
024
025/**
026 * @since 5.9.2
027 */
028public class StateReferenceHelper {
029
030    public static String getIdForNode(Object node) throws NoSuchFieldException, SecurityException,
031            IllegalArgumentException, IllegalAccessException {
032        Field idField = node.getClass().getDeclaredField("id");
033        idField.setAccessible(true);
034        String result = (String) idField.get(node);
035        return result;
036    }
037
038    public static Object getStateForPath(List<?> nodes, String[] path, Object[] states) throws NoSuchFieldException,
039            SecurityException, IllegalArgumentException, IllegalAccessException {
040        for (int i = 0; i < nodes.size(); i++) {
041            if (getIdForNode(nodes.get(i)).equals(path[0])) {
042                if (path.length == 1) {
043                    return states[i];
044                } else {
045                    List<?> children = getChildrenForNode(nodes.get(i));
046                    if (children != null) {
047                        return getStateForPath(children, Arrays.copyOfRange(path, 1, path.length), (Object[]) states[i]);
048                    } else {
049                        return null;
050                    }
051                }
052            }
053        }
054        return null;
055    }
056
057    public static List<?> getChildrenForNode(Object node) throws NoSuchFieldException, SecurityException,
058            IllegalArgumentException, IllegalAccessException {
059        Field idField = node.getClass().getDeclaredField("children");
060        idField.setAccessible(true);
061        List<?> result = (List<?>) idField.get(node);
062        return result;
063    }
064
065    public static Object getChildrenState(Object state) {
066        Object[] temp = (Object[]) ((Object[]) state)[1];
067        return ((Object[]) temp[0])[1];
068    }
069
070    public static String getTypeForNode(Object node) throws NoSuchFieldException, SecurityException,
071            IllegalArgumentException, IllegalAccessException {
072        Field idField = node.getClass().getDeclaredField("type");
073        idField.setAccessible(true);
074        String result = (String) idField.get(node);
075        return result;
076    }
077
078}