All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.vgregion.notifications.service.RaindanceInvoiceService Maven / Gradle / Ivy

/**
 * Copyright 2010 Västra Götalandsregionen
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 */

package se.vgregion.notifications.service;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import se.vgregion.raindancenotifier.domain.InvoiceNotification;
import se.vgregion.raindancenotifier.services.RaindanceInvoicesService;

import java.io.IOException;
import java.util.List;

/**
 * @author Patrik Bergström
 */
@Service
class RaindanceInvoiceService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RaindanceInvoiceService.class);

    private RaindanceInvoicesService invoicesService;

    public RaindanceInvoiceService() {
        // Empty constructor is needed to make CGLIB happy
    }

    @Autowired
    public RaindanceInvoiceService(RaindanceInvoicesService invoicesService) {
        this.invoicesService = invoicesService;
    }

    public List getInvoices(final String userId, boolean cachedResult) {
        List invoices;
        try {
            invoices = invoicesService.getInvoices(userId);
        } catch (RuntimeException ex) {
            LOGGER.error(ex.getMessage()); // Do it this way since the logs would be full with stacktraces otherwise
            logCause(ex);
            return null;
        }
        return invoices;
    }

    private void logCause(Throwable ex) {
        Throwable cause = ex.getCause();
        if (cause != null) {
            LOGGER.error("Cause: " + cause.getMessage());
            logCause(cause);
        }
    }

    public String getInvoicesJson(final String userId) {
        List invoices = invoicesService.getInvoices(userId);
        ObjectWriter writer = new ObjectMapper().writerWithDefaultPrettyPrinter();
        try {
            return writer.writeValueAsString(invoices);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            return "Internt fel.";
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy