Package org.sakaiproject.entitybus.entityprovider.search

Examples of org.sakaiproject.entitybus.entityprovider.search.Search


     *
     * @param params the request params from a request (do not include headers)
     * @return a search filter object
     */
    public static Search makeSearchFromRequestParams(Map<String, Object> params) {
        Search search = new Search();
        int page = -1;
        int limit = -1;
        try {
            if (params != null) {
                for (Entry<String, Object> entry : params.entrySet()) {
                    String key = entry.getKey();
                    // filter out certain keys
                    if (getIgnoreSet().contains(key)) {
                        continue; // skip this key
                    }
                    Object value = entry.getValue();
                    if (value == null) {
                        // in theory this should not happen
                        continue;
                    } else if (value.getClass().isArray()) {
                        // use the value as is
                    } else {
                        // get paging values out if possible
                        if ("_limit".equals(key)
                                || "_perpage".equals(key)
                                || "perpage".equals(key)
                                || "count".equals(key)
                                || "itemsPerPage".equals(key)) {
                            try {
                                limit = Integer.valueOf(value.toString()).intValue();
                                search.setLimit(limit);
                            } catch (NumberFormatException e) {
                                System.out.println("WARN Invalid non-number passed in for _limit/_perpage param: " + value + ":" + e);
                            }
                            continue;
                        } else if ("_start".equals(key)
                                || "startIndex".equals(key)) {
                            try {
                                int start = Integer.valueOf(value.toString()).intValue();
                                search.setStart(start);
                            } catch (NumberFormatException e) {
                                System.out.println("WARN Invalid non-number passed in for '_start' param: " + value + ":" + e);
                            }
                            continue;
                        } else if ("_page".equals(key)
                                || "page".equals(key)
                                || "startPage".equals(key)) {
                            try {
                                page = Integer.valueOf(value.toString()).intValue();
                            } catch (NumberFormatException e) {
                                System.out.println("WARN Invalid non-number passed in for '_page' param: " + value + ":" + e);
                            }
                            continue;
                        } else if ("_order".equals(key)
                                || "_sort".equals(key)
                                || "sort".equals(key)) {
                            if (value != null) {
                                String val = value.toString();
                                String[] sortBy = new String[] {val};
                                if (val.indexOf(',') > 0) {
                                    // multiple sort params
                                    sortBy = val.split(",");
                                }
                                try {
                                    for (int i = 0; i < sortBy.length; i++) {
                                        String sortItem = sortBy[i].trim();
                                        if (sortItem.endsWith("_reverse")) {
                                            search.addOrder( new Order(sortItem.substring(0, sortItem.length()-8), false) );
                                        } else if (sortItem.endsWith("_desc")) {
                                            search.addOrder( new Order(sortItem.substring(0, sortItem.length()-5), false) );
                                        } else if (sortItem.endsWith("_asc")) {
                                            search.addOrder( new Order(sortItem.substring(0, sortItem.length()-4)) );
                                        } else {
                                            search.addOrder( new Order(sortItem) );
                                        }
                                    }
                                } catch (RuntimeException e) {
                                    System.out.println("WARN Failed while getting the sort/order param: " + val + ":" + e);
                                }
                            }
                            continue;
                        } else if ("_searchTerms".equals(key)
                                || "searchTerms".equals(key)) {
                            // indicates a space delimited list of search terms
                            if (value != null) {
                                String val = value.toString();
                                String[] terms = val.split(" ");
                                search.addRestriction( new Restriction("searchTerms", terms) );
                            }
                            continue;
                        }
                    }
                    search.addRestriction( new Restriction(key, value) );
                }
            }
        } catch (Exception e) {
            // failed to translate the request to a search, not really much to do here
            System.out.println("WARN Could not translate entity request into search params: " + e.getMessage() + ":" + e);
        }
        // translate page into start/limit
        if (page > 0) {
            if (limit <= -1) {
                limit = 10; // set to a default value
                search.setLimit(limit);
                System.out.println("WARN page is set without a limit per page, setting per page limit to default value of 10");
            }
            search.setStart( (page-1) * limit );
        }
        return search;
    }
View Full Code Here


     * Translate the search into one using the standard search params
     * @param search
     * @return the translated search
     */
    public static Search translateStandardSearch(Search search) {
        Search togo = search;
        if (search == null) {
            togo = new Search();
        } else {
            EntityDataUtils.translateSearchReference(search, CollectionResolvable.SEARCH_USER_REFERENCE,
                    new String[] {"userReference","userId","userEid","user"}, "/user/");
            EntityDataUtils.translateSearchReference(search, CollectionResolvable.SEARCH_LOCATION_REFERENCE,
                    new String[] {"locationReference","locationId","location","siteReference","siteId","site","groupReference","groupId","group"}, "/site/");
View Full Code Here

                // if the user wants to serialize their objects specially then allow them to translate them
                OutputSerializable serializable = entityProviderManager.getProviderByPrefixAndCapability(prefix, OutputSerializable.class);
                if (serializable != null) {
                    if (entities == null) {
                        // these will be EntityData
                        entities = entityBrokerManager.getEntitiesData(ref, new Search(), params);
                    }
                    if (! entities.isEmpty()) {
                        // find the type of the objects this providers deals in
                        Object sample = entityBrokerManager.getSampleEntityObject(prefix, null);
                        Class<?> entityType = Object.class;
View Full Code Here

        }

        // get the entities if not supplied
        if (entities == null) {
            // these will be EntityData
            entities = entityBrokerManager.getEntitiesData(ref, new Search(), params);
        }
        if (entities.isEmpty()) {
            // just log this for now
            System.out.println("INFO: EntityEncodingManager: No entities to format ("+format+") and output for ref (" + ref + ")");
        }
View Full Code Here

TOP

Related Classes of org.sakaiproject.entitybus.entityprovider.search.Search

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.