package com.capgemini.reports.r26;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.framework.teamcenter.object.types.SnecmaObjectTypes;
import com.capgemini.framework.teamcenter.policies.PolicyLocation;
import com.capgemini.reports.common.ServletMessageHelper;
import com.capgemini.reports.common.obtaindata.AbstractObtainDataFromModelObject;
import com.capgemini.reports.exceptions.InvalidUserInputException;
import com.capgemini.reports.exceptions.UserInputErrorEnum;
import com.teamcenter.soa.client.model.ModelObject;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;

import static com.capgemini.framework.teamcenter.TeamcenterObjectProperties.ITEM_ID;

/** This class receives demands form ajax client to initialize value for Component. */
@Slf4j
@WebServlet("/report_r26/obtainDataR26")
public class ObtainDataServletR26 extends AbstractObtainDataFromModelObject {

    private static final long serialVersionUID = 6615381966462711567L;

    private static final String SUCCES_MESSAGE = "Donnée obtenue avec succès pour le rapport R26.";

    @Override
    public JSONObject obtainDataFromModelObject(final ModelObject modelObject, final TcSession session) throws InvalidUserInputException{
        log.info("Server received a request to obtain data for R26 report.");

        if(!SnecmaObjectTypes.isPart(modelObject.getTypeObject().getName()) && !SnecmaObjectTypes.isPartRevision(modelObject.getTypeObject().getName())){
            throw new InvalidUserInputException(UserInputErrorEnum.THE_SELECTED_OBJECT_IS_NOT_PERMITTED_ON_THIS_REPORT);
        }

        // Start JSON
        JSONObject json = ServletMessageHelper.successMessage(SUCCES_MESSAGE);

        String itemId = modelObject.getPropertyObject(ITEM_ID).getStringValue();

        // send data
        json.put("ObjectId", itemId);

        return json;
    }

    @Override
    protected PolicyLocation obtainLocation() {
        return PolicyLocation.make("R26_policy_full", "/xml/policies/r26report/R26_policy_full.xml");
    }
}
