/** Save the ProductionSite to DB. */
  protected void saveForm() {
    Log.d(TAG, "Save form.");
    boolean savedOK = false;
    if (updating) {
      getOptionalFields();
      savedOK = productionSiteDB.saveProductionSite(site);
      Log.d(TAG, "Updated the ProductionSite " + site.toString() + ": " + savedOK);
    } else { // save
      savedOK = verifyForm();
      Log.d(TAG, "Verified the form: " + savedOK);
      if (savedOK) {
        createProductionSiteFromForm();
        Log.d(TAG, "Has created site from form: " + site.get_id() + ", " + site.toString());
        savedOK = productionSiteDB.saveProductionSite(site);
      }
    }

    if (savedOK) {
      Intent intent = new Intent(this, MainActivity.class);
      intent.putExtra(MainActivity.EXTRA_SITE_UPDATED, site.getTitle());
      startActivity(intent);
    } else {
      String text = getString(R.string.toast_could_not_save) + " " + site.getTitle();
      Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
  }
  /** "Constructor." */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_production_site);

    findViews();
    setupClickListeners();

    productionSiteDB = new ProductionSiteDB(this);
    productionSiteDB.open();

    // find out if it as new ProductionSite or update of existing.
    if (getIntent().hasExtra(EXTRA_PRODUCTION_SITE)) {
      setupUpdate();
    }
    // try to set focus, no effect if updating
    etPpnr.requestFocus();
    // disable direct entry of coordinates
    Utils.disableEntry(etCoord);

    // TODO Enable location buttons if we get the services working
    if (hasLocationService) {
      ibHere.setEnabled(true);
      ibMap.setEnabled(true);
    }
  }
  /** Perform the actual deletion of the production site. */
  protected void deleteProductionSite() {
    int rowsAffected = productionSiteDB.deleteProductionSite(site);
    Log.d(TAG, "deleted nr of rows: " + rowsAffected);

    // back to main
    Intent intent = new Intent(this, MainActivity.class);
    ;
    startActivity(intent);
  }
  /** Fill fields with intent-extras, disable input on ProductionSiteNr. */
  private void setupUpdate() {
    updating = true;
    Intent intent = getIntent();

    String siteNrStr = intent.getStringExtra(EXTRA_PRODUCTION_SITE);
    ProductionSiteNr siteNr = new ProductionSiteNr(siteNrStr);

    if (siteNr != null) {
      site = productionSiteDB.getProductionSite(siteNr);
    }

    if (site == null) {
      String text = getString(R.string.toast_could_not_fetch) + " " + siteNrStr;
      Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
      return;
    }

    Log.d(TAG, "site id: " + site.get_id());
    etOrg.setText(site.getSiteNr().getOrg());
    etPpnr.setText(site.getSiteNr().getPpnr());
    // disable input
    Utils.disableEntry(etOrg);
    Utils.disableEntry(etPpnr);

    if (site.hasName()) etName.setText(site.getName());
    if (site.hasAddress()) etAddress.setText(site.getAddress());
    if (site.hasPostnr()) etPostnr.setText(site.getPostnr());
    if (site.hasPostaddress()) etPostaddress.setText(site.getPostaddress());
    if (site.hasCoordinates()) etCoord.setText(site.getCoordinates());
    if (site.hasImageUriStr()) {
      imageUri = Uri.parse(site.getImageUriStr());
      Log.d(TAG, "loaded URI: " + imageUri);
      setThumbnail();
    } else {
      Log.d(TAG, "no loaded URI");
    }
  }
 @Override
 protected void onPause() {
   productionSiteDB.close();
   super.onPause();
 }
 @Override
 protected void onResume() {
   productionSiteDB.open();
   super.onResume();
 }