Tuesday, January 18, 2011

Easy operate with database settings profile in java

The easy way to get settings from profile document from any part of your java progam.

Call "readProperties" method at the beginig of your program to read properties from profile.

The next example read one item called "isProductionDatabase". You could extend this class to read all items.

public class DatabaseProperties{
 private DatabaseProperties(){}
 
    private static String ProfileDatabaseSettings = 
         "settings.database";
    private final static String ITEM_IS_PRODUCTION_DATABASE = 
         "isProductionDatabase";
    
    private static Properties databaseProperties;

    private static void readPropertyFromDocument(
      Document profile, 
      String propertyName) throws NotesException{
       String property = profile.getItemValueString(propertyName);
     databaseProperties.setProperty(propertyName, property);   
    }
    
    public static void readProperties(Database database) throws NotesException{
     databaseProperties = new Properties();
     Document profile = 
      database.getProfileDocument(ProfileDatabaseSettings, null);
     
     readPropertyFromDocument(profile, ITEM_IS_PRODUCTION_DATABASE);
    }

    public static void readProperties(
      Database database, 
      String profileName) throws NotesException{
       if(profileName != null){
      ProfileDatabaseSettings = profileName;
     }
       readProperties(database);
    }
 
    public static String get(String key){
     return databaseProperties.getProperty(key); 
    }
    
    public static boolean isProductionDatabases(){
        String isProductionDatabase = 
         databaseProperties.getProperty(ITEM_IS_PRODUCTION_DATABASE);
        return "1".equals(isProductionDatabase);
    }
}

No comments: