String mopAccount = GeneralImport.addField(csvElement,
"MOP Account", values, 5);
if (mopAccount.equals(GeneralImport.NO_CHANGE)) {
mopAccount = supplyGeneration.getMopAccount();
}
HhdcContract hhdcContract = null;
String hhdcContractName = GeneralImport.addField(csvElement,
"HHDC Contract", values, 6);
if (hhdcContractName.equals(GeneralImport.NO_CHANGE)) {
hhdcContract = supplyGeneration.getHhdcContract();
} else {
hhdcContract = HhdcContract.getHhdcContract(hhdcContractName);
}
String hhdcAccount = GeneralImport.addField(csvElement,
"HHDC account", values, 7);
if (hhdcAccount.equals(GeneralImport.NO_CHANGE)) {
hhdcAccount = supplyGeneration.getHhdcAccount();
}
String hasImportKwhStr = GeneralImport.addField(csvElement,
"Has HH import kWh?", values, 8);
boolean hasImportKwh = hasImportKwhStr
.equals(GeneralImport.NO_CHANGE) ? supplyGeneration
.getChannel(true, true) != null : Boolean
.parseBoolean(hasImportKwhStr);
String hasImportKvarhStr = GeneralImport.addField(csvElement,
"Has HH import kVArh?", values, 9);
boolean hasImportKvarh = hasImportKvarhStr
.equals(GeneralImport.NO_CHANGE) ? supplyGeneration
.getChannel(true, false) != null : Boolean
.parseBoolean(hasImportKvarhStr);
String hasExportKwhStr = GeneralImport.addField(csvElement,
"Has HH export kWh?", values, 10);
boolean hasExportKwh = hasExportKwhStr
.equals(GeneralImport.NO_CHANGE) ? supplyGeneration
.getChannel(false, true) != null : Boolean
.parseBoolean(hasExportKwhStr);
String hasExportKvarhStr = GeneralImport.addField(csvElement,
"Has HH export kVArh?", values, 11);
boolean hasExportKvarh = hasExportKvarhStr
.equals(GeneralImport.NO_CHANGE) ? supplyGeneration
.getChannel(false, false) != null : Boolean
.parseBoolean(hasExportKvarhStr);
for (boolean isImport : new boolean[] { true, false }) {
for (boolean isKwh : new boolean[] { true, false }) {
boolean hasChannel;
if (isImport) {
if (isKwh) {
hasChannel = hasImportKwh;
} else {
hasChannel = hasImportKvarh;
}
} else {
if (isKwh) {
hasChannel = hasExportKwh;
} else {
hasChannel = hasExportKvarh;
}
}
if (hasChannel
&& supplyGeneration.getChannel(isImport, isKwh) == null) {
supplyGeneration.insertChannel(isImport, isKwh);
}
if (!hasChannel
&& supplyGeneration.getChannel(isImport, isKwh) != null) {
supplyGeneration.deleteChannel(isImport, isKwh);
}
}
}
String meterSerialNumber = GeneralImport.addField(csvElement,
"Meter Serial Number", values, 12);
if (meterSerialNumber.equals(GeneralImport.NO_CHANGE)) {
meterSerialNumber = supplyGeneration.getMeterSerialNumber();
}
String pcCode = GeneralImport.addField(csvElement, "Profile Class",
values, 13);
Pc pc = null;
if (pcCode.equals(GeneralImport.NO_CHANGE)) {
pc = supplyGeneration.getPc();
} else {
pc = Pc.getPc(pcCode);
}
String mtcCode = GeneralImport.addField(csvElement,
"Meter Timeswitch Class", values, 14);
if (mtcCode.equals(GeneralImport.NO_CHANGE)) {
mtcCode = supplyGeneration.getMtc().toString();
}
String copCode = GeneralImport.addField(csvElement, "CoP", values,
15);
Cop cop = null;
if (pcCode.equals(GeneralImport.NO_CHANGE)) {
cop = supplyGeneration.getCop();
} else {
cop = Cop.getCop(copCode);
}
Ssc ssc = null;
String sscCode = GeneralImport.addField(csvElement, "SSC", values,
16);
if (sscCode.equals(GeneralImport.NO_CHANGE)) {
ssc = supplyGeneration.getSsc();
} else {
ssc = sscCode.length() == 0 ? null : Ssc.getSsc(sscCode);
}
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