in objectModel/Java/objectmodel/src/main/java/com/microsoft/commondatamodel/objectmodel/persistence/modeljson/ManifestPersistence.java [269:439]
public static CompletableFuture<Model> toData(
final CdmManifestDefinition instance,
final ResolveOptions resOpt,
final CopyOptions options) {
return CompletableFuture.supplyAsync(() -> {
final Model result = new Model();
result.setName(instance.getManifestName());
result.setDescription(instance.getExplanation());
result.setModifiedTime(instance.getLastFileModifiedTime());
result.setLastChildFileModifiedTime(instance.getLastChildFileModifiedTime());
result.setLastFileStatusCheckTime(instance.getLastFileStatusCheckTime());
result.setDocumentVersion(instance.getDocumentVersion());
final TraitToPropertyMap t2pm = new TraitToPropertyMap(instance);
final CdmTraitReference isHiddenTrait = t2pm.fetchTraitReference("is.hidden");
if (isHiddenTrait != null) {
result.setHidden(true);
}
final CdmTraitReference applicationTrait = t2pm.fetchTraitReference("is.managedBy");
if (applicationTrait != null) {
result.setApplication(
applicationTrait.getArguments().get(0).getValue().toString());
}
final CdmTraitReference versionTrait = t2pm.fetchTraitReference("is.modelConversion.modelVersion");
if (versionTrait != null) {
result.setVersion(versionTrait.getArguments().get(0).getValue().toString());
} else {
// Version property is required. If it doesn't exist set default.
result.setVersion("1.0");
}
final CdmTraitReference cultureTrait = t2pm.fetchTraitReference("is.partition.culture");
if (cultureTrait != null) {
result.setCulture(cultureTrait.getArguments().get(0).getValue().toString());
}
final Map<String, String> referenceEntityLocations = new LinkedHashMap<>();
final Map<String, String> referenceModels = new LinkedHashMap<>();
final CdmTraitReference referenceModelsTrait = t2pm.fetchTraitReference("is.modelConversion.referenceModelMap");
if (referenceModelsTrait != null) {
final JsonNode refModels = JMapper.MAP
.valueToTree(referenceModelsTrait.getArguments().getAllItems().get(0).getValue());
refModels.forEach(referenceModel -> {
final JsonNode referenceModelId = referenceModel.get("id");
final String referenceModelIdAsString = referenceModelId.asText();
final JsonNode referenceModelLocation = referenceModel.get("location");
final String referenceModelLocationAsString = referenceModelLocation.asText();
referenceModels.put(referenceModelIdAsString, referenceModelLocationAsString);
referenceEntityLocations.put(referenceModelLocationAsString, referenceModelIdAsString);
});
}
Utils.processTraitsAndAnnotationsToData(instance.getCtx(), result, instance.getExhibitsTraits());
if (instance.getEntities() != null && instance.getEntities().getCount() > 0) {
final List<CompletableFuture<Void>> promises = new ArrayList<>();
final CopyOnWriteArrayList<Entity> obtainedEntities = new CopyOnWriteArrayList<>();
for (final CdmEntityDeclarationDefinition entity : instance.getEntities()) {
final CompletableFuture<Void> createdPromise = CompletableFuture.runAsync(() -> {
Entity element = null;
if ((entity.getObjectType() == CdmObjectType.LocalEntityDeclarationDef)) {
element = LocalEntityDeclarationPersistence.toData(
entity,
instance,
resOpt,
options
).join();
} else if ((entity.getObjectType() == CdmObjectType.ReferencedEntityDeclarationDef)) {
element = ReferencedEntityDeclarationPersistence.toData(
entity,
resOpt,
options
).join();
String location = instance.getCtx()
.getCorpus()
.getStorage()
.corpusPathToAdapterPath(
entity.getEntityPath()
);
if (StringUtils.isNullOrEmpty(location)) {
Logger.error(instance.getCtx(), TAG, "toData", instance.getAtCorpusPath(), CdmLogCode.ErrPersistModelJsonInvalidEntityPath);
element = null;
}
if (element instanceof ReferenceEntity) {
// path separator can differ depending on the adapter, cover the case where path uses '/' or '\'
final ReferenceEntity referenceEntity = (ReferenceEntity) element;
int lastSlashLocation = location.lastIndexOf("/") > location.lastIndexOf("\\") ? location.lastIndexOf("/") : location.lastIndexOf("\\");
if (lastSlashLocation > 0) {
location = location.substring(0, lastSlashLocation);
}
if (referenceEntity.getModelId() != null) {
final String savedLocation = referenceModels.get(referenceEntity.getModelId());
if (savedLocation != null && !Objects.equals(savedLocation, location)) {
Logger.error(instance.getCtx(), TAG, "toData", instance.getAtCorpusPath(), CdmLogCode.ErrPersistModelJsonModelIdDuplication);
element = null;
} else if (savedLocation == null) {
referenceModels.put(referenceEntity.getModelId(), location);
referenceEntityLocations.put(location, referenceEntity.getModelId());
}
} else if (referenceEntityLocations.containsKey(location)) {
referenceEntity.setModelId(referenceEntityLocations.get(location));
} else {
referenceEntity.setModelId(UUID.randomUUID().toString());
referenceModels.put(referenceEntity.getModelId(), location);
referenceEntityLocations.put(location, referenceEntity.getModelId());
}
}
}
if (element != null) {
obtainedEntities.add(element);
} else {
Logger.error(instance.getCtx(), TAG, "toData", instance.getAtCorpusPath(), CdmLogCode.ErrPersistModelJsonEntityDeclarationConversionError, entity.getEntityName());
}
});
try {
// TODO: Currently function is synchronous. Remove next line to turn it asynchronous.
// Currently some functions called are not thread safe.
createdPromise.get();
promises.add(createdPromise);
} catch (InterruptedException | ExecutionException e) {
Logger.error(instance.getCtx(), TAG, "toData", instance.getAtCorpusPath(), CdmLogCode.ErrPersistModelJsonEntityDeclarationConversionFailure, entity.getEntityName(), e.getMessage());
}
}
for (final CompletableFuture<Void> promise : promises) {
promise.join();
} // TODO-BQ: 2019-09-05 Refactor.
result.setEntities(new ArrayList<>(obtainedEntities));
}
if (!referenceModels.isEmpty()) {
result.setReferenceModels(new ArrayList<>());
for (final Map.Entry<String, String> referenceModel : referenceModels.entrySet()) {
final ReferenceModel newReferenceModel = new ReferenceModel();
newReferenceModel.setId(referenceModel.getKey());
newReferenceModel.setLocation(referenceModel.getValue());
result.getReferenceModels().add(newReferenceModel);
}
}
if (null != instance.getRelationships() && instance.getRelationships().getCount() > 0) {
result.setRelationships(new ArrayList<>());
instance.getRelationships().forEach(cdmRelationship -> {
final SingleKeyRelationship relationship = RelationshipPersistence
.toData(cdmRelationship, resOpt, options).join();
if (null != relationship) {
result.getRelationships().add(relationship);
}
});
}
if (instance.getImports() != null && instance.getImports().getCount() > 0) {
result.setImports(new ArrayList<>());
instance.getImports().forEach(element ->
result.getImports().add(ImportPersistence.toData(element, resOpt, options)));
}
return result;
});
}