package com.armedia.acm.services.notification.service;

/*-
 * #%L
 * ACM Service: Notification
 * %%
 * Copyright (C) 2014 - 2018 ArkCase LLC
 * %%
 * This file is part of the ArkCase software. 
 * 
 * If the software was purchased under a paid ArkCase license, the terms of 
 * the paid license agreement will prevail.  Otherwise, the software is 
 * provided under the following open source license terms:
 * 
 * ArkCase is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *  
 * ArkCase 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ArkCase. If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.ACTION_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.CREATOR_FULL_NAME_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.DATA_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.DESCRIPTION_PARSEABLE;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.MODIFIER_FULL_NAME_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.OWNER_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.PARENT_ID_S;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.PARENT_NAME_T;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.PARENT_NUMBER_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.PARENT_REF_S;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.PARENT_TYPE_S;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.STATE_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.STATUS_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.TITLE_PARSEABLE;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.TITLE_PARSEABLE_LCS;
import static com.armedia.acm.services.search.model.solr.SolrAdditionalPropertiesConstants.TYPE_LCS;

import com.armedia.acm.services.notification.dao.NotificationDao;
import com.armedia.acm.services.notification.model.Notification;
import com.armedia.acm.services.notification.model.NotificationConstants;
import com.armedia.acm.services.search.model.solr.SolrAdvancedSearchDocument;
import com.armedia.acm.services.search.service.AcmObjectToSolrDocTransformer;
import com.armedia.acm.services.users.dao.UserDao;
import com.armedia.acm.services.users.model.AcmUser;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class NotificationToSolrTransformer implements AcmObjectToSolrDocTransformer<Notification>
{
    private final Logger LOG = LogManager.getLogger(getClass());

    private NotificationDao notificationDao;
    private UserDao userDao;
    private NotificationUtils notificationUtils;

    @Override
    public List<Notification> getObjectsModifiedSince(Date lastModified, int start, int pageSize)
    {
        return getNotificationDao().findModifiedSince(lastModified, start, pageSize);
    }

    @Override
    public SolrAdvancedSearchDocument toSolrAdvancedSearch(Notification in)
    {
        LOG.debug("Creating Solr advanced search document for Notification.");

        SolrAdvancedSearchDocument solrDoc = new SolrAdvancedSearchDocument();

        mapRequiredProperties(solrDoc, in.getId(), in.getCreator(), in.getCreated(), in.getModifier(), in.getModified(),
                NotificationConstants.OBJECT_TYPE, null);

        mapAdditionalProperties(in, solrDoc.getAdditionalProperties());

        return solrDoc;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.armedia.acm.services.search.service.AcmObjectToSolrDocTransformer#isAcmObjectTypeSupported(java.lang.Class)
     */
    @Override
    public boolean isAcmObjectTypeSupported(Class acmObjectType)
    {
        return Notification.class.equals(acmObjectType);
    }

    @Override
    public void mapAdditionalProperties(Notification in, Map<String, Object> additionalProperties)
    {
        additionalProperties.put(ACTION_LCS, in.getAction());
        additionalProperties.put(DATA_LCS, in.getData());

        additionalProperties.put(TITLE_PARSEABLE, in.getTitle());
        additionalProperties.put(TITLE_PARSEABLE_LCS, in.getTitle());
        additionalProperties.put(PARENT_ID_S, (in.getParentId() + ""));
        additionalProperties.put(PARENT_TYPE_S, in.getParentType());
        additionalProperties.put(PARENT_NUMBER_LCS, in.getParentName());
        additionalProperties.put(PARENT_NAME_T, in.getParentTitle());
        additionalProperties.put(OWNER_LCS, in.getUser());
        additionalProperties.put(DESCRIPTION_PARSEABLE, in.getNote());
        additionalProperties.put(STATE_LCS, in.getState());
        additionalProperties.put(PARENT_REF_S, in.getParentId() + "-" + in.getParentType());
        additionalProperties.put(STATUS_LCS, in.getStatus());
        additionalProperties.put(TYPE_LCS, in.getType());

        /** Additional properties for full names instead of ID's */
        AcmUser creator = getUserDao().quietFindByUserId(in.getCreator());
        if (creator != null)
        {
            additionalProperties.put(CREATOR_FULL_NAME_LCS, creator.getFirstName() + " " + creator.getLastName());
        }

        AcmUser modifier = getUserDao().quietFindByUserId(in.getModifier());
        if (modifier != null)
        {
            additionalProperties.put(MODIFIER_FULL_NAME_LCS, modifier.getFirstName() + " " + modifier.getLastName());
        }

        Long relatedObjectId = in.getRelatedObjectId();
        String relatedObjectType = in.getRelatedObjectType();
        String relatedObjectNumber = in.getRelatedObjectNumber();
        Date actionDate = in.getActionDate();
        String notificationLink = getNotificationUtils().buildNotificationLink(in.getParentType(), in.getParentId(),
                relatedObjectType, relatedObjectId);
        additionalProperties.put("related_object_id_l", relatedObjectId);
        additionalProperties.put("related_object_type_s", relatedObjectType);
        additionalProperties.put("related_object_number_s", relatedObjectNumber);
        additionalProperties.put("action_date_tdt", actionDate);
        additionalProperties.put("notification_link_s", notificationLink);
        additionalProperties.put("notification_type_lcs", in.getType());
    }

    public NotificationDao getNotificationDao()
    {
        return notificationDao;
    }

    public void setNotificationDao(NotificationDao notificationDao)
    {
        this.notificationDao = notificationDao;
    }

    public UserDao getUserDao()
    {
        return userDao;
    }

    public void setUserDao(UserDao userDao)
    {
        this.userDao = userDao;
    }

    @Override
    public Class<?> getAcmObjectTypeSupported()
    {
        return Notification.class;
    }

    public NotificationUtils getNotificationUtils()
    {
        return notificationUtils;
    }

    public void setNotificationUtils(NotificationUtils notificationUtils)
    {
        this.notificationUtils = notificationUtils;
    }
}
