package com.smartgwt.sample.server.listener;

import javax.servlet.http.HttpServletRequest;

import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DSResponse;
import com.isomorphic.datasource.DataSource;
import com.isomorphic.log.Logger;

public class SupplyItemUpload {
	public DSResponse add(DSRequest request, HttpServletRequest servletRequest) throws Exception {
		Logger log = new Logger("com.isomorphic." + SupplyItemUpload.class.getName());
		log.info("SupplyItemUpload DMI called");

		String itemName = request.getValues().get("itemName").toString();
		if (itemName != null && (itemName.startsWith("A") || itemName.startsWith("T")))
			return new DSResponse().setFailure("\"A\" or \"T\" as 1st letter in an item name (\"" + itemName + "\") is not allowed.");

		DSRequest supplyItemAddRequest = new DSRequest("supplyItem", DataSource.OP_ADD, request.getRPCManager());
		supplyItemAddRequest.setValues(request.getValues());
		DSResponse supplyItemAddResponse = supplyItemAddRequest.execute();

//		DSResponse returnResponse = new DSResponse(request.getDataSource(), supplyItemAddResponse.getStatus());
//		if (supplyItemAddResponse.statusIsError())
//			returnResponse.setErrors(supplyItemAddResponse.getErrors());
//		else if (supplyItemAddResponse.statusIsSuccess())
//			returnResponse.addRelatedUpdate(supplyItemAddResponse);

		return supplyItemAddResponse;
	}
};