package cz.csu.sdmx.soap.endpoints;

import cz.csu.sdmx.dao.LogicConditionsDao;
import cz.csu.sdmx.soap.GetConditionByIdRequest;
import cz.csu.sdmx.soap.GetConditionByIdResponse;
import cz.csu.sdmx.soap.GetConditionsByDsdUrnRequest;
import cz.csu.sdmx.soap.GetConditionsResponse;
import cz.csu.sdmx.validation.model.LogicCondition;
import cz.csu.sdmx.validation.model.dsds.Dsd;
import java.util.List;
import javax.xml.bind.JAXBElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class ConditionEndpoint extends BaseEndpoint{
        
        @Autowired
        private LogicConditionsDao conditionsDao;

	@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getConditionByIdRequest")
	@ResponsePayload
	public GetConditionByIdResponse getConditionById(@RequestPayload GetConditionByIdRequest request) {
            GetConditionByIdResponse response = factory.createGetConditionByIdResponse();
            
            response.setCondition(conditionsDao.find(request.getConditionID()));
            
            return response;
	}

	@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getConditionsByDsdUrnRequest")
	@ResponsePayload
	public JAXBElement<GetConditionsResponse> getConditionByUrn(@RequestPayload GetConditionsByDsdUrnRequest request) {
            GetConditionsResponse response = factory.createGetConditionsResponse();
            
            //if urn is supplied, get conditions from DB
            String urn = request.getDsdUrn();
            if(urn != null){
                List<LogicCondition> conditions = conditionsDao.getByDsdUrn(urn);
                if (conditions != null) {
                    response.getConditions().addAll(conditions);                
                }
            }
            
            return factory.createGetConditionsByDsdUrnResponse(response);
	}

	@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getAllConditionsRequest")
	@ResponsePayload
	public JAXBElement<GetConditionsResponse> getAllConditions() {
            GetConditionsResponse response = factory.createGetConditionsResponse();
            
            response.getConditions().addAll(conditionsDao.list());
            
            return factory.createGetAllConditionsResponse(response);
	}

}
