PortletStateContextsMapping portletStateContexts = getContexts(session);
PortletStateContextMapping pscm = portletStateContexts.createPortletStateContext(UUID.randomUUID().toString());
portletStateContexts.getPortletStateContexts().add(pscm);
PortletStateMapping psm = pscm.getState();
psm.setPortletID(portletId);
psm.setProperties(propertyMap);
// get the key
final String key = pscm.getPersistentKey();
// then save