private void updatePeripherals(
      DeviceConfig deviceConfig, List<MedicalTestType> medicalTestTypeList)
      throws NoSuchPeripheralConfigException, SystemException {

    debug("getting peripherals for deviceConfigId: " + deviceConfig.getDeviceConfigId());
    List<PeripheralConfig> peripherals =
        PeripheralConfigLocalServiceUtil.findByDeviceConfigId(deviceConfig.getDeviceConfigId());
    debug("found " + (peripherals == null ? 0 : peripherals.size()) + " peripherals");
    for (PeripheralConfig peripheral : peripherals) {
      debug("Deleting PeripheralConfig: " + peripheral);
      PeripheralConfigLocalServiceUtil.deletePeripheralConfig(peripheral);
    }
    for (MedicalTestType mtt : medicalTestTypeList) {
      debug("Creating PeripheralConfig for MedicalTestType: " + mtt.getNameCurrentValue());
      PeripheralConfig pc = createPeripheral(mtt.getMedicalTestTypeId());
      pc.setDeviceConfigId(deviceConfig.getDeviceConfigId());
      pc.setMedicalTestTypeId(mtt.getMedicalTestTypeId());
      // flag values for Primum to fill
      pc.setModel(mtt.getNameCurrentValue());
      pc.setPeripheralMAC(PrimumConstantsLocalServiceUtil.getDefaultPeripheralMAC());
      pc.setPeripheralPIN(PrimumConstantsLocalServiceUtil.getDefaultPeripheralPIN());
      pc = PeripheralConfigLocalServiceUtil.addPeripheralConfig(pc);
      debug("PeripheralConfig saved!");
    }
    debug("Peripherals updated!!");
  }
  /*
   * NOTE FOR DEVELOPERS:
   *
   * Never reference this interface directly. Always use {@link com.primum.service.WizardServiceLocalServiceUtil} to access the wizard service local service.
   */
  public boolean updateConfig(
      User creatorUser,
      long deviceConfigId,
      String deviceConfigName,
      long patientId,
      Date startDate,
      Date endDate,
      List<MedicalTestType> medicalTestTypeList,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    debug("dates:" + startDate + " to " + endDate);

    Date now = new Date();

    DeviceConfig deviceConfig = null;
    if (deviceConfigId > 0) {
      debug("finding deviceConfig for deviceConfigId: " + deviceConfigId);
      deviceConfig = DeviceConfigLocalServiceUtil.getDeviceConfig(deviceConfigId);
    } else {
      debug("Creating deviceUser");
      User deviceUser = createDeviceUser(creatorUser, deviceConfigName, serviceContext);
      debug("finding deviceConfig for new User");
      deviceConfig = DeviceConfigLocalServiceUtil.getDeviceConfigByDeviceId(deviceUser.getUserId());
    }

    debug("deviceConfig found: " + deviceConfig);

    if (deviceConfig != null) {
      deviceConfig.setModifiedDate(now);
      deviceConfig.setPatientId(patientId);
      deviceConfig.setCreatorUserId(creatorUser.getUserId());
      deviceConfig = DeviceConfigLocalServiceUtil.updateDeviceConfig(deviceConfig);
      debug("deviceConfig updated: " + deviceConfig);
      updatePeripherals(deviceConfig, medicalTestTypeList);
      CalEvent calEvent =
          createEvent4Patient(
              deviceConfigName, creatorUser, patientId, startDate, endDate, serviceContext);

    } else {
      throw new SystemException("Unable to create a valid deviceConfig");
    }

    _log.info("Patient (#" + patientId + ") config was updated: " + deviceConfig);
    for (MedicalTestType mtt : medicalTestTypeList) {
      _log.info("   new medicalTestType added: " + mtt.getMedicalTestTypeKey());
    }

    return true;
  }