Search in sources :

Example 6 with VUserException

use of io.vertigo.lang.VUserException in project vertigo by KleeGroup.

the class ESStatement method remove.

/**
 * Supprime des documents.
 * @param query Requete de filtrage des documents à supprimer
 */
void remove(final ListFilter query) {
    Assertion.checkNotNull(query);
    // -----
    final QueryBuilder queryBuilder = ESSearchRequestBuilder.translateToQueryBuilder(query);
    final SearchRequestBuilder searchRequestBuilder = esClient.prepareSearch().setIndices(indexName).setTypes(typeName).setSearchType(SearchType.QUERY_THEN_FETCH).setNoFields().setSize(// 
    DEFAULT_SCROLL_SIZE).setScroll(// 
    DEFAULT_SCROLL_KEEP_ALIVE).addSort("_id", SortOrder.ASC).setQuery(queryBuilder);
    try {
        // get first scroll doc_id
        SearchResponse queryResponse = searchRequestBuilder.execute().actionGet();
        // the scrolling id
        final String scrollid = queryResponse.getScrollId();
        SearchHits searchHits = queryResponse.getHits();
        while (searchHits.getHits().length > 0) {
            // collect the id for this scroll
            final BulkRequestBuilder bulkRequest = esClient.prepareBulk().setRefresh(BULK_REFRESH);
            for (final SearchHit searchHit : searchHits) {
                bulkRequest.add(esClient.prepareDelete(indexName, typeName, searchHit.getId()));
            }
            // bulk delete all ids
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
            if (bulkResponse.hasFailures()) {
                throw new VSystemException("Can't removeBQuery {0} into {1} index.\nCause by {3}", typeName, indexName, bulkResponse.buildFailureMessage());
            }
            LOGGER.info("Deleted " + searchHits.getHits().length + " elements from index " + indexName);
            // new scrolling
            queryResponse = // 
            esClient.prepareSearchScroll(scrollid).setScroll(// 
            DEFAULT_SCROLL_KEEP_ALIVE).execute().actionGet();
            searchHits = queryResponse.getHits();
        }
    } catch (final SearchPhaseExecutionException e) {
        final VUserException vue = new VUserException(SearchResource.DYNAMO_SEARCH_QUERY_SYNTAX_ERROR);
        vue.initCause(e);
        throw vue;
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) VSystemException(io.vertigo.lang.VSystemException) SearchResponse(org.elasticsearch.action.search.SearchResponse) VUserException(io.vertigo.lang.VUserException)

Example 7 with VUserException

use of io.vertigo.lang.VUserException in project vertigo by KleeGroup.

the class ESStatement method loadList.

/**
 * @param indexDefinition Index de recherche
 * @param searchQuery Mots clés de recherche
 * @param listState Etat de la liste (tri et pagination)
 * @param defaultMaxRows Nombre de ligne max par defaut
 * @return Résultat de la recherche
 */
FacetedQueryResult<I, SearchQuery> loadList(final SearchIndexDefinition indexDefinition, final SearchQuery searchQuery, final DtListState listState, final int defaultMaxRows) {
    Assertion.checkNotNull(searchQuery);
    // -----
    final SearchRequestBuilder searchRequestBuilder = new ESSearchRequestBuilder(indexName, typeName, esClient).withSearchIndexDefinition(indexDefinition).withSearchQuery(searchQuery).withListState(listState, defaultMaxRows).build();
    LOGGER.info("loadList " + searchRequestBuilder);
    try {
        final SearchResponse queryResponse = searchRequestBuilder.execute().actionGet();
        return new ESFacetedQueryResultBuilder(esDocumentCodec, indexDefinition, queryResponse, searchQuery).build();
    } catch (final SearchPhaseExecutionException e) {
        final VUserException vue = new VUserException(SearchResource.DYNAMO_SEARCH_QUERY_SYNTAX_ERROR);
        vue.initCause(e);
        throw vue;
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) SearchResponse(org.elasticsearch.action.search.SearchResponse) VUserException(io.vertigo.lang.VUserException)

Example 8 with VUserException

use of io.vertigo.lang.VUserException in project vertigo by KleeGroup.

the class AbstractSqlExceptionHandler method handleConstraintSQLException.

/**
 * Traite l'exception lié à la contrainte d'intégrité.
 * Et lance une KUserException avec le message par défaut passé en paramètre et une MessageKey basé sur le nom de la contrainte.
 * @param sqle Exception SQL
 * @param defaultMsg Message par defaut
 */
protected final VUserException handleConstraintSQLException(final SQLException sqle, final MessageKey defaultMsg) {
    final String msg = sqle.getMessage();
    // recherche le nom de la contrainte d'intégrité
    final String constraintName = extractConstraintName(msg);
    Assertion.checkNotNull(constraintName, "Impossible d''extraire le nom de la contrainte : {0}", msg);
    // recherche le message pour l'utilisateur
    // On crée une clé de MessageText dynamiquement sur le nom de la contrainte d'intégrité
    // Ex: CK_PERSON_FULL_NAME_UNIQUE
    final MessageKey constraintKey = new SQLConstraintMessageKey(constraintName);
    // On récupère ici le message externalisé par défaut : Resources.DYNAMO_SQL_CONSTRAINT_IMPOSSIBLE_TO_DELETE ou Resources.DYNAMO_SQL_CONSTRAINT_ALREADY_REGISTRED)
    final String defaultConstraintMsg = MessageText.of(defaultMsg).getDisplay();
    final MessageText userContraintMessageText = MessageText.builder().withKey(constraintKey).withDefaultMsg(defaultConstraintMsg).build();
    final VUserException constraintException = new VUserException(userContraintMessageText);
    constraintException.initCause(sqle);
    return constraintException;
}
Also used : MessageKey(io.vertigo.core.locale.MessageKey) MessageText(io.vertigo.core.locale.MessageText) VUserException(io.vertigo.lang.VUserException)

Example 9 with VUserException

use of io.vertigo.lang.VUserException in project vertigo by KleeGroup.

the class ContactsWebServices method readContactView.

@GET("/contactView/{conId}")
public ContactView readContactView(@PathParam("conId") final long conId) {
    final Contact contact = contactDao.get(conId);
    if (contact == null) {
        // 404 ?
        throw new VUserException("Contact #" + conId + " unknown");
    }
    // we sheet and use 3 times the same address.
    final DtList<Address> addresses = DtList.of(contact.getAddressAccessor().get(), contact.getAddressAccessor().get(), contact.getAddressAccessor().get());
    final ContactView contactView = new ContactView();
    contactView.setName(contact.getName());
    contactView.setFirstName(contact.getFirstName());
    contactView.setHonorificCode(contact.getHonorificCode());
    contactView.setEmail(contact.getEmail());
    contactView.setBirthday(contact.getBirthday());
    contactView.setAddresses(addresses);
    // 200
    return contactView;
}
Also used : Address(io.vertigo.vega.webservice.data.domain.Address) Contact(io.vertigo.vega.webservice.data.domain.Contact) VUserException(io.vertigo.lang.VUserException) ContactView(io.vertigo.vega.webservice.data.domain.ContactView) GET(io.vertigo.vega.webservice.stereotype.GET)

Example 10 with VUserException

use of io.vertigo.lang.VUserException in project vertigo by KleeGroup.

the class RestfulServiceWebServiceHandlerPlugin method handle.

/**
 * {@inheritDoc}
 */
@Override
public Object handle(final Request request, final Response response, final WebServiceCallContext routeContext, final HandlerChain chain) throws SessionException {
    final WebServiceDefinition webServiceDefinition = routeContext.getWebServiceDefinition();
    final Object[] serviceArgs = makeArgs(routeContext);
    final Method method = webServiceDefinition.getMethod();
    final WebServices webServices = (WebServices) Home.getApp().getComponentSpace().resolve(method.getDeclaringClass());
    if (method.getName().startsWith("create")) {
        // by convention, if method starts with 'create', an http 201 status code is returned (if ok)
        response.status(HttpServletResponse.SC_CREATED);
    }
    try {
        return ClassUtil.invoke(webServices, method, serviceArgs);
    } catch (final RuntimeException e) {
        // If throwed exception was ValidationUserException, VUserException, SessionException, VSecurityException, RuntimeException
        // we re throw it
        final Throwable cause = e.getCause();
        if (cause instanceof InvocationTargetException) {
            final Throwable targetException = ((InvocationTargetException) cause).getTargetException();
            if (targetException instanceof ValidationUserException) {
                throw (ValidationUserException) targetException;
            } else if (targetException instanceof VUserException) {
                throw (VUserException) targetException;
            } else if (targetException instanceof SessionException) {
                throw (SessionException) targetException;
            } else if (targetException instanceof VSecurityException) {
                throw (VSecurityException) targetException;
            } else if (targetException instanceof RuntimeException) {
                throw (RuntimeException) targetException;
            }
        }
        throw e;
    }
}
Also used : WebServiceDefinition(io.vertigo.vega.webservice.metamodel.WebServiceDefinition) ValidationUserException(io.vertigo.vega.webservice.validation.ValidationUserException) WebServices(io.vertigo.vega.webservice.WebServices) SessionException(io.vertigo.vega.webservice.exception.SessionException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) VSecurityException(io.vertigo.vega.webservice.exception.VSecurityException) VUserException(io.vertigo.lang.VUserException)

Aggregations

VUserException (io.vertigo.lang.VUserException)10 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)3 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 MessageKey (io.vertigo.core.locale.MessageKey)2 IndexReader (org.apache.lucene.index.IndexReader)2 TooManyClauses (org.apache.lucene.search.BooleanQuery.TooManyClauses)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 TopDocs (org.apache.lucene.search.TopDocs)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 MessageText (io.vertigo.core.locale.MessageText)1 VSystemException (io.vertigo.lang.VSystemException)1 WebServices (io.vertigo.vega.webservice.WebServices)1 Address (io.vertigo.vega.webservice.data.domain.Address)1 Contact (io.vertigo.vega.webservice.data.domain.Contact)1 ContactView (io.vertigo.vega.webservice.data.domain.ContactView)1 SessionException (io.vertigo.vega.webservice.exception.SessionException)1 VSecurityException (io.vertigo.vega.webservice.exception.VSecurityException)1 WebServiceDefinition (io.vertigo.vega.webservice.metamodel.WebServiceDefinition)1 GET (io.vertigo.vega.webservice.stereotype.GET)1