001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
016 */
017package org.nuxeo.ecm.platform.sessioninspector.jsf;
018
019import java.lang.reflect.Field;
020import java.util.Arrays;
021import java.util.List;
022
023/**
024 * @since 5.9.2
025 */
026public class StateReferenceHelper {
027
028    public static String getIdForNode(Object node) throws NoSuchFieldException, SecurityException,
029            IllegalArgumentException, IllegalAccessException {
030        Field idField = node.getClass().getDeclaredField("id");
031        idField.setAccessible(true);
032        String result = (String) idField.get(node);
033        return result;
034    }
035
036    public static Object getStateForPath(List<?> nodes, String[] path, Object[] states) throws NoSuchFieldException,
037            SecurityException, IllegalArgumentException, IllegalAccessException {
038        for (int i = 0; i < nodes.size(); i++) {
039            if (getIdForNode(nodes.get(i)).equals(path[0])) {
040                if (path.length == 1) {
041                    return states[i];
042                } else {
043                    List<?> children = getChildrenForNode(nodes.get(i));
044                    if (children != null) {
045                        return getStateForPath(children, Arrays.copyOfRange(path, 1, path.length), (Object[]) states[i]);
046                    } else {
047                        return null;
048                    }
049                }
050            }
051        }
052        return null;
053    }
054
055    public static List<?> getChildrenForNode(Object node) throws NoSuchFieldException, SecurityException,
056            IllegalArgumentException, IllegalAccessException {
057        Field idField = node.getClass().getDeclaredField("children");
058        idField.setAccessible(true);
059        List<?> result = (List<?>) idField.get(node);
060        return result;
061    }
062
063    public static Object getChildrenState(Object state) {
064        Object[] temp = (Object[]) ((Object[]) state)[1];
065        return ((Object[]) temp[0])[1];
066    }
067
068    public static String getTypeForNode(Object node) throws NoSuchFieldException, SecurityException,
069            IllegalArgumentException, IllegalAccessException {
070        Field idField = node.getClass().getDeclaredField("type");
071        idField.setAccessible(true);
072        String result = (String) idField.get(node);
073        return result;
074    }
075
076}