/*
 * CODENVY CONFIDENTIAL
 * __________________
 *
 *  [2012] - [2014] Codenvy, S.A.
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.saas;

import com.codenvy.api.account.server.dao.AccountDao;
import com.codenvy.api.core.ApiException;

import javax.annotation.security.RolesAllowed;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

/**
 * Service to call charging over REST
 *
 * @author Alexander Garagatyi
 */
// must be eager singleton
@Path("billing/saas")
@Singleton
public class SaasBillingRestService {

    private final SaasBillingService saasBillingService;
    private final AccountDao accountDao;

    @Inject
    public SaasBillingRestService(SaasBillingService saasBillingService, AccountDao accountDao) {
        this.saasBillingService = saasBillingService;
        this.accountDao = accountDao;
    }

    @POST
    @Path("charge")
    @RolesAllowed({"system/admin", "system/manager"})
    public void chargeAccountsForPreviousPeriodAsynchronously() throws ApiException {
        saasBillingService.chargeAccounts();
    }

    @POST
    @Path("charge/{accountId}")
    @RolesAllowed({"system/admin", "system/manager"})
    public void chargeAccountForPreviousPeriod(@PathParam("accountId") String accountId) throws ApiException {
        saasBillingService.chargeAccount(accountId);
    }
}

@@ -659,16 +659,67 @@
 .saas;%0A%0A
+import com.codenvy.api.account.server.dao.Account;%0A
 import c
@@ -764,16 +764,16 @@
 untDao;%0A
-
 import c
@@ -992,16 +992,43 @@
 thParam;
+%0Aimport java.util.Calendar;
 %0A%0A/**%0A *
@@ -1643,22 +1643,8 @@
 riod
-Asynchronously
 () t
@@ -1915,32 +1915,32 @@
  ApiException %7B%0A
-
         saasBill
@@ -1981,11 +1981,1318 @@
 );%0A    %7D
+%0A%0A    @POST%0A    @Path(%22charge/%7BaccountId%7D/current%22)%0A    @RolesAllowed(%7B%22system/admin%22, %22system/manager%22%7D)%0A    public void chargeAccountForCurrentMonth(@PathParam(%22accountId%22) String accountId) throws ApiException %7B%0A        final Account account = accountDao.getById(accountId);%0A%0A        final Calendar currentMonthStart = Calendar.getInstance();%0A        currentMonthStart.set(Calendar.DATE, currentMonthStart.getActualMinimum(Calendar.DAY_OF_MONTH));%0A        currentMonthStart.set(Calendar.HOUR_OF_DAY, currentMonthStart.getActualMinimum(Calendar.HOUR_OF_DAY));%0A        currentMonthStart.set(Calendar.MINUTE, 0);%0A        currentMonthStart.set(Calendar.SECOND, 0);%0A        currentMonthStart.set(Calendar.MILLISECOND, 0);%0A%0A        final Calendar currentMonthEnd = Calendar.getInstance();%0A        currentMonthEnd.add(Calendar.MONTH, 1);%0A        currentMonthEnd.set(Calendar.DATE, currentMonthEnd.getActualMinimum(Calendar.DAY_OF_MONTH));%0A        currentMonthEnd.set(Calendar.HOUR_OF_DAY, currentMonthEnd.getActualMinimum(Calendar.HOUR_OF_DAY));%0A        currentMonthEnd.set(Calendar.MINUTE, 0);%0A        currentMonthEnd.set(Calendar.SECOND, 0);%0A        currentMonthEnd.set(Calendar.MILLISECOND, 0);%0A%0A        saasBillingService.chargeAccount(account, currentMonthStart.getTime(), currentMonthEnd.getTime());%0A    %7D
 %0A%7D%0A

add rest method to charge for current month