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

org.xlcloud.console.controllers.utils.AccountsLazyLoader Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.console.controllers.utils;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.xlcloud.console.controllers.request.RequestParameters;
import org.xlcloud.console.extensions.scope.ViewScoped;
import org.xlcloud.service.Account;
import org.xlcloud.service.api.AccountsApi;

/**
 * Lazy loader for accounts.
 * @author Konrad Król, AMG.net
 */
@Named
@ViewScoped
public class AccountsLazyLoader implements Serializable {
    
    private static final long serialVersionUID = -5822897808977079642L;
    
    private Map accounts = Collections.synchronizedMap(new HashMap());
    
    @Inject
    private AccountsApi accountsApi;
    
    @Inject
    RequestParameters requestParams;
    
    /**
     * Retrieves account data from the accounts api.
     * @param accountId requested account id
     */
    public void loadAccount(Long accountId) {
        if(accountId != null && !accounts.containsKey(accountId)) {
            accounts.put(accountId, accountsApi.getAccount(accountId));
        }
        requestParams.addCallbackParameter("stopPoller", true);
    }
    
    /**
     * Returns the requeted account.
     * The account has to be loaded first using {@link #loadAccount(Long)}, otherwise: {@code null} is returned.
     * @param accountId requested account id.
     * @return account
     */
    public Account getAccount(Long accountId) {
        return accounts.get(accountId);
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy