Package org.apache.stanbol.entityhub.servicesapi.query

Examples of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery


        log.debug("  > field  : " + field);
        log.debug("  > lang  : " + language);
        log.debug("  > limit : " + limit);
        log.debug("  > offset: " + offset);
        log.debug("  > ldpath: " + ldpath);
        FieldQuery query;
        if(ldpath != null && !ldpath.isEmpty()){ //STANBOL-417
            query = new LDPathFieldQueryImpl();
            ((LDPathFieldQueryImpl)query).setLDPathSelect(ldpath);
        } else { //if no LDPath is parsed select the default field
            query = queryFactory.createFieldQuery();
            Collection<String> selectedFields = new ArrayList<String>();
            selectedFields.add(field); //select also the field used to find entities
            query.addSelectedFields(selectedFields);
        }
        if (language == null || language.trim().isEmpty()) {
            query.setConstraint(field, new TextConstraint(name, PatternType.wildcard, false));
        } else {
            query.setConstraint(field, new TextConstraint(name, PatternType.wildcard, false, language));
        }
        if (limit != null && limit > 0) {
            query.setLimit(limit);
        }
        if(offset != null && offset > 0) {
            query.setOffset(offset);
        }
        return query;
    }
View Full Code Here


    public static FieldQuery fromJSON(String jsonQueryString,MediaType acceptedMediaType) throws JSONException,WebApplicationException{
        if(jsonQueryString == null){
            throw new IllegalArgumentException("The parsed JSON object MUST NOT be NULL!");
        }
        JSONObject jQuery = new JSONObject(jsonQueryString);
        FieldQuery query;
        if(jQuery.has("ldpath")){ //STANBOL-417: support for using LDPath as select
            LDPathFieldQueryImpl ldPathQuery = new LDPathFieldQueryImpl();
            ldPathQuery.setLDPathSelect(jQuery.getString("ldpath"));
            query = ldPathQuery;
        } else {
            query = new FieldQueryImpl();
        }
        if(!jQuery.has("constraints")){
            StringBuilder message = new StringBuilder();
            message.append("The parsed Field Query MUST contain at least a single 'constraints'\n");
            message.append("Parsed Query:\n");
            message.append(jQuery.toString(4));
            log.warn(message.toString());
            throw new WebApplicationException(
                Response.status(Status.BAD_REQUEST).entity(
                    message.toString()).header(HttpHeaders.ACCEPT, acceptedMediaType.toString())
                    .build());
        }
        JSONArray constraints = jQuery.getJSONArray("constraints");
        //collect all parsing Errors to report a complete set of all errors
        boolean parsingError = false;
        StringBuilder parsingErrorMessages = new StringBuilder();
        parsingErrorMessages.append("Constraint parsing Errors:\n");
        for(int i=0;i<constraints.length();i++){
            JSONObject jConstraint = constraints.getJSONObject(i);
            if(jConstraint.has("field")){
                String field = jConstraint.getString("field");
                field = NamespaceEnum.getFullName(field);
                //check if there is already a constraint for that field
                if(field == null || field.isEmpty()){
//                    log.warn("The value of the key \"field\" MUST NOT be NULL nor emtpy!");
//                    log.warn("Constraint:\n {}",jConstraint.toString(4));
                    parsingErrorMessages.append('\n');
                    parsingErrorMessages.append(
                        "Each Field Query Constraint MUST define a value for 'field'\n");
                    parsingErrorMessages.append("Parsed Constraint:\n");
                    parsingErrorMessages.append(jConstraint.toString(4));
                    parsingErrorMessages.append('\n');
                    parsingError = true;
                    continue;
                } else if(query.isConstrained(field)){
//                   log.warn("Multiple constraints for field {} in parsed FieldQuery!",field);
                    parsingErrorMessages.append('\n');
                    parsingErrorMessages.append(
                        "The parsed Query defines multiple constraints fr the field '"
                        +field+"'!\n");
                    parsingErrorMessages.append("FieldQuery allows only a single Constraint for a field\n");
                    parsingErrorMessages.append("Parsed Constraints:\n");
                    parsingErrorMessages.append(constraints.toString(4));
                    parsingErrorMessages.append('\n');
                    parsingError = true;
                    continue;
                } else {
                    try {
                        query.setConstraint(field, parseConstraint(jConstraint));
                    } catch (IllegalArgumentException e) {
                        parsingErrorMessages.append('\n');
                        parsingErrorMessages.append(e.getMessage());
                        parsingErrorMessages.append('\n');
                        parsingError = true;
                        continue;
                    }
                }
            } else { //empty field
                parsingErrorMessages.append('\n');
                parsingErrorMessages.append("Constraints MUST define a value for 'field'\n");
                parsingErrorMessages.append("Parsed Constraint:\n");
                parsingErrorMessages.append(jConstraint.toString(4));
                parsingErrorMessages.append('\n');
                parsingError = true;
                continue;
            }
        }
        if(parsingError){
            String message = parsingErrorMessages.toString();
            log.warn(message);
            throw new WebApplicationException(
                Response.status(Status.BAD_REQUEST).entity(
                    message).header(HttpHeaders.ACCEPT, acceptedMediaType.toString())
                    .build());
        }
        //parse selected fields
        JSONArray selected = jQuery.optJSONArray("selected");
        if(selected != null){
            for(int i=0;i<selected.length();i++){
                String selectedField = selected.getString(i);
                selectedField = NamespaceEnum.getFullName(selectedField);
                if(selectedField != null && !selectedField.isEmpty()){
                    query.addSelectedField(selectedField);
                }
            }
        } //else no selected fields -> funny but maybe someone do need only the ids
        //parse limit and offset
        if(jQuery.has("limit") && !jQuery.isNull("limit")){
            try {
                query.setLimit(jQuery.getInt("limit"));
            } catch (JSONException e) {
                parsingErrorMessages.append('\n');
                parsingErrorMessages.append("Property \"limit\" MUST BE a valid integer number!\n");
                parsingErrorMessages.append("Parsed Value:");
                parsingErrorMessages.append(jQuery.get("init"));
                parsingErrorMessages.append('\n');
                parsingError = true;
            }
        }
        if(jQuery.has("offset") && !jQuery.isNull("offset")){
            try {
                query.setOffset(jQuery.getInt("offset"));
            } catch (JSONException e) {
                parsingErrorMessages.append('\n');
                parsingErrorMessages.append("Property \"offset\" MUST BE a valid integer number!\n");
                parsingErrorMessages.append("Parsed Value:");
                parsingErrorMessages.append(jQuery.get("init"));
View Full Code Here

        return results;
    }

    @Override
    public Collection<Object> listSubjects(Object property, Object object) {
        FieldQuery query = createQuery();
        if(this.isURI(object)){
            query.setConstraint(property.toString(), new ReferenceConstraint(object.toString()));
        } else if(object instanceof Text){
            Text text = (Text)object;
            TextConstraint constraint;
            if(text.getLanguage() == null){
                constraint = new TextConstraint(text.getText(), PatternType.none, true);
            } else {
                constraint = new TextConstraint(text.getText(), PatternType.none, true,text.getLanguage());
            }
            query.setConstraint(property.toString(), constraint);
        } else {
            Set<DataTypeEnum> dataTypes = DataTypeEnum.getPrimaryDataTypes(object.getClass());
            if(dataTypes == null || dataTypes.isEmpty()){
                query.setConstraint(property.toString(),
                    new ValueConstraint(object));
            } else {
                Collection<String> types = new ArrayList<String>(dataTypes.size());
                for(DataTypeEnum type : dataTypes){
                    types.add(type.getUri());
                }
                query.setConstraint(property.toString(),
                    new ValueConstraint(object,types));
            }
        }
        query.setLimit(Integer.valueOf(DEFAULT_MAX_SELECT));
        QueryResultList<String> results;
        try {
            results = query(query);
           
        } catch (EntityhubException  e) {
            throw new IllegalStateException("Unable to query for resources with value '"+
                object+"' on property '"+property+"'!",e);
        }
        Collection<Object> references;
        if(results.isEmpty()){
            references = Collections.emptySet();
        } else if(results.size() == 1){ //assuming that a single result is a likely case
            references = Collections.singleton(
                (Object)getValueFactory().createReference(results.iterator().next()));
        } else {
            int offset = 0;
            references = new HashSet<Object>(results.size());
            for(String result : results){
                references.add(getValueFactory().createReference(result));
            }
            while(results.size() >= DEFAULT_MAX_SELECT && references.size() <= DEFAULT_MAX_RESULTS-DEFAULT_MAX_SELECT){
                offset = offset + results.size();
                query.setOffset(offset);
                try {
                    results = query(query);
                } catch (EntityhubException e) {
                    throw new IllegalStateException("Unable to query for resources with value '"+
                        object+"' on property '"+property+"'!",e);
View Full Code Here

        }
        return container;
    }

    private JSONObject reconcile(ReconcileQuery rQuery) throws JSONException, EntityhubException {
        FieldQuery query = createFieldQuery();
        query.addSelectedFields(SELECTED_FIELDS);
        addNameConstraint(rQuery, query);
        addTypeConstraint(rQuery, query);
        addPropertyConstraints(rQuery, query);
        query.setLimit(query.getLimit());
        QueryResultList<Representation> results = performQuery(query);
        List<JSONObject> jResultList = new ArrayList<JSONObject>(results.size());
        //we need to know the highest score to normalise between [0..1]
        double maxQueryScore = -1;
        if(!results.isEmpty()){
View Full Code Here

            if (field.isEmpty()) {
                field = DEFAULT_FIND_FIELD;
            }
        }
       
        FieldQuery query = JerseyUtils.createFieldQueryForFindRequest(name, field, language,
            limit == null || limit < 1 ? DEFAULT_FIND_RESULT_LIMIT : limit, offset,ldpath);
        return executeQuery(ContextHelper.getServiceFromContext(
            ReferencedSiteManager.class, servletContext), query, acceptedMediaType, headers);
    }
View Full Code Here

        }
        if(search == null || search.isEmpty()){
            throw new IllegalArgumentException("The parsed list of search strings MUST NOT be NULL nor empty");
        }
        //build the query and than return the result
        FieldQuery query = factory.createFieldQuery();
        if(includeFields == null){
            query.addSelectedField(field);
        } else {
            if(!includeFields.contains(field)){
                query.addSelectedField(field);
            }
            for(String select : includeFields){
                query.addSelectedField(select);
            }
        }
        query.setLimit(20);//TODO make configurable
        query.setConstraint(field, new TextConstraint(search, languages));
        return query;
    }
View Full Code Here

                                           String... languages) throws IllegalStateException {
        Entityhub entityhub = getSearchService();
        if(entityhub == null){
            throw new IllegalStateException("The Entityhub is currently not active");
        }
        FieldQuery query = EntitySearcherUtils.createFieldQuery(entityhub.getQueryFactory(),
            field, includeFields, search, languages);
        QueryResultList<Representation> results;
        try {
            results = entityhub.find(query);
        } catch (EntityhubException e) {
View Full Code Here

        }
        if(search == null || search.isEmpty()){
            throw new IllegalArgumentException("The parsed list of search strings MUST NOT be NULL nor empty");
        }
        //build the query and than return the result
        FieldQuery query = factory.createFieldQuery();
        if(includeFields == null){
            query.addSelectedField(field.getUnicodeString());
        } else {
            if(!includeFields.contains(field.getUnicodeString())){
                query.addSelectedField(field.getUnicodeString());
            }
            for(UriRef select : includeFields){
                query.addSelectedField(select.getUnicodeString());
            }
        }
        query.setLimit(20);//TODO make configurable
        query.setConstraint(field.getUnicodeString(), new TextConstraint(search, languages));
        return query;
    }
View Full Code Here

                                           Integer limit) throws EntitySearcherException {
        Entityhub entityhub = getSearchService();
        if(entityhub == null){
            throw new EntitySearcherException("The Entityhub is currently not active");
        }
        FieldQuery query = EntitySearcherUtils.createFieldQuery(entityhub.getQueryFactory(),
            field, includeFields, search, languages);
        if(limit != null && limit > 0){
            query.setLimit(limit);
        } else if(this.limit != null){
            query.setLimit(this.limit);
        }
        QueryResultList<Representation> results;
        try {
            results = entityhub.find(query);
        } catch (EntityhubException e) {
View Full Code Here

        //build the query and than return the result
        Site site = getSearchService();
        if(site == null){
            throw new IllegalStateException("ReferencedSite "+siteId+" is currently not available");
        }
        FieldQuery query = EntitySearcherUtils.createFieldQuery(site.getQueryFactory(),
            field, includeFields, search, languages);
        if(limit != null && limit > 0){
            query.setLimit(limit);
        } else if(this.limit != null){
            query.setLimit(this.limit);
        }
        QueryResultList<Representation> results;
        try {
            results = site.find(query);
        } catch (SiteException e) {
View Full Code Here

TOP

Related Classes of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery

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.