Q:

How to create a Liferay 7 structure programmatically?

In Liferay 7, how to create a structure from a Java module?
Here is my attempt:
Map<Locale, String> nameMap = new HashMap<Locale, String>();
nameMap.put(Locale.JAPAN, "The name");
Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
descriptionMap.put(Locale.JAPAN, "The description");

DDMForm ddmForm = DDMUtil.getDDMForm("<here goes my real JSON form>");
DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);

DDMStructureLocalServiceUtil.addStructure(
    20156, // userId
    33421, // groupId
    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID, // parentStructureId
    PortalUtil.getPortal().getClassNameId(DDLRecordSet.class), // classNameId
    new Long(CounterLocalServiceUtil.increment()).toString(), // structureKey
    nameMap,
    descriptionMap,
    ddmForm,
    ddmFormLayout,
    StorageType.JSON.toString(),
    0, // type
    new ServiceContext()
);

The structure gets created in the database's DDMStructure table:

Unfortunately, it does not appear in that site's Liferay UI:

How to make it show up?

No error appear in Liferay's log during creation nor when loading the UI.
When I create a structure manually, it shows up correctly.
The solution to this question for Liferay 6, which I tried too, leads to the same problem.
I noticed that when creating a structure manually, 3 rows get added to the ResourcePermission table... when creating a structure in Java should I also create these 3 objects?

A:

It is a classNameId problem. Replacing DDLRecordSet with JournalArticle solves the problem, making the structure show up correctly in Liferay's Structures UI.
Code that works:
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(group.getGroupId());
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);

Map<Locale, String> nameMap = new HashMap<Locale, String>();
nameMap.put(Locale.JAPAN, "The name");
Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
descriptionMap.put(Locale.JAPAN, "The description");

DDMForm ddmForm = null;
try {
    ddmForm = DDMUtil.getDDMForm(json);
} catch (PortalException e) {
    log.error("Exception when parsing structure JSON", e);
}

DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);
long scopeClassNameId = PortalUtil.getPortal().getClassNameId(JournalArticle.class);
long parentStructureId = DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID;
String storageType = StorageType.JSON.toString();
String structureKey = "my structure";

try {
    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.addStructure(
        user.getUserId(), group.getGroupId(), parentStructureId,
        scopeClassNameId, structureKey,
        nameMap, descriptionMap, ddmForm, ddmFormLayout, storageType,
        DDMStructureConstants.TYPE_DEFAULT, serviceContext);
} catch (StructureDuplicateStructureKeyException e) {
    log.info("Skipping creation of structure that already exists");
} catch (PortalException e) {
    log.error("Exception when creating structure: " + structureDefinitionFilePath, e);
}

