/*
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at http://www.opensource.org/licenses/cddl1.php
 * or http://www.opensource.org/licenses/cddl1.txt.
 *
 * When distributing Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://www.opensource.org/licenses/cddl1.php.
 * If applicable, add the following below the CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * The Original Software is de.dev.eth0_obroker_war_0.1. The Initial Developer of the Original
 * Software is Alexander Muthmann <amuthmann at dev-eth0.de>.
 */
package de.dev.eth0.obroker.services.dao.impl;

import de.dev.eth0.obroker.entities.account.User;
import de.dev.eth0.obroker.exceptions.DAOException;
import de.dev.eth0.obroker.services.dao.PersistanceManagerFactory;
import de.dev.eth0.obroker.services.dao.UserDAO;
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jdo.*;

/**
 *
 * @author Alexander Muthmann <amuthmann at dev-eth0.de>
 * @version 03/2010
 */
public class UserDAOImpl implements UserDAO {

    private static Logger logger = Logger.getLogger("UserDAO");

    public void addUser(User _user) throws DAOException {
        PersistenceManager pm = PersistanceManagerFactory.get().getPersistenceManager();
        try {
            pm.makePersistent(_user);
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Saving User failed: ", e);
            throw new DAOException(e.getMessage());
        } finally {
            pm.close();
        }
    }

    public void deleteUser(User _user) throws DAOException {
        PersistenceManager pm = PersistanceManagerFactory.get().getPersistenceManager();
        pm.deletePersistent(_user);
        pm.close();
    }

    public User getUserByLogin(String _login) throws DAOException {
        PersistenceManager pm = PersistanceManagerFactory.get().getPersistenceManager();
        User o = pm.getObjectById(User.class, _login);
        pm.close();
        return o;
    }

    public void updateUser(final User _update) throws DAOException {
        User u = getUserByLogin(_update.getLogin());
        Transaction transaction = null;
        PersistenceManager pManager = null;
        try {
            // Obtain a Persistence Manager from the Factory Object
            pManager = PersistanceManagerFactory.get().getPersistenceManager();
            // Retrieve a Transaction object from the Persistence Manager
            transaction = pManager.currentTransaction();
            // Begin a transaction
            transaction.begin();

            //Copy all fields via reflection
            Class clazz = User.class;
            Field[] fields = clazz.getDeclaredFields();
            for (Field f : fields) {
                f.set(u, f.get(_update));
            }

            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            logger.log(Level.SEVERE, "Updating User failed: ", e);
            throw DAOException.updateError;
        } finally {
            pManager.close();
        }
    }
}


