/*
 *
 *    OPEN-XCHANGE legal information
 *
 *    All intellectual property rights in the Software are protected by
 *    international copyright laws.
 *
 *
 *    In some countries OX, OX Open-Xchange, open xchange and OXtender
 *    as well as the corresponding Logos OX Open-Xchange and OX are registered
 *    trademarks of the Open-Xchange, Inc. group of companies.
 *    The use of the Logos is not covered by the GNU General Public License.
 *    Instead, you are allowed to use these Logos according to the terms and
 *    conditions of the Creative Commons License, Version 2.5, Attribution,
 *    Non-commercial, ShareAlike, and the interpretation of the term
 *    Non-commercial applicable to the aforementioned license is published
 *    on the web site http://www.open-xchange.com/EN/legal/index.html.
 *
 *    Please make sure that third-party modules and libraries are used
 *    according to their respective licenses.
 *
 *    Any modifications to this package must retain all copyright notices
 *    of the original copyright holder(s) for the original code used.
 *
 *    After any such modifications, the original and derivative code shall remain
 *    under the copyright of the copyright holder(s) and/or original author(s)per
 *    the Attribution and Assignment Agreement that can be located at
 *    http://www.open-xchange.com/EN/developer/. The contributing author shall be
 *    given Attribution for the derivative code and a license granting use.
 *
 *     Copyright (C) 2004-2011 Open-Xchange, Inc.
 *     Mail: info@open-xchange.com
 *
 *
 *     This program is free software; you can redistribute it and/or modify it
 *     under the terms of the GNU General Public License, Version 2 as published
 *     by the Free Software Foundation.
 *
 *     This program is distributed in the hope that it will be useful, but
 *     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *     or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *     for more details.
 *
 *     You should have received a copy of the GNU General Public License along
 *     with this program; if not, write to the Free Software Foundation, Inc., 59
 *     Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

package com.openexchange.groupware.importexport.importers;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.openexchange.api.OXPermissionException;
import com.openexchange.api2.ContactSQLInterface;
import com.openexchange.api2.OXException;
import com.openexchange.database.DBPoolingException;
import com.openexchange.groupware.AbstractOXException.Category;
import com.openexchange.groupware.contact.ContactInterface;
import com.openexchange.groupware.contact.ContactInterfaceDiscoveryService;
import com.openexchange.groupware.contact.OverridingContactInterface;
import com.openexchange.groupware.contact.helpers.ContactField;
import com.openexchange.groupware.container.Contact;
import com.openexchange.groupware.container.FolderObject;
import com.openexchange.groupware.importexport.AbstractImporter;
import com.openexchange.groupware.importexport.Format;
import com.openexchange.groupware.importexport.ImportExportExceptionCodes;
import com.openexchange.groupware.importexport.ImportResult;
import com.openexchange.groupware.importexport.exceptions.ImportExportException;
import com.openexchange.groupware.userconfiguration.UserConfigurationStorage;
import com.openexchange.server.impl.EffectivePermission;
import com.openexchange.server.services.ServerServiceRegistry;
import com.openexchange.tools.oxfolder.OXFolderAccess;
import com.openexchange.tools.session.ServerSession;
import com.openexchange.tools.stream.UnsynchronizedByteArrayInputStream;
import com.openexchange.tools.versit.VersitDefinition;
import com.openexchange.tools.versit.VersitException;
import com.openexchange.tools.versit.VersitObject;
import com.openexchange.tools.versit.converter.ConverterException;
import com.openexchange.tools.versit.converter.OXContainerConverter;
import com.openexchange.tools.versit.filetokenizer.VCardFileToken;
import com.openexchange.tools.versit.filetokenizer.VCardTokenizer;

/**
 * This importer translates VCards into contacts for the OX.
 *
 * @see OXContainerConverter - if you have a problem with the content of the parsed ICAL file
 * @see ContactSQLInterface - if you cannot enter the parsed content as contact into the database
 *
 * @author <a href="mailto:sebastian.kauss@open-xchange.com">Sebastian Kauss</a>
 * @author <a href="mailto:tobias.prinz@open-xchange.com">Tobias 'Tierlieb' Prinz</a> (minor: changes to new interface)
 */
public class VCardImporter extends AbstractImporter {

    private static final Log LOG = LogFactory.getLog(VCardImporter.class);

    public boolean canImport(final ServerSession session, final Format format, final List<String> folders, final Map<String, String[]> optionalParams) throws ImportExportException {
        if (!format.equals(Format.VCARD)) {
            return false;
        }
        if (!UserConfigurationStorage.getInstance().getUserConfigurationSafe(session.getUserId(), session.getContext()).hasContact()) {
            throw ImportExportExceptionCodes.CONTACTS_DISABLED.create(new OXPermissionException(
                OXPermissionException.Code.NoPermissionForModul,
                "Contacts"));
        }
        final OXFolderAccess folderAccess = new OXFolderAccess(session.getContext());
        final Iterator<String> iterator = folders.iterator();
        while (iterator.hasNext()) {
            final String folder = iterator.next();

            int folderId = 0;
            try {
                folderId = Integer.parseInt(folder);
            } catch (final NumberFormatException e) {
                throw ImportExportExceptionCodes.NUMBER_FAILED.create(e, folder);
            }

            FolderObject fo;
            try {
                fo = folderAccess.getFolderObject(folderId);
            } catch (final OXException e) {
                return false;
            }

            // check format of folder
            if (fo.getModule() == FolderObject.CONTACT) {
                if (!UserConfigurationStorage.getInstance().getUserConfigurationSafe(session.getUserId(),
                        session.getContext()).hasContact()) {
                    return false;
                }
            } else {
                return false;
            }
            // check read access to folder
            EffectivePermission perm;
            try {
                perm = fo.getEffectiveUserPermission(session.getUserId(), UserConfigurationStorage.getInstance()
                        .getUserConfigurationSafe(session.getUserId(), session.getContext()));
            } catch (final DBPoolingException e) {
                throw ImportExportExceptionCodes.NO_DATABASE_CONNECTION.create(e);
            } catch (final SQLException e) {
                throw ImportExportExceptionCodes.SQL_PROBLEM.create(e, e.getMessage());
            }

            if (!perm.canCreateObjects()) {
                return false;
            }
        }

        return true;
    }

