4 Replies Latest reply: Apr 21, 2017 5:52 AM by Raina RSS

DFC code to edit properties file in repository

Raina

Hello!

 

This is something new which I'm trying out while making a job self contained, I'm trying to update a property file which is saved in the repository.

 

I'll be able to load the document by following:

 

IDfDocument doc = (IDfDocument) session.getObjectByPath( filePath + "/" + fileName);

  if(doc==null)
  System.out.println("Unable to load "+ fileName + " properties file");
  else{
  prop = new Properties();
  prop.load(doc.getContent());
  }

 

Now, I want to update the property which can be carried out by

 

prop.setProperty("key", "value");

 

But how can I check out that properties file and check back in (if possible as same version) after updating the property value?

 

Any help would be great!

  • 1. Re: DFC code to edit properties file in repository
    Luca Rocci

    Hello!

    after you've updated the properties you can save the object in a ByteArrayOutputStream and then save it to the repository in the same object (overwriting or creating a new version).

    Simple solution (overwrite) no control
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Save properties in ByteArrayOutputStream 
    prop.store(baos , "Your comment here");
    // Set content as ByteArrayOutputStream 
    doc.setContent(baos);
    // Save (overwrite content)
    doc.save(); 
    

     

    Advanced Solution (versioning) single access control
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Save properties in ByteArrayOutputStream 
    prop.store(baos , "Your comment here");
    if (!doc.isCheckedOut()) {
     // checkout
     doc.checkout();
     // Set content as ByteArrayOutputStream 
     doc.setContent(baos);
    // checkin as next minor version
     IDfVersionPolicy vp = doc.getVersionPolicy();
     IDfId id = doc.checkin(false, vp.getNextMinorLabel() + ",CURRENT"); 
    }
    

     

    I suggest you use this in a weighted mode, to avoid excessive loads of content server.

     

    Best regards,

    Luca

  • 2. Re: DFC code to edit properties file in repository
    Raina

    Thank you, Luca! that was simple!

     

    By the way, what is the "weighted mode" which you had mentioned, I'm sorry, not familiar with it.

  • 3. Re: DFC code to edit properties file in repository
    Luca Rocci

    I'm happy to have been useful to you.

     

    There is no "weighted mode" to activate on the content server.

    I apologize for my English, I explained to myself badly. Use configuration files in the repository only in the contexts where it really is required. Generally a configuration file can reside safely on the filesystem.

     

    My warning was on the topic in general, I'm sure you've already assessed your specific case.

    Greetings

  • 4. Re: DFC code to edit properties file in repository
    Raina

    Basically to make a job self contained, we need all these dependencies loaded from repositories; as such I'm converting a job into module which will load all these files which are currently in the file system.