Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014 Zenika * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * * @author M. Labusquire */ package com.zenika.vertx.lib.asongo.then; import com.mongodb.MongoException; import com.zenika.vertx.lib.asongo.AsongoConfiguration; import com.zenika.vertx.lib.asongo.domain.result.DocumentResult; import com.zenika.vertx.lib.asongo.mapper.marshall.Unmarshaller; import com.zenika.vertx.lib.asongo.operation.DocumentMongoOperator; import org.vertx.java.core.Handler; import org.vertx.java.core.eventbus.Message; import org.vertx.java.core.json.JsonObject; import org.vertx.java.core.logging.Logger; import org.vertx.java.core.logging.impl.LoggerFactory; /** * This implementation is convenient for any generified return type * T is the return type * K is the type of document * * @author M. Labusquire */ public abstract class DocumentThenTemplate<T, K> implements Then<T> { private final static Logger LOGGER = LoggerFactory.getLogger(DocumentThenTemplate.class); protected final DocumentMongoOperator operator; protected final AsongoConfiguration configuration; protected DocumentThenTemplate(DocumentMongoOperator operator, AsongoConfiguration configuration) { if (operator == null || configuration == null) { throw new IllegalArgumentException("An operator and a configuration is required"); } this.operator = operator; this.configuration = configuration; } @Override public void then(final Handler<T> handler) { final JsonObject command = getCommand(); final Class<K> clazz = getClazz(); if (LOGGER.isDebugEnabled()) LOGGER.debug("The command " + command + ", is send to " + configuration.getMongoPersistorAdress()); configuration.getEventBus().send(configuration.getMongoPersistorAdress(), command, new Handler<Message<JsonObject>>() { @Override public void handle(final Message<JsonObject> message) { Unmarshaller unmarshaller = configuration.getMapper().getUnmarshaller(); final DocumentResult<K> presult = unmarshaller.unmarshall(message.body().toString(), DocumentResult.class, clazz); if (LOGGER.isDebugEnabled()) LOGGER.debug("The result is " + presult); if (presult.isNotError()) { handler.handle(operator.<T, K>getResult(presult)); } else { String errorMsg = presult.getMessage(); LOGGER.error("Bad request to mongo " + errorMsg + " with the command " + command); throw new MongoException( "Bad request to mongo " + errorMsg + " with the command " + command); } } }); } protected abstract JsonObject getCommand(); protected abstract Class<K> getClazz(); }