You can create a custom data handler for your editor for loading initial values into the editor controls and saving the values entered by the user to the PCD. A data handler, which extends PCMDataHandler, implements loadData() and saveData().
The loadData() method is called when the editor is launched, when the editor is refreshed (that is, when the administrator clicks Refresh) and when the editor manually requests a reload of data by calling setLoadDataRequired() on the editor context.
The saveData() is called when the editor framework tries to save the editor's data, for example, when the user clicks Save in the editor toolbar.
If you do not create a data handler for your editor, the default data handler is called. The default handler does not load any data and only saves changes made to the property editor.
Create a new class that extends PCMDataHandler.
Implement loadData(), in which you initialize the data model by setting properties in the editor context. The context is passed into the method as an IEditorContext object.
The following is the method's signature:
Syntax
public void loadData( IEditorContext context, IPrincipal principal, PPLogger logger) throws EditorDataException { }
In the editor context, set the value for properties that later will be saved to the PCD for the currently edited PCD object, as in the following example:
Syntax
context.setProperty("payment.type", "creditCard");
Implement saveData(), in which you save properties from the editor context into the PCD. The following is the method's signature:
Syntax
public void saveData( IEditorContext context, IPrincipal principal, PPLogger logger) throws EditorDataException, EditorResourceException { }
In saveData(), do the following:
Save Data from the Editor: The data entered by the user in your editor is stored in the data model in the editor context. Save this data to the PCD, that is, save each item in the data model to a specific attribute of the PCD object that is being edited.
The following is an example of looking up the current PCD object and saving an object attribute:
Syntax
//Get the object ID for the currently edited PCD object. Map parametersMap = context.getInitialParameters(); String objId = (String)parametersMap.get("objectID"); //Set environment variables for the PCD lookup. Hashtable env = new Hashtable(); env.put(Context.SECURITY_PRINCIPAL, principal); env.put(Constants.APPLY_ASPECT_TO_CONTEXTS, Constants.APPLY_ASPECT_TO_CONTEXTS); env.put(Constants.REQUESTED_ASPECT, PcmConstants.ASPECT_ADMINISTRATION); //Perform the lookup, returning an IAdminBase object. InitialContext iCtx; IAdminBase adminBase = null; try { iCtx = new InitialContext(env); adminBase = (IAdminBase)iCtx.lookup(objId); } catch (NamingException ne) { } //Get the object's IAttributeSet interface. IAttributeSet attrSet = (IAttributeSet) adminBase.getImplementation(IAdminBase.ATTRIBUTE_SET); //Set the attribute. attrSet.putAttribute("myProperty", "200"); //Save the changes. try { attrSet.save(); } catch (ValidationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
Save Changes Made in the Property Editor: Call saveData() on the data handler's super class, as follows:
Syntax
super.saveData(context, principal, logger);
This saves any changes made to the object via the property editor.
If you want the data handler to be available to editors in other portal applications (PARs), implement the IService interface.
From the data handler's getModifiedPropertyObject() method, you can retrieve the value of any property that is displayed in the property editor, as in the following example:
Syntax
IProperty prop = this.getModifiedPropertyObject(context).getProperty( "com.sap.portal.pcm.Description"); String description = prop.getValue();