/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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 org.wso2.carbon.device.mgt.mobile.impl.ios.enrollment;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.ios.core.bean.ChallengeTokenBean;
import org.wso2.carbon.device.mgt.ios.core.exception.IOSEnrollmentException;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.IOSDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.impl.IOSEnrollmentTokenDAO;

public class IOSEnrollmentTokenServiceImpl implements IOSEnrollmentTokenService {

    private static final Log log = LogFactory.getLog(IOSEnrollmentTokenServiceImpl.class);
    private IOSEnrollmentTokenDAO iosEnrollmentTokenDAO;

    public IOSEnrollmentTokenServiceImpl() {
        iosEnrollmentTokenDAO = IOSDAOFactory.getIOSEnrollmentTokenDAO();
    }

    @Override
    public void saveDeviceEnrollmentToken(ChallengeTokenBean challengeTokenBean) throws IOSEnrollmentException {
        try {
            IOSDAOFactory.beginTransaction();
            iosEnrollmentTokenDAO.add(challengeTokenBean);
            IOSDAOFactory.commitTransaction();
        } catch (MobileDeviceManagementDAOException e) {
            try {
                IOSDAOFactory.rollbackTransaction();
            } catch (MobileDeviceManagementDAOException ex) {
                log.warn("Error occurred while roll-backing the transaction", ex);
            }
            throw new IOSEnrollmentException("Error occurred while persisting challenge token", e);
        } finally {
            IOSDAOFactory.closeConnection();
        }
    }

    @Override
    public void updateDeviceEnrollmentToken(ChallengeTokenBean challengeTokenBean) throws IOSEnrollmentException {
        if (challengeTokenBean.getChallengeToken() == null) {
            log.warn("Unable to fetch existing entry to update. Challenge token is empty.");
        }
        try {
            IOSDAOFactory.beginTransaction();
            iosEnrollmentTokenDAO.update(challengeTokenBean);
            IOSDAOFactory.commitTransaction();
        } catch (MobileDeviceManagementDAOException e) {
            try {
                IOSDAOFactory.rollbackTransaction();
            } catch (MobileDeviceManagementDAOException ex) {
                log.warn("Error occurred while roll-backing the transaction", ex);
            }
            throw new IOSEnrollmentException("Error occurred while updating challenge token", e);
        } finally {
            IOSDAOFactory.closeConnection();
        }
    }

    @Override
    public ChallengeTokenBean getDeviceEnrollmentToken(String challengeToken) throws IOSEnrollmentException {
        ChallengeTokenBean challengeTokenBean;
        try {
            IOSDAOFactory.beginTransaction();
            challengeTokenBean = iosEnrollmentTokenDAO.get(challengeToken);
            IOSDAOFactory.commitTransaction();
        } catch (MobileDeviceManagementDAOException e) {
            try {
                IOSDAOFactory.rollbackTransaction();
            } catch (MobileDeviceManagementDAOException ex) {
                log.warn("Error occurred while roll-backing the transaction", ex);
            }
            throw new IOSEnrollmentException("Error occurred while fetching the token entry for challengeToken: " +
                    challengeToken, e);
        } finally {
            IOSDAOFactory.closeConnection();
        }
        return challengeTokenBean;
    }

    @Override
    public ChallengeTokenBean getDeviceEnrollmentTokenByDeviceId(String deviceId) throws IOSEnrollmentException {
        ChallengeTokenBean challengeTokenBean;
        try {
            IOSDAOFactory.beginTransaction();
            challengeTokenBean = iosEnrollmentTokenDAO.getByDeviceId(deviceId);
            IOSDAOFactory.commitTransaction();
        } catch (MobileDeviceManagementDAOException e) {
            try {
                IOSDAOFactory.rollbackTransaction();
            } catch (MobileDeviceManagementDAOException ex) {
                log.warn("Error occurred while roll-backing the transaction", ex);
            }
            throw new IOSEnrollmentException("Error occurred while fetching the token entry for deviceId: " +
                    deviceId, e);
        } finally {
            IOSDAOFactory.closeConnection();
        }
        return challengeTokenBean;
    }

    @Override
    public void removeDeviceEnrollmentToken(String deviceId) throws IOSEnrollmentException {
        try {
            IOSDAOFactory.beginTransaction();
            iosEnrollmentTokenDAO.delete(deviceId);
            IOSDAOFactory.commitTransaction();
        } catch (MobileDeviceManagementDAOException e) {
            try {
                IOSDAOFactory.rollbackTransaction();
            } catch (MobileDeviceManagementDAOException ex) {
                log.warn("Error occurred while roll-backing the transaction", ex);
            }
            throw new IOSEnrollmentException("Error occurred while removing the token entry for deviceId: " +
                    deviceId, e);
        } finally {
            IOSDAOFactory.closeConnection();
        }
    }
}
