package com.kylietech.oaj.web.actions.impexp;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import net.javalib.util.i18n.GetText;
import net.javalib.util.vl.ValueListInfo;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import com.kylietech.oaj.common.util.*;
import com.kylietech.oaj.extras.service.*;
import com.kylietech.oaj.impexp.*;
import com.kylietech.oaj.impexp.ImpExpData;
import com.kylietech.oaj.server.domain.party.address.PartyAddress;
import com.kylietech.oaj.server.logic.party.*;
import com.kylietech.oaj.web.editor.GenericIdTypeEditor;
import com.kylietech.oaj.web.mvc.OAFormEdit;
/**
* @spring.bean
* name = "/setup.import.import"
* @spring.property name="commandClass" value="com.kylietech.oaj.impexp.ExtrasImpExpData"
* @spring.property name="formView" value="/importer/import"
* @spring.property name="successView" value="setup.import.success"
* @spring.property name="validator" ref="extrasDataImportValidator"
*/
public class ImportDataPage extends OAFormEdit {
public String getTitle(HttpServletRequest request, Object command) {
return GetText.get("Import Purchase Orders");
}
protected void onBind(HttpServletRequest request, Object command,
BindException errors) throws Exception {
ImpExpData iData = (ImpExpData)command;
if (isSaving(request)) {
MultipartHttpServletRequest req = (MultipartHttpServletRequest)request;
Object entries[] = req.getFileMap().values().toArray();
for (int i=0; i < entries.length; i++) {
iData.setMultipartFile((CommonsMultipartFile)entries[i]);
break;
}
}
super.onBind(request, command, errors);
}
protected void onSave(Object command, HttpServletRequest request) {
DataImportService manager = (DataImportService)OAServiceUtil.getBean("DataImportManager");
manager.doImport((ImpExpData)command);
}
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.registerCustomEditor(byte[].class, "file", new ByteArrayMultipartFileEditor());
binder.registerCustomEditor(PartyAddress.class, null, new GenericIdTypeEditor(PartyAddress.class));
}
protected void getModel(Map model, HttpServletRequest request, Object command)
throws Exception {
super.getModel(model, request, command);
ExtrasImpExpData data = (ExtrasImpExpData)command;
ValueListInfo vl = new ValueListInfo();
vl.addFilterNamedParam("name","like", "Walca SA%");
vl.setLimit(1);
List saList = OADomainUtil.getAll(PartyAddress.class, vl);
if (saList.size() > 0)
data.setPoPlacedBy((PartyAddress)saList.get(0));
vl = new ValueListInfo();
vl.addFilterNamedParam("name","like", "Walca FE%");
vl.setLimit(1);
List feList = OADomainUtil.getAll(PartyAddress.class, vl);
if (feList.size() > 0)
data.setPoTakenBy((PartyAddress)feList.get(0));
model.put("_internalOrgAddressList", PartyAddressUtil.getOrgInternalAddresses());
model.put("_supplierAddressList", PartyAddressUtil.getSupplierAddresses());
}
}