Example usage for org.apache.ibatis.session SqlSession commit

List of usage examples for org.apache.ibatis.session SqlSession commit

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession commit.

Prototype

void commit();

Source Link

Document

Flushes batch statements and commits database connection.

Usage

From source file:net.cbtltd.rest.AbstractReservation.java

/**
 * Creates, updates or cancels a reservation.
 *
 * @param action the reservation parameter.
 * @param pos the POS code.//from w w w .  j ava  2 s.  c  o m
 * @param xsl the style sheet.
 * @return the provisional reservation.
 */
protected static synchronized Reservation cancelUnPaidBookings(Reservation action, String pos, String xsl) {
    Date timestamp = new Date();
    String message = "POST reservation?pos=" + pos + "&xsl=" + xsl + "\nparams " + action.paramString();
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Reservation result = null;
    try {

        @SuppressWarnings("unused")
        String partyid = Constants.decryptPos(pos);

        Product product = getProduct(sqlSession, pos, action.getProductid(), License.DEFAULT_WAIT);
        if (product == null) {
            throw new ServiceException(Error.product_id, action.getProductid());
        }

        if (action.notValidState()) {
            throw new ServiceException(Error.reservation_state, action.getId() + " " + action.getState());
        }

        if (!action.noId()) { //existing reservation
            result = sqlSession.getMapper(ReservationMapper.class).read(action.getId());
        }

        if (result == null) {
            throw new ServiceException(Error.reservation_id, action.getId());
        }

        int propertyManagerId = Integer.valueOf(action.getOrganizationid());
        PropertyManagerInfo propertyManagerInfo = sqlSession.getMapper(PropertyManagerInfoMapper.class)
                .readbypmid(propertyManagerId);

        PropertyManagerCancellationRule cancellationRule = PaymentHelper.getRuleForCurrentDate(sqlSession,
                action, propertyManagerInfo.getPropertyManagerId());
        double cancellationTransactionFee = 0;
        if (cancellationRule != null) {
            cancellationTransactionFee = cancellationRule.getCancellationTransactionFee() == null ? 0
                    : cancellationRule.getCancellationTransactionFee();
            @SuppressWarnings("unused")
            double amountToRenter = PaymentHelper.getCancellationAmountWithoutFee(action, cancellationRule,
                    action.getCurrency());
        }

        if (cancellationTransactionFee == 0) {
            result.setState(Reservation.State.Cancelled.name());
            sqlSession.getMapper(ReservationMapper.class).update(result);
            MonitorService.update(sqlSession, Data.Origin.XML_JSON, NameId.Type.Reservation, result);
        }
        sqlSession.commit();
    } catch (Throwable x) {
        x.printStackTrace();
        sqlSession.rollback();
        result = new Reservation();
        result.setMessage(message + "\nsetReservation failed " + x.getMessage());
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);

    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractReservationExt.java

/**
 * Creates a provisional reservation.//from   w ww  . ja va2s.c  om
 *
 * @param productid the productid
 * @param fromdate the fromdate
 * @param todate the todate
 * @param emailaddress the emailaddress
 * @param familyname the familyname
 * @param firstname the firstname
 * @param notes the reservation notes
 * @param quote the quoted price for the stay
 * @param tax the total tax on the quoted price for the stay
 * @param currency the currency code of the quote
 * @param pos the pos
 * @param test the test
 * @param xsl the xsl
 * @return the provisional
 */
protected static Provisional getProvisional(String productid, String fromdate, String todate,
        String emailaddress, String familyname, String firstname, String notes, String quote, String tax,
        String currency, String pos, Boolean test, String xsl) {
    Date timestamp = new Date();
    String message = "/reservation/" + productid + "/" + fromdate + "/" + todate + "/" + emailaddress + "/"
            + familyname + "/" + firstname + "/" + notes + "/" + quote + "/" + tax + "/" + currency + "?pos="
            + pos + "&test=" + test + "&xsl=" + xsl;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Provisional result = null;
    try {
        Party agent = Constants.getParty(sqlSession, pos);

        Product product = getProduct(sqlSession, pos, productid, License.DEFAULT_WAIT); //sqlSession.getMapper(ProductMapper.class).read(productid);
        if (product == null || !product.hasState(Constants.CREATED)) {
            throw new ServiceException(Error.product_id, productid);
        }
        if (product.notType(Product.Type.Accommodation.name())) {
            throw new ServiceException(Error.product_type, productid + " is not Accommodation");
        }
        if (product.noRank()) {
            throw new ServiceException(Error.product_not_online, productid);
        }

        if (emailaddress == null || emailaddress.isEmpty() || !Party.isEmailAddress(emailaddress)) {
            throw new ServiceException(Error.party_emailaddress, emailaddress);
        }
        if (fromdate == null || fromdate.isEmpty() || todate == null || todate.isEmpty()) {
            throw new ServiceException(Error.date_invalid, fromdate + " to " + todate);
        }
        if (!Constants.parseDate(todate).after(Constants.parseDate(fromdate))) {
            throw new ServiceException(Error.date_format, fromdate + " to " + todate);
        }
        if (familyname == null || familyname.isEmpty()) {
            throw new ServiceException(Error.family_name, familyname);
        }
        Party customer = sqlSession.getMapper(PartyMapper.class).readbyemailaddress(emailaddress);
        if (customer == null) {
            customer = new Party();
            customer.setEmailaddress(emailaddress);
            customer.setName(familyname, firstname);
            sqlSession.getMapper(PartyMapper.class).create(customer);
            RelationService.create(sqlSession, Relation.ORG_PARTY_ + Party.Type.Customer.name(), agent.getId(),
                    customer.getId());
            RelationService.create(sqlSession, Relation.ORG_PARTY_ + Party.Type.Customer.name(),
                    product.getSupplierid(), customer.getId());
        }

        Reservation reservation = new Reservation();
        reservation.setOrganizationid(product.getSupplierid());
        reservation.setActorid(Party.NO_ACTOR);
        reservation.setAgentid(agent.getId());
        reservation.setCustomerid(customer.getId());
        reservation.setFromdate(Constants.parseDate(fromdate));
        reservation.setTodate(Constants.parseDate(todate));
        reservation.setDate(new Date());
        reservation.setArrivaltime("14:00");
        reservation.setDeparturetime("10:00");
        reservation.setDuedate(reservation.getDate());
        reservation.setDonedate(null);
        reservation.setUnit(product.getUnit());
        reservation.setCurrency(product.getCurrency());
        reservation.setNotes(notes == null || notes.isEmpty() ? "XML Reservation Request" : notes);
        reservation.setState(Reservation.State.Provisional.name());
        reservation.setProductid(productid);
        ReservationService.computePrice(sqlSession, reservation, null);

        if (quote != null && tax != null) {
            Double oldquote = reservation.getQuote();
            if (currency != null && !product.hasCurrency(currency)) {
                reservation.setQuote(WebService.getRate(sqlSession, currency, product.getCurrency())
                        * ((oldquote == null) ? 0.0 : oldquote));
            }
            Double newquote = Double.valueOf(quote);
            reservation.setQuote(newquote);
            if (reservation.hasQuotedetail()) {
                for (net.cbtltd.shared.Price price : reservation.getQuotedetail()) {
                    if (price.hasType(net.cbtltd.shared.Price.TAX_EXCLUDED)
                            || price.hasType(net.cbtltd.shared.Price.TAX_INCLUDED)
                            || price.hasType(net.cbtltd.shared.Price.TAX_ON_TAX)) {
                        price.setValue(price.getValue() * newquote / oldquote);
                    }
                }
            }
        }

        //TODO: save tax allocations
        reservation.setCost(
                reservation.getQuote() * ReservationService.getDiscountfactor(sqlSession, reservation));

        String textid = NameId.Type.Party.name() + product.getSupplierid() + Text.Code.Contract.name();
        String terms = TextService.notes(sqlSession, textid, Language.EN);

        if (!test) {
            reservation.setName(
                    SessionService.pop(sqlSession, reservation.getOrganizationid(), Serial.RESERVATION));
            sqlSession.getMapper(ReservationMapper.class).create(reservation);

            reservation.setCollisions(ReservationService.getCollisions(sqlSession, reservation));
            if (reservation.hasCollisions()) {
                throw new ServiceException(Error.product_not_available,
                        productid + " " + fromdate + " " + todate);
            }

            message = "The provisional reservation number is " + reservation.getName();
            sqlSession.commit();
            EmailService.provisionalReservation(sqlSession, reservation);
        }

        result = new Provisional(reservation.getId(), reservation.getName(), productid, product.getName(),
                fromdate, todate, emailaddress, familyname, firstname,
                String.valueOf(NameId.round(reservation.getPrice())),
                String.valueOf(NameId.round(reservation.getQuote())),
                String.valueOf(NameId.round(reservation.getCost())),
                String.valueOf(NameId.round(reservation.getDeposit(reservation.getQuote()))),
                reservation.getCurrency(), getAlert(sqlSession, reservation), reservation.getNotes(), terms,
                message, xsl);
    } catch (Throwable x) {
        sqlSession.rollback();
        LOG.error(message + "\n" + x.getMessage());
        result = new Provisional(null, null, productid, null, fromdate, todate, emailaddress, familyname,
                firstname, null, null, null, null, null, null, null, null, message + " " + x.getMessage(), xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractReservationExt.java

/**
 * Gets the upload.//from  w w  w . ja  v  a2 s  . c  om
 *
 * @param reservationid the reservationid
 * @param productid the productid
 * @param fromdate the fromdate
 * @param todate the todate
 * @param emailaddress the emailaddress
 * @param familyname the familyname
 * @param firstname the firstname
 * @param price the price
 * @param currency the currency
 * @param pos the pos
 * @param xsl the xsl
 * @return the upload
 */
protected static Download getUpload(String reservationid, String productid, String fromdate, String todate,
        String emailaddress, String familyname, String firstname, String price, String currency, String pos,
        String xsl) {
    Date timestamp = new Date();
    String message = "/reservation/upload/" + reservationid + "/" + productid + "/" + fromdate + "/" + todate
            + "/" + emailaddress + "/" + familyname + "/" + firstname + "/" + price + "/" + currency + "?pos="
            + pos + "&xsl=" + xsl;
    LOG.debug(message);

    //TODO: CJM
    //if ("76e3fef6c90093eb".equalsIgnoreCase(pos)) {
    //   return new Download("0", "0", "0", "2010-01-01", "2020-12-31", "aaa@bbb.com", "familyname", "firstname", null, null, null, price, currency, message, xsl);
    //}

    SqlSession sqlSession = RazorServer.openSession();
    Download result = null;
    try {
        Party organization = Constants.getParty(sqlSession, pos);
        Party customer = organization;

        if (fromdate == null || fromdate.isEmpty() || todate == null || todate.isEmpty()) {
            throw new ServiceException(Error.date_range, fromdate + " to " + todate);
        }
        if (!Constants.parseDate(todate).after(Constants.parseDate(fromdate))) {
            throw new ServiceException(Error.date_format, fromdate + " to " + todate);
        }

        Product product = getProduct(sqlSession, pos, productid, License.DEFAULT_WAIT); //sqlSession.getMapper(ProductMapper.class).read(productid);
        if (product == null || !product.hasState(Constants.CREATED)) {
            throw new ServiceException(Error.product_id, productid);
        }
        if (product.noRank()) {
            throw new ServiceException(Error.product_not_online, productid);
        }
        if (product.notType(Product.Type.Accommodation.name())) {
            throw new ServiceException(Error.product_id, productid + " not type Accommodation");
        }
        if (!organization.hasId(product.getSupplierid())) {
            throw new ServiceException(Error.pos_party,
                    organization.getId() + " pos code <> manager " + product.getSupplierid());
        }

        Double quote = Double.valueOf(price);
        if (!product.hasCurrency(currency)) { // convert
            Double exchangerate = WebService.getRate(sqlSession, currency, product.getCurrency(), new Date());
            quote = NameId.round(quote * exchangerate);
        }

        if (emailaddress != null && !emailaddress.isEmpty() && !emailaddress.equals(Constants.BLANK)
                && Party.isEmailAddress(emailaddress) && familyname != null && !familyname.isEmpty()
                && !familyname.equals(Constants.UNALLOCATED)) {
            //Find or create customer
            customer = sqlSession.getMapper(PartyMapper.class).readbyemailaddress(emailaddress);
            if (customer == null) {
                customer = new Party();
                customer.setEmailaddress(emailaddress);
                customer.setName(familyname, firstname);
                customer.setCountry(organization.getCountry());
                customer.setCreatorid(organization.getId());
                customer.setCurrency(organization.getCurrency());
                customer.setLanguage(organization.getLanguage());
                customer.setLocationid(organization.getLocationid());
                customer.setState(Party.CREATED);
                sqlSession.getMapper(PartyMapper.class).create(customer);
                RelationService.create(sqlSession, Relation.ORG_PARTY_ + Party.Type.Customer.name(),
                        organization.getId(), customer.getId());
                RelationService.create(sqlSession, Relation.ORG_PARTY_ + Party.Type.Customer.name(),
                        product.getSupplierid(), customer.getId());
            }
        }
        //LOG.debug("ReservationService getUpload 1 " + customer);

        Reservation reservation = new Reservation();
        reservation.setName(reservationid);
        reservation.setOrganizationid(product.getSupplierid());
        //         reservation.setOrganizationid(organization.getId());
        if (reservationid.contains(".")) {
            reservation = sqlSession.getMapper(ReservationMapper.class).readbyforeignid(reservationid);
        } else {
            reservation = sqlSession.getMapper(ReservationMapper.class).readbyname(reservation);
        }

        //LOG.debug("ReservationService getUpload 2 " + reservation);

        if (reservation == null && reservationid.contains(".")) { // upload new foreign reservation
            //Create reservation
            reservation = new Reservation();
            String supplierId = product.getSupplierid();
            PropertyManagerInfo proeprtyManagerInfo = sqlSession.getMapper(PropertyManagerInfoMapper.class)
                    .readbypmid(Integer.valueOf(supplierId));
            reservation.setOrganizationid(supplierId);
            reservation.setActorid(Party.NO_ACTOR);
            reservation.setAgentid(organization.getId());
            reservation.setCustomerid(customer.getId());
            reservation.setFromdate(Constants.parseDate(fromdate));
            reservation.setTodate(Constants.parseDate(todate));
            reservation.setArrivaltime(proeprtyManagerInfo.getCheckInTime().toString());
            reservation.setDeparturetime(proeprtyManagerInfo.getCheckOutTime().toString());
            reservation.setDate(new Date());
            reservation.setDuedate(reservation.getDate());
            reservation.setDonedate(null);
            //reservation.setDeposit();
            reservation.setUnit(product.getUnit());
            reservation.setCurrency(product.getCurrency());
            reservation.setNotes("Reservation Upload by " + organization.getName());
            reservation.setPrice(NameId.round(Double.valueOf(price)));
            reservation.setPrice(quote);
            reservation.setQuote(reservation.getPrice());
            reservation.setCost(reservation.getPrice());
            reservation.setState(Reservation.State.Confirmed.name());
            reservation.setProductid(productid);

            reservation.setName(
                    SessionService.pop(sqlSession, reservation.getOrganizationid(), Serial.RESERVATION));
            sqlSession.getMapper(ReservationMapper.class).create(reservation);
            RelationService.load(sqlSession, Downloaded.RESERVATION_UPLOAD, reservation.getId(), reservationid);
            RelationService.load(sqlSession, Downloaded.RESERVATION_DOWNLOAD, reservation.getName(),
                    reservation.getOrganizationid());
            //To prevent an uploaded reservation from being downloaded again
        } else if (reservation != null) { // update reservation
            reservation.setOrganizationid(product.getSupplierid());
            reservation.setCustomerid(customer.getId());
            reservation.setFromdate(Constants.parseDate(fromdate));
            reservation.setTodate(Constants.parseDate(todate));
            reservation.setUnit(product.getUnit());
            reservation.setCurrency(product.getCurrency());
            reservation.setPrice(NameId.round(Double.valueOf(price)));
            //            reservation.setPrice(Double.valueOf(price));
            reservation.setQuote(reservation.getPrice());
            reservation.setCost(reservation.getPrice());
            reservation.setState(Reservation.State.Confirmed.name());
            reservation.setProductid(productid);
            sqlSession.getMapper(ReservationMapper.class).update(reservation);
        } else {
            throw new ServiceException(Error.reservation_upload_id, reservationid);
        }
        //LOG.debug("getUpload 3 " + reservation);

        //TODO: CJM         reservation.setCollisions(sqlSession.getMapper(ReservationMapper.class).collisions(reservation));
        reservation.setCollisions(ReservationService.getCollisions(sqlSession, reservation));
        if (reservation.hasCollisions()) {
            throw new ServiceException(Error.product_not_available,
                    productid + " from " + fromdate + " to " + todate);
        }
        result = new Download(reservation.getName(), reservationid, productid, fromdate, todate, emailaddress,
                familyname, firstname, null, null, null, price, currency, message, xsl);
        MonitorService.update(sqlSession, Data.Origin.XML_JSON, NameId.Type.Reservation, reservation);
        sqlSession.commit();
    } catch (Throwable x) {
        sqlSession.rollback();
        if (x != null && x.getMessage() != null
                && !x.getMessage().startsWith(Error.product_not_available.name())) {
            LOG.error(message + "\n" + x.getMessage());
        }
        result = new Download(null, reservationid, productid, fromdate, todate, emailaddress, familyname,
                firstname, null, null, null, price, currency, message + " " + x.getMessage(), xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractReservationExt.java

/**
 * Sets the provisional reservation./*from w w w.  j av a2s  . c o m*/
 *
 * @param reservationid the reservation ID
 * @param productid the product ID
 * @param fromdate the arrival date
 * @param todate the departure date
 * @param quote the quoted price
 * @param cost the sto rate
 * @param currency the quote currency
 * @param pos the pos code
 * @param test the test flag
 * @param xsl the style sheet
 * @return the provisional reservation
 */
protected static Provisional setProvisional(String reservationid, String productid, String fromdate,
        String todate, Double quote, Double cost, String currency, String pos, Boolean test, String xsl) {

    Date timestamp = new Date();
    String message = "/reservation/update/" + reservationid + "/" + productid + "/" + fromdate + "/" + todate
            + "/" + quote + "/" + cost + "/" + currency + "?pos=" + pos + "&test=" + test + "&xsl=" + xsl;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Provisional result = null;
    try {
        Party agent = Constants.getParty(sqlSession, pos);

        if (reservationid == null || reservationid.isEmpty()) {
            throw new ServiceException(Error.reservation_id, reservationid);
        }
        Reservation reservation = sqlSession.getMapper(ReservationMapper.class).read(reservationid);
        if (reservation == null || reservationid.isEmpty()) {
            throw new ServiceException(Error.reservation_id, reservationid);
        }
        if (reservation.hasState(Reservation.State.Initial.name())
                || reservation.hasState(Reservation.State.Closed.name())
                || reservation.hasState(Reservation.State.Cancelled.name())
                || reservation.hasState(Reservation.State.Arrived.name())
                || reservation.hasState(Reservation.State.Departed.name())
                || reservation.hasState(Reservation.State.Final.name())) {
            throw new ServiceException(Error.reservation_state,
                    reservation.getName() + " " + reservation.getState());
        }

        if (productid == null || productid.isEmpty()) {
            productid = reservation.getProductid();
        }
        Product product = getProduct(sqlSession, pos, productid, License.DEFAULT_WAIT); //sqlSession.getMapper(ProductMapper.class).read(productid);

        if (product == null) {
            throw new ServiceException(Error.product_id, productid);
        }
        if (!product.hasState(Constants.CREATED)) {
            throw new ServiceException(Error.product_state, productid + " has state " + product.getState());
        }
        if (product.notType(Product.Type.Accommodation.name())) {
            throw new ServiceException(Error.product_type, product.getType());
        }
        if (product.noRank()) {
            throw new ServiceException(Error.product_not_online, productid);
        }
        reservation.setProductid(productid);

        if (fromdate != null && !fromdate.isEmpty()) {
            reservation.setFromdate(Constants.parseDate(fromdate));
        }
        if (todate != null && !todate.isEmpty()) {
            reservation.setTodate(Constants.parseDate(todate));
        }
        if (!reservation.getTodate().after(reservation.getFromdate())) {
            throw new ServiceException(Error.date_range, fromdate + " to " + todate);
        }

        if (quote != null && quote > 0.0 && reservation.hasCurrency(currency)) {
            reservation.setQuote(quote);
        }

        if (cost != null && cost > 0.0 && reservation.hasCurrency(currency)) {
            reservation.setCost(cost);
        }

        if (!test) {
            sqlSession.getMapper(ReservationMapper.class).update(reservation);

            //TODO: CJM            reservation.setCollisions(sqlSession.getMapper(ReservationMapper.class).collisions(reservation));
            reservation.setCollisions(ReservationService.getCollisions(sqlSession, reservation));
            if (reservation.hasCollisions()) {
                throw new ServiceException(Error.product_not_available,
                        productid + " from " + fromdate + " to " + todate);
            }
            MonitorService.update(sqlSession, Data.Origin.XML_JSON, NameId.Type.Reservation, reservation);
            sqlSession.commit();
        }
        result = new Provisional(productid, product.getName(), Constants.formatDate(reservation.getFromdate()),
                Constants.formatDate(reservation.getTodate()),
                String.valueOf(NameId.round(reservation.getQuote())), reservation.getCurrency(),
                getAlert(sqlSession, reservation), message, xsl);
    } catch (Throwable x) {
        sqlSession.rollback();
        LOG.error(message + "\n" + x.getMessage());
        result = new Provisional(productid, null, fromdate, todate, String.valueOf(NameId.round(quote)),
                currency, null, message, xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractReservationExt.java

/**
 * Cancel download./* www. j  a va 2  s .  com*/
 *
 * @param reservationname the reservation number
 * @param pos the pos
 * @param test the test
 * @param xsl the xsl
 * @return the response
 */
protected static Response cancelDownload(String reservationname, String pos, Boolean test, String xsl) {

    Date timestamp = new Date();
    String message = "/reservation/cancel/" + reservationname + "?pos=" + pos + "&test=" + test + "&xsl=" + xsl;
    LOG.debug(message);

    SqlSession sqlSession = RazorServer.openSession();
    Response result = null;
    try {
        //String organizationid = Constants.decryptPos(pos);
        Party organization = Constants.getParty(sqlSession, pos);
        Reservation reservation = new Reservation();
        reservation.setName(reservationname);
        reservation.setOrganizationid(organization.getId());
        if (reservationname.contains(".")) {
            reservation = sqlSession.getMapper(ReservationMapper.class).readbyforeignid(reservationname);
        } else {
            reservation = sqlSession.getMapper(ReservationMapper.class).readbyname(reservation);
        }
        if (reservation == null) {
            reservation = sqlSession.getMapper(ReservationMapper.class).read(reservationname);
        }
        if (reservation == null || !organization.hasId(reservation.getOrganizationid())) {
            throw new ServiceException(Error.reservation_bad, reservationname);
        }

        getProduct(sqlSession, pos, reservation.getProductid(), License.DEFAULT_WAIT);

        if (!test) {
            reservation.setState(Reservation.State.Cancelled.name());
            //EmailService.canceledReservation(sqlSession, organizationid, reservation);
            reservation.setDonedate(new Date());
            sqlSession.getMapper(ReservationMapper.class).update(reservation);
            MonitorService.update(sqlSession, Data.Origin.XML_JSON, NameId.Type.Reservation, reservation);
            sqlSession.commit();
        }
        result = new Response("Canceled " + reservation.getName(), message, xsl);
    } catch (Throwable x) {
        sqlSession.rollback();
        LOG.error(message + "\n" + x.getMessage());
        result = new Response("Error " + x.getMessage(), message, xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractReservationExt.java

/**
 * Gets the download./*from   w  w w  .  j a  v  a  2 s .c o m*/
 *
 * @param pos the pos
 * @param xsl the xsl
 * @return the download
 */
protected static Downloads getDownload(String pos, String xsl) {

    Date timestamp = new Date();
    String message = "/reservation/download/" + "?pos=" + pos + "&xsl=" + xsl;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Downloads result = null;
    try {
        String organizationid = Constants.decryptPos(pos);
        ArrayList<Download> downloads = sqlSession.getMapper(ReservationMapper.class).download(organizationid);
        //LOG.debug("downloads " + downloads);
        if (downloads == null || downloads.isEmpty()) {
            throw new ServiceException(Error.reservation_download, organizationid);
        }
        result = new Downloads(message, downloads, xsl);
        for (Download download : downloads) {
            RelationService.load(sqlSession, Downloaded.RESERVATION_DOWNLOAD, download.getReservationid(),
                    organizationid);
        }
        sqlSession.commit();
    } catch (Throwable x) {
        sqlSession.rollback();
        LOG.error(message + "\n" + x.getMessage());
        result = new Downloads(message + " " + x.getMessage(), null, xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractReservationExt.java

/**
 * Gets the downloads./*from  w w w.j av a2  s .  c om*/
 *
 * @param pos the pos
 * @param xsl the xsl
 * @return the downloads
 */
protected static Downloads getDownloads(String pos, String xsl) {

    Date timestamp = new Date();
    String message = "/reservation/downloads/" + "?pos=" + pos + "&xsl=" + xsl;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Downloads result = null;
    try {
        String organizationid = Constants.decryptPos(pos);
        ArrayList<Download> downloads = sqlSession.getMapper(ReservationMapper.class).downloads(organizationid);
        if (downloads == null || downloads.isEmpty()) {
            throw new ServiceException(Error.reservation_download, organizationid);
        }
        result = new Downloads(message, downloads, xsl);
        for (Download download : downloads) {
            RelationService.load(sqlSession, Downloaded.RESERVATION_DOWNLOAD, download.getReservationid(),
                    organizationid);
        }
        sqlSession.commit();
    } catch (Throwable x) {
        sqlSession.rollback();
        LOG.error(message + "\n" + x.getMessage());
        result = new Downloads(message + " " + x.getMessage(), null, xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.AbstractReservationExt.java

/**
 * Gets the uploads.//from  w  ww  . ja v  a 2  s . co  m
 *
 * @param pos the pos
 * @param xsl the xsl
 * @return the uploads
 */
protected static Downloads getUploads(String pos, String xsl) {

    Date timestamp = new Date();
    String message = "/reservation/uploads/" + "?pos=" + pos + "&xsl=" + xsl;
    LOG.debug(message);
    SqlSession sqlSession = RazorServer.openSession();
    Downloads result = null;
    try {
        String organizationid = Constants.decryptPos(pos);
        ArrayList<Download> downloads = sqlSession.getMapper(ReservationMapper.class).uploads(organizationid);
        if (downloads == null || downloads.isEmpty()) {
            throw new ServiceException(Error.reservation_upload, organizationid);
        }
        result = new Downloads(message, downloads, xsl);
        sqlSession.commit();
    } catch (Throwable x) {
        sqlSession.rollback();
        LOG.error(message + "\n" + x.getMessage());
        result = new Downloads(message + " " + x.getMessage(), null, xsl);
    } finally {
        sqlSession.close();
    }
    LOG.debug(result);
    MonitorService.monitor(message, timestamp);
    return result;
}

From source file:net.cbtltd.rest.interhome.A_Handler.java

License:Open Source License

/**
 * Create reservation.//from   w  ww.j a v  a  2  s .  c o m
 *
 * @param sqlSession the current SQL session.
 * @param reservation the reservation to be created.
 * @param product the product to be reserved.
 */
public void createReservation(SqlSession sqlSession, Reservation reservation) {
    Date timestamp = new Date();
    String message = "createReservation " + reservation.getId();
    LOG.debug(message);
    try {
        if (reservation.notActive()) {
            throw new ServiceException(Error.reservation_state,
                    reservation.getId() + " state " + reservation.getState());
        }

        Product product = sqlSession.getMapper(ProductMapper.class).read(reservation.getProductid());
        if (product == null) {
            throw new ServiceException(Error.product_id, reservation.getProductid());
        }
        if (reservation.noAgentid()) {
            throw new ServiceException(Error.reservation_agentid);
        }
        Party agent = sqlSession.getMapper(PartyMapper.class).read(reservation.getAgentid());
        if (agent == null) {
            throw new ServiceException(Error.party_id, reservation.getAgentid());
        }
        if (reservation.noCustomerid()) {
            reservation.setCustomerid(Party.NO_ACTOR);
        }
        Party customer = sqlSession.getMapper(PartyMapper.class).read(reservation.getCustomerid());
        if (customer == null) {
            throw new ServiceException(Error.reservation_customerid, reservation.getCustomerid());
        }

        computePrice(sqlSession, reservation, product.getAltid(), reservation.getCurrency());

        RetailerBookingInputValue rq = initializeRequest(product, reservation, agent, customer);

        LOG.debug("\nretailerBooking rq=" + rq + "\n");
        RetailerBookingReturnValue rs = getAuthenticatedPort(getRetailerCode(reservation.getCurrency()))
                .retailerBooking(rq);
        LOG.debug("\nretailerBooking rs=" + rs + "\n");

        if (rs.isOk()) {
            reservation.setAltid(rs.getBookingID());
            reservation.setAltpartyid(getAltpartyid());
            reservation.setMessage(null);
        } else {
            throw new ServiceException(Error.reservation_api,
                    rs.getErrors().getError().get(0).getDescription());
        }
    } catch (Throwable x) {
        reservation.setMessage(x.getMessage());
        reservation.setState(Reservation.State.Cancelled.name());
        LOG.error(x.getMessage());
    }
    sqlSession.getMapper(ReservationMapper.class).update(reservation);
    sqlSession.commit();
    MonitorService.monitor(message, timestamp);
}

From source file:net.cbtltd.rest.interhome.A_Handler.java

License:Open Source License

/**
 * Transfer accommodation alerts.// w  w w. ja  v  a  2s.com
 *
 * @param sqlSession the current SQL session.
 */
public synchronized void readAlerts() {
    Date timestamp = new Date();
    String message = "readAlerts " + getAltpartyid();
    LOG.debug(message);
    String fn = null;
    final SqlSession sqlSession = RazorServer.openSession();
    try {
        JAXBContext jc = JAXBContext.newInstance("net.cbtltd.rest.interhome.alert");
        Unmarshaller um = jc.createUnmarshaller();

        fn = "alert_en.xml";
        Alerts alerts = (Alerts) um.unmarshal(ftp(fn));
        int i = 0;
        for (Alert alert : alerts.getAlert()) {
            LOG.debug(alert);
            net.cbtltd.shared.Alert action = new net.cbtltd.shared.Alert();
            Product product = sqlSession.getMapper(ProductMapper.class)
                    .altread(new NameId(getAltpartyid(), alert.getCode()));
            if (product == null) {
                LOG.error(Error.product_id.getMessage() + " " + alert.getCode());
            } else {
                action.setEntitytype(NameId.Type.Product.name());
                action.setEntityid(product.getId());
                action.setFromdate(alert.getStartdate().toGregorianCalendar().getTime());
                action.setTodate(alert.getEnddate().toGregorianCalendar().getTime());
                action.setName(alert.getText());
                net.cbtltd.shared.Alert duplicate = sqlSession.getMapper(AlertMapper.class).duplicate(action);
                if (duplicate == null) {
                    sqlSession.getMapper(AlertMapper.class).create(action);
                } else {
                    sqlSession.getMapper(AlertMapper.class).update(action);
                }
                sqlSession.commit();
                LOG.debug(i++ + " " + alert.getCode() + " = " + action.getId());
            }
            wait(getAlertwait());
        }
    } catch (Throwable x) {
        sqlSession.rollback();
        LOG.error(x.getMessage());
    } finally {
        sqlSession.close();
        delete(fn);
    }
    MonitorService.monitor(message, timestamp);
}