/*
 * This is part of the Ktunaxa referral system.
 *
 * Copyright 2011 Ktunaxa Nation Council, http://www.ktunaxa.org/, Canada.
 */

package org.ktunaxa.referral.server.command;

import org.geomajas.command.Command;
import org.geomajas.command.CommandDispatcher;
import org.geomajas.command.dto.SearchFeatureRequest;
import org.geomajas.command.dto.SearchFeatureResponse;
import org.geomajas.global.ExceptionCode;
import org.geomajas.global.GeomajasConstant;
import org.geomajas.global.GeomajasException;
import org.geomajas.layer.feature.Feature;
import org.geomajas.security.SecurityContext;
import org.ktunaxa.referral.client.referral.ReferralUtil;
import org.ktunaxa.referral.server.command.dto.GetReferralRequest;
import org.ktunaxa.referral.server.command.dto.GetReferralResponse;
import org.ktunaxa.referral.server.service.KtunaxaConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Command to get details of a referral.
 * 
 * @author Jan De Moerloose
 */
@Component
public class GetReferralCommand implements Command<GetReferralRequest, GetReferralResponse> {

	@Autowired
	private CommandDispatcher commandDispatcher;

	@Autowired
	private SecurityContext securityContext;

	public GetReferralResponse getEmptyCommandResponse() {
		return new GetReferralResponse();
	}

	public void execute(GetReferralRequest request, GetReferralResponse response) throws Exception {
		String taskId = request.getReferralId();
		if (null == taskId) {
			throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "taskId");
		}

		// Add the referral to the response:
		response.setReferral(getReferral(request.getReferralId()));
	}

	/**
	 * Get the referral feature object for a feature id.
	 *
	 * @param referralId referral id
	 * @return referral feature
	 */
	public Feature getReferral(String referralId) {
		if (null != referralId) {
			SearchFeatureRequest searchFeatureRequest = new SearchFeatureRequest();
			searchFeatureRequest.setBooleanOperator("AND");
			searchFeatureRequest.setCriteria(ReferralUtil.createCriteria(referralId));
			searchFeatureRequest.setCrs(KtunaxaConstant.MAP_CRS);
			searchFeatureRequest.setLayerId(KtunaxaConstant.REFERRAL_SERVER_LAYER_ID);
			searchFeatureRequest.setMax(1);
			searchFeatureRequest.setFeatureIncludes(GeomajasConstant.FEATURE_INCLUDE_ALL);
			SearchFeatureResponse searchFeatureResponse = (SearchFeatureResponse) commandDispatcher.execute(
					SearchFeatureRequest.COMMAND, searchFeatureRequest, securityContext.getToken(), null);
			if (searchFeatureResponse.getFeatures().length > 0) {
				return searchFeatureResponse.getFeatures()[0];
			}
		}
		return null;
	}

}

@@ -257,16 +257,61 @@
 atcher;%0A
+import org.geomajas.command.CommandResponse;%0A
 import o
@@ -878,32 +878,89 @@
 tunaxaConstant;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.sprin
@@ -1243,16 +1243,96 @@
 nse%3E %7B%0A%0A
+%09private final Logger log = LoggerFactory.getLogger(GetReferralCommand.class);%0A%0A
 %09@Autowi
@@ -1640,20 +1640,24 @@
 %09String 
-task
+referral
 Id = req
@@ -1692,20 +1692,24 @@
 null == 
-task
+referral
 Id) %7B%0A%09%09
@@ -1775,12 +1775,16 @@
 G, %22
-task
+referral
 Id%22)
@@ -1866,35 +1866,24 @@
 erral(re
-quest.getRe
 ferralId
 ()));%0A%09%7D
@@ -1874,18 +1874,16 @@
 ferralId
-()
 ));%0A%09%7D%0A%0A
@@ -2553,77 +2553,41 @@
 %0A%09%09%09
-SearchFeatureResponse searchFeatureResponse = (SearchFeatureResponse)
+CommandResponse commandResponse =
  com
@@ -2702,16 +2702,167 @@
  null);%0A
+%09%09%09if (commandResponse instanceof SearchFeatureResponse) %7B%0A%09%09%09%09SearchFeatureResponse searchFeatureResponse = (SearchFeatureResponse) commandResponse;%0A%09
 %09%09%09if (s
@@ -2906,24 +2906,25 @@
 ngth %3E 0) %7B%0A
+%09
 %09%09%09%09return s
@@ -2962,16 +2962,141 @@
 s()%5B0%5D;%0A
+%09%09%09%09%7D%0A%09%09%09%7D else %7B%0A%09%09%09%09log.error(%22Search referral failed %7B%7D %7B%7D%22, commandResponse.isError(), commandResponse.getExceptions());%0A
 %09%09%09%7D%0A%09%09%7D

make more resilient to exceptions when running command