All Downloads are FREE. Search and download functionalities are using the official Maven repository.

StorageHelper.RetrieveByPath Maven / Gradle / Ivy

There is a newer version: 1.14
Show newest version
package StorageHelper;

import Data.Storage;
import DataTools.Utils;
import org.junit.Assert;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 Copyright 2016 Alianza Inc.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */

public class RetrieveByPath {
    HashMap storage;
    Storage AllData;

    public RetrieveByPath(Storage data) {
        storage = data.toMap();
        AllData = data;
    }

    /**
     * get a saved object by the path given
     * @param path String[] of the path
     * @return Object that is found from the given path
     */
    public Object findElementByPath(String[] path) {
        Object current = storage;

        for (int j = 0; j < path.length; ++j) {

            if (!hasGet(current)) {
                current = CreatorHelper.createDataStorage(current);
                AllData.put(Arrays.copyOfRange(path, 0, j), current);
            }

            try {
                current = findElement(current, path[j]);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                Utils.debug("PATH INVALID:\nPATH: " + getItemsFromArray(path) + "\nDATA: " + AllData.toJson(), "error");
                e.printStackTrace();
                Assert.fail();
            }

        }
        return current;
    }

    public void assurePathExists(String[] path) {
        Object current;
        String nextPath[] = new String[0];
        ArrayList currentPath = new ArrayList<>();
//        Object previous = AllData;

        for (int i = 0; i < path.length; ++i) {
            String key = path[i];
            try {
                current = findElement(AllData.get(currentPath.toArray(nextPath)), key);
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                current = null;
            }

            //is this an array or not
            if ((i < path.length - 1) && isArray(path[i + 1])) {
                if (current == null || current.equals(null)) {
                    current = new ArrayList<>();
                }
                //populate the array to make sure to get to the number needed
                int index = Integer.parseInt(path[i+1]);
                //use reflection to add things to the array, different types of arrays
                Class methods = current.getClass();
                try {
                    //find if it is size or length
                    String sizeName = "size";
                    String addName = "put";
                    for (Method meth : methods.getDeclaredMethods()) {
                        if (meth.getName().equals("length")) {
                            sizeName = "length";
                        } else if (meth.getName().equals("add")) {
                            addName = "add";
                        }
                    }
                    Method size = methods.getMethod(sizeName);
                    Method add = methods.getMethod(addName, Object.class);
                    while (index >= (Integer) size.invoke(current)) {
                        add.invoke(current, new String[]{null});
                    }
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    e.printStackTrace();
                }

            } else if (current == null) {
                current = new HashMap<>();
            }
            try {
                Update update = new Update(AllData.get(currentPath.toArray(nextPath)));
                update.performUpdate(key, current);
            } catch (IllegalAccessException | InvocationTargetException e1) {
                e1.printStackTrace();
            }
            currentPath.add(key);
            nextPath = new String[currentPath.size()];
//            previous = AllData.get(currentPath.toArray(nextPath));
        }

        //update class properties
        storage = AllData.toMap();
    }

    private boolean hasGet(Object obj) {
        boolean found = false;
        try {
            for (Method method : obj.getClass().getMethods()) {
                String methName = method.getName();
                if (methName.equals("get"))
                    found = true;
            }
        } catch (NullPointerException e) {
            Utils.debug("object not found", "error");
        }
        return found;
    }

    private String getItemsFromArray (String[] list) {
        String items = "";

        for (int i = 0; i < list.length; i++) {
            items += i + ":" + list[i] + " ";
        }

        return items;
    }

    private Object findElement(Object currentItem, String key) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Object nextItem;
        boolean array = isArray(key);

            //get next object in path, if it is an array need to give an int to find it
        if (array && currentItem.getClass().getTypeName().contains("Array"))
            nextItem = currentItem.getClass().getMethod("get", new Class[]{int.class}).invoke(currentItem, Integer.parseInt(key));
        else
            try {
                nextItem = currentItem.getClass().getMethod("get", new Class[]{Object.class}).invoke(currentItem, key);
                //sometimes it complains because param Object class and String class are different
            } catch (NoSuchMethodException e) {
                nextItem = currentItem.getClass().getMethod("get", new Class[]{String.class}).invoke(currentItem, key);
            }

        return nextItem;
    }

    private boolean isArray(String key) {
        boolean array = true;
        //will know if it is an array because it will be a number to find the item
        for (int i = 0; i < key.length() && array; ++i) {
            if (!Character.isDigit(key.charAt(i)))
                array = false;
        }

        return array;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy