/**
  * @see
  *     org.openmrs.messagesource.MutableMessageSource#removePresentation(org.openmrs.messagesource.PresentationMessage)
  */
 public void removePresentation(PresentationMessage message) {
   File propertyFile = findPropertiesFileFor(message.getCode());
   if (propertyFile != null) {
     Properties props = new Properties();
     try {
       OpenmrsUtil.loadProperties(props, propertyFile);
       props.remove(message.getCode());
       OpenmrsUtil.storeProperties(props, propertyFile, PROPERTIES_FILE_COMMENT);
     } catch (Exception e) {
       log.error("Error generated", e);
     }
   }
 }
 /**
  * @see
  *     org.openmrs.messagesource.MutableMessageSource#addPresentation(org.openmrs.messagesource.PresentationMessage)
  */
 public void addPresentation(PresentationMessage message) {
   File propertyFile = findPropertiesFileFor(message.getCode());
   if (propertyFile != null) {
     Properties props = new Properties();
     try {
       OpenmrsUtil.loadProperties(props, propertyFile);
       props.setProperty(message.getCode(), message.getMessage());
       OpenmrsUtil.storeProperties(props, propertyFile, "OpenMRS Application Messages");
     } catch (Exception e) {
       log.error("Error generated", e);
     }
   }
 }
  /** @see org.openmrs.messagesource.MutableMessageSource#merge(MutableMessageSource, boolean) */
  public void merge(MutableMessageSource fromSource, boolean overwrite) {

    // collect all existing properties
    Collection<File> propertiesFiles = findPropertiesFiles();
    Map<Locale, List<File>> localeToFilesMap = new HashMap<Locale, List<File>>();
    Map<File, Properties> fileToPropertiesMap = new HashMap<File, Properties>();

    for (File propertiesFile : propertiesFiles) {
      Properties props = new Properties();
      Locale propsLocale = parseLocaleFrom(propertiesFile.getName());
      List<File> propList = localeToFilesMap.get(propsLocale);
      if (propList == null) {
        propList = new ArrayList<File>();
        localeToFilesMap.put(propsLocale, propList);
      }
      propList.add(propertiesFile);

      try {
        OpenmrsUtil.loadProperties(props, propertiesFile);
        fileToPropertiesMap.put(propertiesFile, props);
      } catch (Exception e) {
        // skip over errors in loading a single file
        log.error("Unable to load properties from file: " + propertiesFile.getAbsolutePath(), e);
      }
    }

    // merge in the new properties
    for (PresentationMessage message : fromSource.getPresentations()) {
      Locale messageLocale = message.getLocale();

      List<File> filelist = localeToFilesMap.get(messageLocale);
      if (filelist != null) {
        Properties propertyDestination = null;
        boolean propExists = false;
        for (File propertiesFile : filelist) {
          Properties possibleDestination = fileToPropertiesMap.get(propertiesFile);

          if (possibleDestination.containsKey(message.getCode())) {
            propertyDestination = possibleDestination;
            propExists = true;
            break;
          } else if (propertyDestination == null) {
            propertyDestination = possibleDestination;
          }
        }
        if ((propExists && overwrite) || !propExists) {
          propertyDestination.put(message.getCode(), message.getMessage());
        }

      } else {
        // no properties files for this locale, create one
        File newPropertiesFile =
            new File(basenames[0] + "_" + messageLocale.toString() + ".properties");
        Properties newProperties = new Properties();
        fileToPropertiesMap.put(newPropertiesFile, newProperties);
        newProperties.put(message.getCode(), message.getMessage());
        List<File> newFilelist = new ArrayList<File>();
        newFilelist.add(newPropertiesFile);
        localeToFilesMap.put(messageLocale, newFilelist);
      }

      message.getCode();
    }
  }