    public List<ImportResult> importData(final ServerSession session, final Format format, final InputStream is,
            final List<String> folders, final Map<String, String[]> optionalParams) throws ImportExportException {

        int contactFolderId = -1;
        final OXFolderAccess folderAccess = new OXFolderAccess(session.getContext());
        final Iterator<String> iterator = folders.iterator();
        while (iterator.hasNext()) {
            final String folder = iterator.next();

            final int folderId = Integer.parseInt(folder);
            FolderObject fo;
            try {
                fo = folderAccess.getFolderObject(folderId);
            } catch (final OXException e) {
                throw ImportExportExceptionCodes.LOADING_FOLDER_FAILED.create(folder);
            }

            if (fo.getModule() == FolderObject.CONTACT) {
                contactFolderId = folderId;
                break;
            }
        }

        OXContainerConverter oxContainerConverter = null;

        final List<ImportResult> list = new ArrayList<ImportResult>();

        try {
            boolean canOverrideInCaseOfTruncation = false;
            oxContainerConverter = new OXContainerConverter(session);
            final VCardTokenizer tokenizer = new VCardTokenizer(is);
            final List<VCardFileToken> chunks = tokenizer.split();
            if (chunks.isEmpty()) {
                throw ImportExportExceptionCodes.NO_VCARD_FOUND.create();
            }
            for (final VCardFileToken chunk : chunks) {
                final VersitDefinition def = chunk.getVersitDefinition();
                final ImportResult importResult = new ImportResult();

                if (def == null) {
                    // could not find appropriate parser for this part of the vcard file
                    LOG.error("Could not recognize format of the following VCard data: " + Arrays.toString(chunk.getContent()));
                    importResult.setDate(new Date(System.currentTimeMillis()));
                    importResult.setException(ImportExportExceptionCodes.UNKNOWN_VCARD_FORMAT.create(chunk.getContent()));
                } else {
                    final VersitDefinition.Reader versitReader = def.getReader(new UnsynchronizedByteArrayInputStream(
                            chunk.getContent()), "UTF-8");
                    try {
                        final VersitObject versitObject = def.parse(versitReader);

                        importResult.setFolder(String.valueOf(contactFolderId));

                        final Contact contactObj = oxContainerConverter.convertContact(versitObject);
                        contactObj.setParentFolderID(contactFolderId);
                        importResult.setDate(new Date());
                        try {
                            final ContactInterface contactInterface = ServerServiceRegistry.getInstance().getService(
                                ContactInterfaceDiscoveryService.class).newContactInterface(contactObj.getParentFolderID(), session);
                            if(contactInterface instanceof OverridingContactInterface){
                                ((OverridingContactInterface) contactInterface).forceInsertContactObject(contactObj);
                                canOverrideInCaseOfTruncation = true;
                            } else {
                                contactInterface.insertContactObject(contactObj);
                            }
                        } catch (OXException oxEx) {
                            if (Category.USER_INPUT.equals(oxEx.getCategory())) {
                                LOG.debug(oxEx.getMessage(), oxEx);
                            } else {
                                LOG.error(oxEx.getMessage(), oxEx);
                            }
                            if(oxEx.getCategory() != Category.TRUNCATED
                                || (oxEx.getCategory() == Category.TRUNCATED && !canOverrideInCaseOfTruncation)){
                                importResult.setException(oxEx);
                                LOG.debug("cannot import contact object", oxEx);
                            }
                        }
                        importResult.setObjectId(String.valueOf(contactObj.getObjectID()));
                        importResult.setDate(contactObj.getLastModified());
                    } catch (final VersitException e) {
                        LOG.error("cannot parse contact object", e);
                        importResult.setException(ImportExportExceptionCodes.VCARD_PARSING_PROBLEM.create(e, e.getMessage()));
                    } catch (final ConverterException e) {
                        LOG.error("cannot convert contact object", e);
                        importResult.setException(ImportExportExceptionCodes.VCARD_CONVERSION_PROBLEM.create(e, e.getMessage()));
                    }
                }
                list.add(importResult);
            }
        } catch (final UnsupportedEncodingException e) {
            LOG.fatal(e.getMessage(), e);
            throw ImportExportExceptionCodes.UTF8_ENCODE_FAILED.create(e);
        } catch (final IOException e) {
            LOG.error(e.getMessage(), e);
            throw ImportExportExceptionCodes.VCARD_PARSING_PROBLEM.create(e, e.getMessage());
        } catch (final ConverterException e) {
            LOG.error(e.getMessage(), e);
            throw ImportExportExceptionCodes.VCARD_CONVERSION_PROBLEM.create(e, e.getMessage());
        } finally {
            if (oxContainerConverter != null) {
                oxContainerConverter.close();
            }
        }

        return list;
    }

    @Override
    protected String getNameForFieldInTruncationError(final int id, final OXException unused) {
        final ContactField field = ContactField.getByValue(id);
        if (field == null) {
            return String.valueOf(id);
        }
        return field.getReadableName();
    }

}
