// STEP #1. CREATE A TEMP STORE
// Read the incoming XML file, and create a new/temporary store for the
// need to ensure current store doesn't get overridden
//
MockeyXmlFileConfigurationReader msfr = new MockeyXmlFileConfigurationReader();
IMockeyStorage mockServiceStoreTemporary = msfr.readDefinition(strXMLDefintion);
// STEP #2. PROXY SETTINGS
// If the proxy settings are _empty_, then set the incoming
// proxy settings. Otherwise, call out a merge conflict.
//
ProxyServerModel proxyServerModel = store.getProxy();
if (proxyServerModel.hasSettings()) {
mergeResults.addConflictMsg("Proxy settings NOT set from incoming file.");
} else {
store.setProxy(mockServiceStoreTemporary.getProxy());
mergeResults.addAdditionMsg("Proxy settings set.");
}
// STEP #3. BUILD SERVICE REFERENCES
// Why is this needed?
// We are adding _new_ services into the Store, and that means that the
// store's state is always changing. We need references as a saved
// snapshot list of store state prior to adding new services.
// **********
// I forget why we really need this though...
// **********
List<Service> serviceListFromRefs = new ArrayList<Service>();
for (ServiceRef serviceRef : mockServiceStoreTemporary.getServiceRefs()) {
try {
String mockServiceDefinition = getFileContentAsString(new FileInputStream(serviceRef.getFileName()));
// HACK:
// I tried to find an easier way to use XML ENTITY and let
// Digester
// to the work of slurping up the XML but was unsuccessful.
// Hence, the brute force.
// YYYYY
//
List<Service> tmpList = msfr.readServiceDefinition(mockServiceDefinition);
for (Service tmpService : tmpList) {
serviceListFromRefs.add(tmpService);
}
} catch (SAXParseException spe) {
logger.error("Unable to parse file of name " + serviceRef.getFileName(), spe);
mergeResults.addConflictMsg("File not parseable: " + serviceRef.getFileName());
} catch (FileNotFoundException fnf) {
logger.error("File not found: " + serviceRef.getFileName());
mergeResults.addConflictMsg("File not found: " + serviceRef.getFileName());
}
}
addServicesToStore(mergeResults, serviceListFromRefs, tagArguments);
// STEP #4. MERGE SERVICES AND SCENARIOS
// Since this gets complicated, logic was moved to it's own method.
mergeResults = addServicesToStore(mergeResults, mockServiceStoreTemporary.getServices(), tagArguments);
// STEP #5. UNIVERSAL RESPONSE SETTINGS
// Important: usage of the temporary-store's Scenario reference
// information is used to set the primary in-memory store. The primary
// store has all the information and the TEMP store only needs to pass
// the references, e.g. Service 1, Scenario 2.
if (store.getUniversalErrorScenario() != null
&& mockServiceStoreTemporary.getUniversalErrorScenarioRef() != null) {
mergeResults.addConflictMsg("Universal error message already defined with name '"
+ store.getUniversalErrorScenario().getScenarioName() + "'");
} else if (store.getUniversalErrorScenario() == null
&& mockServiceStoreTemporary.getUniversalErrorScenarioRef() != null) {
store.setUniversalErrorScenarioRef(mockServiceStoreTemporary.getUniversalErrorScenarioRef());
mergeResults.addAdditionMsg("Universal error response defined.");
}
// STEP #6. MERGE SERVICE PLANS
for (ServicePlan servicePlan : mockServiceStoreTemporary.getServicePlans()) {
if (tagArguments != null) {
servicePlan.addTagToList(tagArguments);
}
store.saveOrUpdateServicePlan(servicePlan);
}
// STEP #7. TWIST CONFIGURATION
for (TwistInfo twistInfo : mockServiceStoreTemporary.getTwistInfoList()) {
store.saveOrUpdateTwistInfo(twistInfo);
}
// STEP #8. DEFAULT Service Plan ID
// Only set a default service plan ID from the incoming XML file
// if one is not already set in the current store.
ServicePlan servicePlan = mockServiceStoreTemporary.getServicePlanById(mockServiceStoreTemporary
.getDefaultServicePlanIdAsLong());
if (servicePlan != null && store.getDefaultServicePlanIdAsLong() == null) {
// OK, we have a 'default' service plan from the incoming file AND
// the current store does not. Let's update the current store.
store.setDefaultServicePlanId(mockServiceStoreTemporary.getDefaultServicePlanId());
store.setServicePlan(servicePlan);
}
// Don't forget to set state back to original state.