AccountDaoWrapper.java

175 lines | 6.604 kB Blame History Raw Download
/*
 * Copyright 2010-2011 Ning, Inc.
 *
 * Ning licenses this file to you 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 com.ning.billing.account.dao;

import java.util.List;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.IDBI;
import org.skife.jdbi.v2.TransactionCallback;
import org.skife.jdbi.v2.TransactionStatus;
import com.google.inject.Inject;
import com.ning.billing.account.api.Account;
import com.ning.billing.account.api.AccountChangeNotification;
import com.ning.billing.account.api.AccountCreationNotification;
import com.ning.billing.account.api.CustomField;
import com.ning.billing.account.api.DefaultAccount;
import com.ning.billing.account.api.FieldStore;
import com.ning.billing.account.api.Tag;
import com.ning.billing.account.api.user.AccountChangeNotificationDefault;
import com.ning.billing.account.api.user.AccountCreationEventDefault;
import com.ning.billing.util.eventbus.EventBus;

public class AccountDaoWrapper implements AccountDao {
    private final AccountDao accountDao;
    private final IDBI dbi; // needed for transaction support
    private final EventBus eventBus;

    @Inject
    public AccountDaoWrapper(IDBI dbi, EventBus eventBus) {
        this.dbi = dbi;
        this.eventBus = eventBus;
        this.accountDao = dbi.onDemand(AccountDao.class);
    }

    @Override
    public Account getAccountByKey(final String key) {
        return dbi.inTransaction(new TransactionCallback<Account>() {
            @Override
            public Account inTransaction(Handle conn, TransactionStatus status) throws Exception {
                try {
                    conn.begin();
                    Account account = accountDao.getAccountByKey(key);

                    if (account != null) {
                        FieldStoreDao fieldStoreDao = conn.attach(FieldStoreDao.class);
                        List<CustomField> fields = fieldStoreDao.load(account.getId().toString(), account.getObjectName());

                        account.getFields().clear();
                        if (fields != null) {
                            for (CustomField field : fields) {
                                account.getFields().setValue(field.getName(), field.getValue());
                            }
                        }

                        TagStoreDao tagStoreDao = conn.attach(TagStoreDao.class);
                        List<Tag> tags = tagStoreDao.load(account.getId().toString(), account.getObjectName());
                        account.clearTags();

                        if (tags != null) {
                            account.addTags(tags);
                        }
                    }

                    return account;
                } catch (Throwable t) {
                    return null;
                }
            }
        });
    }

    @Override
    public Account getById(final String id) {
        return dbi.inTransaction(new TransactionCallback<Account>() {
            @Override
            public Account inTransaction(Handle conn, TransactionStatus status) throws Exception {
                try {
                    conn.begin();
                    Account account = accountDao.getById(id);

                    if (account != null) {
                        FieldStoreDao fieldStoreDao = conn.attach(FieldStoreDao.class);
                        List<CustomField> fields = fieldStoreDao.load(account.getId().toString(), account.getObjectName());

                        account.getFields().clear();
                        if (fields != null) {
                            for (CustomField field : fields) {
                                account.getFields().setValue(field.getName(), field.getValue());
                            }
                        }

                        TagStoreDao tagStoreDao = conn.attach(TagStoreDao.class);
                        List<Tag> tags = tagStoreDao.load(account.getId().toString(), account.getObjectName());
                        account.clearTags();

                        if (tags != null) {
                            account.addTags(tags);
                        }
                    }

                    return account;
                } catch (Throwable t) {
                    return null;
                }
            }
        });
    }

    @Override
    public List<Account> get() {
        return accountDao.get();
    }

    @Override
    public void test() {
        accountDao.test();
    }

    @Override
    public void save(final Account account) {
        final String accountId = account.getId().toString();
        final String objectType = DefaultAccount.OBJECT_TYPE;

        dbi.inTransaction(new TransactionCallback<Void>() {
            @Override
            public Void inTransaction(Handle conn, TransactionStatus status) throws Exception {
                try {
                    conn.begin();

                    AccountDao accountDao = conn.attach(AccountDao.class);
                    Account currentAccount = accountDao.getById(accountId);
                    accountDao.save(account);

                    FieldStore fieldStore = account.getFields();
                    FieldStoreDao fieldStoreDao = conn.attach(FieldStoreDao.class);
                    fieldStoreDao.save(accountId, objectType, fieldStore.getEntityList());

                    TagStoreDao tagStoreDao = conn.attach(TagStoreDao.class);
                    tagStoreDao.save(accountId, objectType, account.getTagList());

                    if (currentAccount == null) {
                        AccountCreationNotification creationEvent = new AccountCreationEventDefault(account);
                        eventBus.post(creationEvent);
                    } else {
                        AccountChangeNotification changeEvent = new AccountChangeNotificationDefault(account.getId(), currentAccount, account);
                        if (changeEvent.hasChanges()) {
                            eventBus.post(changeEvent);
                        }
                    }

                    conn.commit();
                } catch (Exception e) {
                    conn.rollback();
                    throw e;
                }

                return null;
            }
        });
    }
}