joniles / mpxj

Primary repository for MPXJ library
http://www.mpxj.org/
GNU Lesser General Public License v2.1
248 stars 104 forks source link

Adding metadata or custom fields to a file #715

Closed kirlev closed 3 months ago

kirlev commented 4 months ago

Hi, can the option to add metadata to XER/PMXML files be added?

The use case in mind is when using MPXJ to extract XER and PMXML files from the Primavera P6 DB and sending the files to another machine/service to be processed by MPXJ, it would be beneficial to add metadata to the files so when they are processed we could have context of their extraction.

I could not find a way to do that with MPXJ, can it be added? or maybe it is possible and I missed it?

joniles commented 4 months ago

I think there are a potentially a few options.

If you can provide access to a central service within your application's ecosystem, you could record a checksum calculated from the file, or use the UUID/GUID associated with the a project in the file as a key to look up any metadata you need to store. That way the metadata is available within your ecosystem without the need to store anything additional in the file. (I'm assuming a SaaS application here).

If this type of approach doesn't work and you do need to keep metadata in the file, you could either create a notebook entry at the project level for the information you want to capture (with a topic specific to your application), or you could (ab)use the "Project Web Site URL" project attribute to encode the information you need as a URL. Both of these would require MPXJ to be updated to provide access to project notes and the project URL attribute, but these are simple changes which I'd be happy to put in place for you.

Let me know what you think!

kirlev commented 3 months ago

The Notebook topic idea is excellent, it would be great if you added support for setting and getting a project's notebook entry!

Thanks

joniles commented 3 months ago

So... I've added methods to ProjectProperties to allow you to retrieve notes as text, and also as a NotesObject giving you access to the structure of the note.

For P6 schedules represented as PMXML files, this works just fine: you can read/write notes via the ProjectProperties class and they will appear as project notes in P6.

XER files are a little different. As you may have already found, the top level WBS entry in an XER file typically represents the project, so an XER file actually ends up containing two sets of project information: the project record and the top level WBS entry. MPXJ currently does not attempt to interpret this - it just passes along this WBS entry to you without trying to "flatten" it into the Project Properties.

Currently this means that if you want to read/write Project notes, you have to work with the Notes/NotesObject attributes of the top level WBS entry (in MPXJ, the first task). For the moment you'll need to have some logic in your code to determine which file type you are working with, then add the notes you need to either ProjectProperties or the first Task - hope that all makes sense!

I'm considering changing this behaviour for the next major MPXJ release so that MPXJ "flattens" this top level WBS entry and captures the data it contains in ProjectProperties, and handles mapping to/from this representation when reading or writing XER files. I will probably look to add this functionality later in the MPXJ 13.x releases and use a feature flag to make it optional, then I can make it the default behaviour for MPXJ 14 when users should be expecting breaking changes.

I'll put together some sample code now to illustrate adding a topic and some notes for that topic to the project.

joniles commented 3 months ago

Here's some sample code you'll be able to work with once the new version is avavilable:

https://github.com/joniles/mpxj-java-samples/pull/6/files

This takes an XER or PMXML file and adds or updates a project note using a specific notes topic.