String importMpanCoreStr = GeneralImport.addField(csvElement,
"Import MPAN Core", values, 17);
String importLlfcCode = null;
Integer importAgreedSupplyCapacity = null;
SupplierContract importSupplierContract = null;
String importSupplierAccount = null;
Mpan existingImportMpan = supplyGeneration.getImportMpan();
if (importMpanCoreStr.equals(GeneralImport.NO_CHANGE)) {
importMpanCoreStr = existingImportMpan == null ? null
: existingImportMpan.getCore().toString();
} else if (importMpanCoreStr.length() == 0) {
importMpanCoreStr = null;
}
if (importMpanCoreStr != null) {
importLlfcCode = GeneralImport.addField(csvElement,
"Import LLFC", values, 18);
if (importLlfcCode.equals(GeneralImport.NO_CHANGE)) {
importLlfcCode = existingImportMpan == null ? null
: existingImportMpan.getLlfc().toString();
}
String importAgreedSupplyCapacityStr = GeneralImport
.addField(csvElement, "Import Agreed Supply Capacity",
values, 19);
if (importAgreedSupplyCapacityStr
.equals(GeneralImport.NO_CHANGE)) {
if (existingImportMpan == null) {
throw new UserException(
"There isn't an existing import MPAN.");
} else {
importAgreedSupplyCapacity = existingImportMpan
.getAgreedSupplyCapacity();
}
} else {
try {
importAgreedSupplyCapacity = Integer
.parseInt(importAgreedSupplyCapacityStr);
} catch (NumberFormatException e) {
throw new UserException(
"The import agreed supply capacity must be an integer. "
+ e.getMessage());
}
}
String importSupplierContractName = GeneralImport.addField(
csvElement, "Import Supplier Contract", values, 20);
if (importSupplierContractName.equals(GeneralImport.NO_CHANGE)) {
if (existingImportMpan == null) {
throw new UserException(
"There isn't an existing import supplier.");
}
importSupplierContract = existingImportMpan
.getSupplierContract();
} else {
importSupplierContract = SupplierContract
.getSupplierContract(importSupplierContractName);
}
importSupplierAccount = GeneralImport.addField(csvElement,
"Import Supplier Account", values, 21);
if (importSupplierAccount.equals(GeneralImport.NO_CHANGE)) {
if (existingImportMpan == null) {
throw new UserException(
"There isn't an existing import supplier account.");
}
importSupplierAccount = existingImportMpan
.getSupplierAccount();
}
}
String exportMpanCoreStr = null;
String exportLlfcCode = null;
SupplierContract exportSupplierContract = null;
String exportSupplierAccount = null;
Integer exportAgreedSupplyCapacity = null;
if (values.length > 22) {
exportMpanCoreStr = GeneralImport.addField(csvElement,
"Eport MPAN Core", values, 22);
Mpan existingExportMpan = supplyGeneration.getExportMpan();
if (exportMpanCoreStr.equals(GeneralImport.NO_CHANGE)) {
exportMpanCoreStr = existingExportMpan == null ? null
: existingExportMpan.getCore().toString();
} else if (exportMpanCoreStr.length() == 0) {
exportMpanCoreStr = null;
}
if (exportMpanCoreStr != null) {
exportLlfcCode = GeneralImport.addField(csvElement,
"Export LLFC", values, 23);
if (exportLlfcCode.equals(GeneralImport.NO_CHANGE)) {
exportLlfcCode = existingExportMpan == null ? null
: existingExportMpan.getLlfc().toString();
}
String exportAgreedSupplyCapacityStr = GeneralImport
.addField(csvElement,
"Export Agreed Supply Capacity", values, 24);
if (exportAgreedSupplyCapacityStr
.equals(GeneralImport.NO_CHANGE)) {
if (existingExportMpan == null) {
throw new UserException(
"There isn't an existing export MPAN.");
} else {
exportAgreedSupplyCapacity = existingExportMpan
.getAgreedSupplyCapacity();
}
} else {
try {
exportAgreedSupplyCapacity = new Integer(
exportAgreedSupplyCapacityStr);
} catch (NumberFormatException e) {
throw new UserException(
"The export supply capacity must be an integer. "
+ e.getMessage());
}
}
String exportSupplierContractName = GeneralImport.addField(
csvElement, "Export Supplier Contract", values, 25);
if (exportSupplierContractName
.equals(GeneralImport.NO_CHANGE)) {
if (existingExportMpan == null) {
throw new UserException(
"There isn't an existing export supplier contract.");
}
exportSupplierContract = existingExportMpan
.getSupplierContract();
} else {
exportSupplierContract = SupplierContract
.getSupplierContract(exportSupplierContractName);
}
exportSupplierAccount = GeneralImport.addField(csvElement,
"Export Supplier Account", values, 26);
if (exportSupplierAccount.equals(GeneralImport.NO_CHANGE)) {
if (existingExportMpan == null) {
throw new UserException(
"There isn't an existing export MPAN.");
}
exportSupplierAccount = existingExportMpan
.getSupplierAccount();
}
}
}
supply.updateGeneration(
supplyGeneration,
startDateStr.equals(GeneralImport.NO_CHANGE) ? supplyGeneration
.getStartDate() : new HhStartDate(startDateStr),
finishDateStr.length() == 0 ? null : (finishDateStr
.equals(GeneralImport.NO_CHANGE) ? supplyGeneration
.getFinishDate() : new HhStartDate(finishDateStr)),
mopContract, mopAccount, hhdcContract, hhdcAccount,
meterSerialNumber, pc, mtcCode, cop, ssc,
importMpanCoreStr, importLlfcCode, importSupplierContract,
importSupplierAccount, importAgreedSupplyCapacity,
exportMpanCoreStr, exportLlfcCode, exportSupplierContract,
exportSupplierAccount, exportAgreedSupplyCapacity);
} else if (action.equals("delete")) {
String mpanCoreStr = GeneralImport.addField(csvElement,
"MPAN Core", values, 0);
MpanCore mpanCore = MpanCore.getMpanCore(mpanCoreStr);
Supply supply = mpanCore.getSupply();
String dateStr = GeneralImport.addField(csvElement, "Date", values,
1);
SupplyGeneration supplyGeneration = supply.getGeneration(dateStr
.length() == 0 ? null : new HhStartDate(dateStr));
if (supplyGeneration == null) {
throw new UserException(
"There isn't a generation at this date.");
}
supply.deleteGeneration(supplyGeneration);
} else if (action.equals("insert")) {
String mpanCoreStr = GeneralImport.addField(csvElement,
"MPAN Core", values, 0);
Supply supply = MpanCore.getMpanCore(mpanCoreStr).getSupply();
String startDateStr = GeneralImport.addField(csvElement,
"Start date", values, 1);
HhStartDate startDate = startDateStr.length() == 0 ? null
: new HhStartDate(startDateStr);
SupplyGeneration existingGeneration = supply
.getGeneration(startDate);
if (existingGeneration == null) {
SupplyGeneration firstGeneration = supply.getGenerationFirst();
if (startDate.before(firstGeneration.getStartDate())) {
existingGeneration = firstGeneration;
}
}
if (existingGeneration == null) {
throw new UserException(
"The start date is after end of the supply.");
}
String siteCode = GeneralImport.addField(csvElement, "Site Code",
values, 2);
Site physicalSite = null;
List<Site> logicalSites = new ArrayList<Site>();
if (siteCode.equals(GeneralImport.NO_CHANGE)) {
for (SiteSupplyGeneration ssgen : existingGeneration
.getSiteSupplyGenerations()) {
if (ssgen.getIsPhysical()) {
physicalSite = ssgen.getSite();
} else {
logicalSites.add(ssgen.getSite());
}
}
} else {
physicalSite = Site.getSite(siteCode);
}
String mopContractName = GeneralImport.addField(csvElement,
"MOP Contract", values, 3);
MopContract mopContract = null;
if (mopContractName.equals(GeneralImport.NO_CHANGE)) {
mopContract = existingGeneration.getMopContract();
} else {
if (mopContractName.length() > 0) {
mopContract = MopContract.getMopContract(mopContractName);
}
}
String mopAccount = GeneralImport.addField(csvElement,
"MOP Account Reference", values, 4);
if (mopAccount.equals(GeneralImport.NO_CHANGE)) {
mopAccount = existingGeneration.getMopAccount();
}
String hhdcContractName = GeneralImport.addField(csvElement,
"HHDC Contract", values, 5);
HhdcContract hhdcContract = null;
if (hhdcContractName.equals(GeneralImport.NO_CHANGE)) {
hhdcContract = existingGeneration.getHhdcContract();
} else {
if (hhdcContractName.length() > 0) {
hhdcContract = HhdcContract
.getHhdcContract(hhdcContractName);
}
}
String hhdcAccount = GeneralImport.addField(csvElement,
"HHDC Account Reference", values, 6);
if (hhdcAccount.equals(GeneralImport.NO_CHANGE)) {
hhdcAccount = existingGeneration.getHhdcAccount();
}
String hasImportKwhStr = GeneralImport.addField(csvElement,
"Has HH import kWh", values, 7);
boolean hasImportKwh = hasImportKwhStr
.equals(GeneralImport.NO_CHANGE) ? existingGeneration
.getChannel(true, true) != null : Boolean
.parseBoolean(hasImportKwhStr);
String hasImportKvarhStr = GeneralImport.addField(csvElement,
"Has HH import kVArh", values, 8);
boolean hasImportKvarh = hasImportKvarhStr
.equals(GeneralImport.NO_CHANGE) ? existingGeneration
.getChannel(true, false) != null : Boolean
.parseBoolean(hasImportKvarhStr);
String hasExportKwhStr = GeneralImport.addField(csvElement,
"Has HH export kWh", values, 9);
boolean hasExportKwh = hasImportKwhStr
.equals(GeneralImport.NO_CHANGE) ? existingGeneration
.getChannel(false, true) != null : Boolean
.parseBoolean(hasExportKwhStr);
String hasExportKvarhStr = GeneralImport.addField(csvElement,
"Has HH export kVArh", values, 10);
boolean hasExportKvarh = hasImportKwhStr
.equals(GeneralImport.NO_CHANGE) ? existingGeneration
.getChannel(false, false) != null : Boolean
.parseBoolean(hasExportKvarhStr);
String meterSerialNumber = GeneralImport.addField(csvElement,
"Meter Serial Number", values, 11);
if (meterSerialNumber.equals(GeneralImport.NO_CHANGE)) {
meterSerialNumber = existingGeneration.getMeterSerialNumber();
}
String pcStr = GeneralImport.addField(csvElement, "Profile Class",
values, 12);
Pc pc = null;
if (pcStr.equals(GeneralImport.NO_CHANGE)) {
pc = existingGeneration.getPc();
} else {
pc = Pc.getPc(pcStr);
}
String mtcCode = GeneralImport.addField(csvElement,
"Meter Timeswitch Class", values, 13);
if (mtcCode.equals(GeneralImport.NO_CHANGE)) {
mtcCode = existingGeneration.getMtc().toString();
}
Cop cop = null;
String copStr = GeneralImport.addField(csvElement, "CoP", values,
14);
if (copStr.equals(GeneralImport.NO_CHANGE)) {
cop = existingGeneration.getCop();
} else {
cop = Cop.getCop(copStr);
}
Ssc ssc = null;
String sscCode = GeneralImport.addField(csvElement,
"Standard Settlement Configuration", values, 15);
if (sscCode.equals(GeneralImport.NO_CHANGE)) {
ssc = existingGeneration.getSsc();
} else if (sscCode.length() > 0) {
ssc = Ssc.getSsc(sscCode);
}
String importMpanCoreStr = GeneralImport.addField(csvElement,
"Import MPAN Core", values, 16);
Mpan existingImportMpan = existingGeneration.getImportMpan();
if (importMpanCoreStr.equals(GeneralImport.NO_CHANGE)) {
if (existingImportMpan == null) {
importMpanCoreStr = "";
} else {
importMpanCoreStr = existingImportMpan.getCore().toString();
}
}
SupplierContract importSupplierContract = null;
String importSupplierAccount = null;
Integer importAgreedSupplyCapacity = null;
String importLlfcCode = null;
String importAgreedSupplyCapacityStr = null;
String importContractSupplierName = null;
if (importMpanCoreStr.length() > 0) {
importLlfcCode = GeneralImport.addField(csvElement,
"Import Line Loss Factor Class", values, 17);
if (importLlfcCode.equals(GeneralImport.NO_CHANGE)) {
if (existingImportMpan == null) {
throw new UserException(
"There ins't an existing import MPAN.");
} else {
importLlfcCode = existingImportMpan.getLlfc()
.toString();
}
}
importAgreedSupplyCapacityStr = GeneralImport
.addField(csvElement, "Import Agreed Supply Capacity",
values, 18);
if (importAgreedSupplyCapacityStr
.equals(GeneralImport.NO_CHANGE)) {
if (existingImportMpan != null) {
importAgreedSupplyCapacity = existingImportMpan
.getAgreedSupplyCapacity();
}
} else {
try {
importAgreedSupplyCapacity = Integer
.parseInt(importAgreedSupplyCapacityStr);
} catch (NumberFormatException e) {
throw new UserException(
"The import agreed supply capacity must be an integer. "
+ e.getMessage());
}
}
importContractSupplierName = GeneralImport.addField(csvElement,
"Import Supplier Contract", values, 19);
if (importContractSupplierName.equals(GeneralImport.NO_CHANGE)) {
if (existingImportMpan != null) {
importSupplierContract = existingImportMpan
.getSupplierContract();
}
} else {
importSupplierContract = SupplierContract
.getSupplierContract(importContractSupplierName);
}
importSupplierAccount = GeneralImport.addField(csvElement,
"Import Supplier Account Reference", values, 20);
if (importSupplierAccount.equals(GeneralImport.NO_CHANGE)) {
if (existingImportMpan == null) {
importSupplierAccount = null;
} else {
importSupplierAccount = existingImportMpan
.getSupplierAccount();
}
}
}
String exportMpanCoreStr = null;
String exportLlfcCode = null;
SupplierContract exportSupplierContract = null;
String exportSupplierAccount = null;
Integer exportAgreedSupplyCapacity = null;
if (values.length > 21) {
Mpan existingExportMpan = existingGeneration.getExportMpan();