/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* Kind, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using PortCMIS.Binding.Browser.Json;
using PortCMIS.Constants;
using PortCMIS.Data;
using PortCMIS.Data.Extensions;
using PortCMIS.Enums;
using PortCMIS.Exceptions;
using System;
using System.Collections.Generic;
using System.Numerics;
namespace PortCMIS.Binding.Browser
{
internal static class JsonConverter
{
public enum PropertyMode
{
Object,
Query,
Change
}
///
/// Converts a repository info object.
///
internal static JsonObject Convert(IRepositoryInfo repositoryInfo, string repositoryUrl,
string rootUrl, bool addExtendedDatetimeExtensionFeature)
{
if (repositoryInfo == null)
{
return null;
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonRepInfoId, repositoryInfo.Id);
result.Add(BrowserConstants.JsonRepInfoName, repositoryInfo.Name);
result.Add(BrowserConstants.JsonRepInfoDescription, repositoryInfo.Description);
result.Add(BrowserConstants.JsonRepInfoVendor, repositoryInfo.VendorName);
result.Add(BrowserConstants.JsonRepInfoProduct, repositoryInfo.ProductName);
result.Add(BrowserConstants.JsonRepInfoProductVersion, repositoryInfo.ProductVersion);
result.Add(BrowserConstants.JsonRepInfoRootFolderId, repositoryInfo.RootFolderId);
result.Add(BrowserConstants.JsonRepInfoCapabilities, Convert(repositoryInfo.Capabilities));
SetIfNotNull(BrowserConstants.JsonRepInfoAclCapabilities, Convert(repositoryInfo.AclCapabilities), result);
result.Add(BrowserConstants.JsonRepInfoChangeLogToken, repositoryInfo.LatestChangeLogToken);
result.Add(BrowserConstants.JsonRepInfoCmisVersionSupported, repositoryInfo.CmisVersionSupported);
SetIfNotNull(BrowserConstants.JsonRepInfoThinClientUri, repositoryInfo.ThinClientUri, result);
SetIfNotNull(BrowserConstants.JsonRepInfoChangesIncomplete, repositoryInfo.ChangesIncomplete, result);
JsonArray changesOnType = new JsonArray();
if (repositoryInfo.ChangesOnType != null)
{
foreach (BaseTypeId? type in repositoryInfo.ChangesOnType)
{
if (type != null)
{
changesOnType.Add(GetJsonStringValue(type.GetCmisValue()));
}
}
}
result.Add(BrowserConstants.JsonRepInfoChangesOnType, changesOnType);
SetIfNotNull(BrowserConstants.JsonRepInfoPrincipalIdAnonymous, repositoryInfo.PrincipalIdAnonymous, result);
SetIfNotNull(BrowserConstants.JsonRepInfoPrincipalIdAnyone, repositoryInfo.PrincipalIdAnyone, result);
if (IsNotEmpty(repositoryInfo.ExtensionFeatures))
{
JsonArray extendedFeatures = new JsonArray();
foreach (ExtensionFeature feature in repositoryInfo.ExtensionFeatures)
{
extendedFeatures.Add(Convert(feature));
}
result.Add(BrowserConstants.JsonRepInfoExtendedFeatures, extendedFeatures);
}
if (addExtendedDatetimeExtensionFeature)
{
object extendedFeatures;
if (!result.TryGetValue(BrowserConstants.JsonRepInfoExtendedFeatures, out extendedFeatures))
{
extendedFeatures = new JsonArray();
result.Add(BrowserConstants.JsonRepInfoExtendedFeatures, extendedFeatures);
}
if (extendedFeatures is JsonArray)
{
((JsonArray)extendedFeatures).Add(Convert(ExtensionFeatures.ExtendedDatetimeFormat));
}
}
result.Add(BrowserConstants.JsonRepInfoRepositoryUrl, repositoryUrl);
result.Add(BrowserConstants.JsonRepInfoRootFolderUrl, rootUrl);
ConvertExtension(repositoryInfo, result);
return result;
}
private static JsonObject Convert(IExtensionFeature feature)
{
if (feature == null)
{
return null;
}
JsonObject jsonFeature = new JsonObject();
SetIfNotNull(BrowserConstants.JsonFeatureId, feature.Id, jsonFeature);
SetIfNotNull(BrowserConstants.JsonFeatureUrl, feature.Url, jsonFeature);
SetIfNotNull(BrowserConstants.JsonFeatureCommonName, feature.CommonName, jsonFeature);
SetIfNotNull(BrowserConstants.JsonFeatureVersionLabel, feature.VersionLabel, jsonFeature);
SetIfNotNull(BrowserConstants.JsonFeatureDescription, feature.Description, jsonFeature);
if (IsNotEmpty(feature.FeatureData))
{
JsonObject data = new JsonObject();
foreach (KeyValuePair kv in feature.FeatureData)
{
data.Add(kv.Key, kv.Value);
}
jsonFeature.Add(BrowserConstants.JsonFeatureData, data);
}
ConvertExtension(feature, jsonFeature);
return jsonFeature;
}
///
/// Converts a capabilities object.
///
internal static JsonObject Convert(IRepositoryCapabilities capabilities)
{
if (capabilities == null)
{
return null;
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonCapContentStreamUpdatability, GetJsonEnumValue(capabilities.ContentStreamUpdatesCapability));
result.Add(BrowserConstants.JsonCapChanges, GetJsonEnumValue(capabilities.ChangesCapability));
result.Add(BrowserConstants.JsonCapRenditions, GetJsonEnumValue(capabilities.RenditionsCapability));
result.Add(BrowserConstants.JsonCapGetDescendants, capabilities.IsGetDescendantsSupported);
result.Add(BrowserConstants.JsonCapGetFolderTree, capabilities.IsGetFolderTreeSupported);
result.Add(BrowserConstants.JsonCapMultifiling, capabilities.IsMultifilingSupported);
result.Add(BrowserConstants.JsonCapUnfiling, capabilities.IsUnfilingSupported);
result.Add(BrowserConstants.JsonCapVersionSpecificFiling, capabilities.IsVersionSpecificFilingSupported);
result.Add(BrowserConstants.JsonCapPwcSearchable, capabilities.IsPwcSearchableSupported);
result.Add(BrowserConstants.JsonCapPwcUpdatable, capabilities.IsPwcUpdatableSupported);
result.Add(BrowserConstants.JsonCapAllVersionsSerachable, capabilities.IsAllVersionsSearchableSupported);
result.Add(BrowserConstants.JsonCapOrderBy, GetJsonEnumValue(capabilities.OrderByCapability));
result.Add(BrowserConstants.JsonCapQuery, GetJsonEnumValue(capabilities.QueryCapability));
result.Add(BrowserConstants.JsonCapJoin, GetJsonEnumValue(capabilities.JoinCapability));
result.Add(BrowserConstants.JsonCapAcl, GetJsonEnumValue(capabilities.AclCapability));
if (capabilities.CreatablePropertyTypes != null)
{
ICreatablePropertyTypes creatablePropertyTypes = capabilities.CreatablePropertyTypes;
JsonObject creatablePropertyTypesJson = new JsonObject();
if (creatablePropertyTypes.CanCreate != null)
{
JsonArray canCreate = new JsonArray();
foreach (PropertyType propType in creatablePropertyTypes.CanCreate)
{
canCreate.Add(propType.GetCmisValue());
}
creatablePropertyTypesJson.Add(BrowserConstants.JsonCapCreatablePropertyTypesCanCreate, canCreate);
}
ConvertExtension(creatablePropertyTypes, creatablePropertyTypesJson);
result.Add(BrowserConstants.JsonCapCreatablePropertyTypes, creatablePropertyTypesJson);
}
if (capabilities.NewTypeSettableAttributes != null)
{
INewTypeSettableAttributes newTypeSettableAttributes = capabilities.NewTypeSettableAttributes;
JsonObject newTypeSettableAttributesJson = new JsonObject();
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesId,
newTypeSettableAttributes.CanSetId);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesLocalName,
newTypeSettableAttributes.CanSetLocalName);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesLocalNameSpace,
newTypeSettableAttributes.CanSetLocalNamespace);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesDisplayName,
newTypeSettableAttributes.CanSetDisplayName);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesQueryName,
newTypeSettableAttributes.CanSetQueryName);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesDescription,
newTypeSettableAttributes.CanSetDescription);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesCreatable,
newTypeSettableAttributes.CanSetCreatable);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesFileable,
newTypeSettableAttributes.CanSetFileable);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesQueryable,
newTypeSettableAttributes.CanSetQueryable);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesFulltextIndexed,
newTypeSettableAttributes.CanSetFulltextIndexed);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesIncludeInSupertypeQuery,
newTypeSettableAttributes.CanSetIncludedInSupertypeQuery);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesControlablePolicy,
newTypeSettableAttributes.CanSetControllablePolicy);
newTypeSettableAttributesJson.Add(BrowserConstants.JsonCapNewTypeSettableAttributesControlableAcl,
newTypeSettableAttributes.CanSetControllableAcl);
ConvertExtension(newTypeSettableAttributes, newTypeSettableAttributesJson);
result.Add(BrowserConstants.JsonCapNewTypeSettableAttributes, newTypeSettableAttributesJson);
}
ConvertExtension(capabilities, result);
return result;
}
///
/// Converts an Acl capabilities object.
///
internal static JsonObject Convert(IAclCapabilities capabilities)
{
if (capabilities == null)
{
return null;
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonAclCapSupportedPermissions, GetJsonEnumValue(capabilities.SupportedPermissions));
result.Add(BrowserConstants.JsonAclCapAclPropagation, GetJsonEnumValue(capabilities.AclPropagation));
// permissions
if (capabilities.Permissions != null)
{
JsonArray permissions = new JsonArray();
foreach (PermissionDefinition permDef in capabilities.Permissions)
{
JsonObject permission = new JsonObject();
permission.Add(BrowserConstants.JsonAclCapPermissionPermission, permDef.Id);
permission.Add(BrowserConstants.JsonAclCapPermissionDescription, permDef.Description);
permissions.Add(permission);
}
result.Add(BrowserConstants.JsonAclCapPermissions, permissions);
}
// permission mapping
if (capabilities.PermissionMapping != null)
{
JsonArray permissionMapping = new JsonArray();
foreach (PermissionMapping permMap in capabilities.PermissionMapping.Values)
{
JsonArray mappingPermissions = new JsonArray();
if (permMap.Permissions != null)
{
foreach (string p in permMap.Permissions)
{
mappingPermissions.Add(p);
}
}
JsonObject mapping = new JsonObject();
mapping.Add(BrowserConstants.JsonAclCapMappingKey, permMap.Key);
mapping.Add(BrowserConstants.JsonAclCapMappingPermission, mappingPermissions);
permissionMapping.Add(mapping);
}
result.Add(BrowserConstants.JsonAclCapPermissionMapping, permissionMapping);
}
ConvertExtension(capabilities, result);
return result;
}
internal static IRepositoryInfo ConvertRepositoryInfo(JsonObject json)
{
if (json == null)
{
return null;
}
RepositoryInfoBrowserBinding result = new RepositoryInfoBrowserBinding();
result.Id = GetString(json, BrowserConstants.JsonRepInfoId);
result.Name = GetString(json, BrowserConstants.JsonRepInfoName);
result.Description = GetString(json, BrowserConstants.JsonRepInfoDescription);
result.VendorName = GetString(json, BrowserConstants.JsonRepInfoVendor);
result.ProductName = GetString(json, BrowserConstants.JsonRepInfoProduct);
result.ProductVersion = GetString(json, BrowserConstants.JsonRepInfoProductVersion);
result.RootFolderId = GetString(json, BrowserConstants.JsonRepInfoRootFolderId);
result.RepositoryUrl = GetString(json, BrowserConstants.JsonRepInfoRepositoryUrl);
result.RootUrl = GetString(json, BrowserConstants.JsonRepInfoRootFolderUrl);
result.Capabilities = ConvertRepositoryCapabilities(GetJsonObject(json, BrowserConstants.JsonRepInfoCapabilities));
result.AclCapabilities = ConvertAclCapabilities(GetJsonObject(json, BrowserConstants.JsonRepInfoAclCapabilities));
result.LatestChangeLogToken = GetString(json, BrowserConstants.JsonRepInfoChangeLogToken);
result.CmisVersionSupported = GetString(json, BrowserConstants.JsonRepInfoCmisVersionSupported);
result.ThinClientUri = GetString(json, BrowserConstants.JsonRepInfoThinClientUri);
result.ChangesIncomplete = GetBoolean(json, BrowserConstants.JsonRepInfoChangesIncomplete);
JsonArray changesOnType = GetJsonArray(json, BrowserConstants.JsonRepInfoChangesOnType);
if (changesOnType != null)
{
IList types = new List();
foreach (object type in changesOnType)
{
if (type != null)
{
types.Add(type.ToString().GetCmisEnum());
}
}
result.ChangesOnType = types;
}
result.PrincipalIdAnonymous = GetString(json, BrowserConstants.JsonRepInfoPrincipalIdAnonymous);
result.PrincipalIdAnyone = GetString(json, BrowserConstants.JsonRepInfoPrincipalIdAnyone);
JsonArray extendedFeatures = GetJsonArray(json, BrowserConstants.JsonRepInfoExtendedFeatures);
if (extendedFeatures != null)
{
IList features = new List();
foreach (object extendedFeature in extendedFeatures)
{
JsonObject jsonFeature = GetJsonObject(extendedFeature);
ExtensionFeature feature = new ExtensionFeature();
feature.Id = GetString(jsonFeature, BrowserConstants.JsonFeatureId);
feature.Url = GetString(jsonFeature, BrowserConstants.JsonFeatureUrl);
feature.CommonName = GetString(jsonFeature, BrowserConstants.JsonFeatureCommonName);
feature.VersionLabel = GetString(jsonFeature, BrowserConstants.JsonFeatureVersionLabel);
feature.Description = GetString(jsonFeature, BrowserConstants.JsonFeatureDescription);
JsonObject data = GetJsonObject(jsonFeature, BrowserConstants.JsonFeatureData);
if (data != null)
{
IDictionary dataMap = new Dictionary();
foreach (KeyValuePair kv in data)
{
dataMap.Add(kv.Key, (kv.Value == null ? null : kv.Value.ToString()));
}
if (dataMap.Count > 0)
{
feature.FeatureData = dataMap;
}
}
ConvertExtension(jsonFeature, feature, BrowserConstants.FeatureKeys);
features.Add(feature);
}
if (features.Count > 0)
{
result.ExtensionFeatures = features;
}
}
// handle extensions
ConvertExtension(json, result, BrowserConstants.RepInfoKeys);
return result;
}
internal static IRepositoryCapabilities ConvertRepositoryCapabilities(JsonObject json)
{
if (json == null)
{
return null;
}
RepositoryCapabilities result = new RepositoryCapabilities();
result.ContentStreamUpdatesCapability = GetEnum(json, BrowserConstants.JsonCapContentStreamUpdatability);
result.ChangesCapability = GetEnum(json, BrowserConstants.JsonCapChanges);
result.RenditionsCapability = GetEnum(json, BrowserConstants.JsonCapRenditions);
result.IsGetDescendantsSupported = GetBoolean(json, BrowserConstants.JsonCapGetDescendants);
result.IsGetFolderTreeSupported = GetBoolean(json, BrowserConstants.JsonCapGetFolderTree);
result.IsMultifilingSupported = GetBoolean(json, BrowserConstants.JsonCapMultifiling);
result.IsUnfilingSupported = GetBoolean(json, BrowserConstants.JsonCapUnfiling);
result.IsVersionSpecificFilingSupported = GetBoolean(json, BrowserConstants.JsonCapVersionSpecificFiling);
result.IsPwcSearchableSupported = GetBoolean(json, BrowserConstants.JsonCapPwcSearchable);
result.IsPwcUpdatableSupported = GetBoolean(json, BrowserConstants.JsonCapPwcUpdatable);
result.IsAllVersionsSearchableSupported = GetBoolean(json, BrowserConstants.JsonCapAllVersionsSerachable);
result.OrderByCapability = GetEnum(json, BrowserConstants.JsonCapOrderBy);
result.QueryCapability = GetEnum(json, BrowserConstants.JsonCapQuery);
result.JoinCapability = GetEnum(json, BrowserConstants.JsonCapJoin);
result.AclCapability = GetEnum(json, BrowserConstants.JsonCapAcl);
JsonObject creatablePropertyTypesJson = GetJsonObject(json, BrowserConstants.JsonCapCreatablePropertyTypes);
if (creatablePropertyTypesJson != null)
{
CreatablePropertyTypes creatablePropertyTypes = new CreatablePropertyTypes();
JsonArray canCreateJson = GetJsonArray(creatablePropertyTypesJson, BrowserConstants.JsonCapCreatablePropertyTypesCanCreate);
if (canCreateJson != null)
{
ISet canCreate = new HashSet();
foreach (object o in canCreateJson)
{
try
{
if (o != null)
{
canCreate.Add(o.ToString().GetCmisEnum());
}
}
catch
{
// ignore
}
}
creatablePropertyTypes.CanCreate = canCreate;
}
ConvertExtension(creatablePropertyTypesJson, creatablePropertyTypes, BrowserConstants.CapCreatablePropertyTypesKeys);
result.CreatablePropertyTypes = creatablePropertyTypes;
}
JsonObject newTypeSettableAttributesJson = GetJsonObject(json, BrowserConstants.JsonCapNewTypeSettableAttributes);
if (newTypeSettableAttributesJson != null)
{
NewTypeSettableAttributes newTypeSettableAttributes = new NewTypeSettableAttributes();
newTypeSettableAttributes.CanSetId = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesId);
newTypeSettableAttributes.CanSetLocalName = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesLocalName);
newTypeSettableAttributes.CanSetLocalNamespace = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesLocalNameSpace);
newTypeSettableAttributes.CanSetDisplayName = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesDisplayName);
newTypeSettableAttributes.CanSetQueryName = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesQueryName);
newTypeSettableAttributes.CanSetDescription = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesDescription);
newTypeSettableAttributes.CanSetCreatable = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesCreatable);
newTypeSettableAttributes.CanSetFileable = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesFileable);
newTypeSettableAttributes.CanSetQueryable = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesQueryable);
newTypeSettableAttributes.CanSetFulltextIndexed = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesFulltextIndexed);
newTypeSettableAttributes.CanSetIncludedInSupertypeQuery = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesIncludeInSupertypeQuery);
newTypeSettableAttributes.CanSetControllablePolicy = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesControlablePolicy);
newTypeSettableAttributes.CanSetControllableAcl = GetBoolean(newTypeSettableAttributesJson,
BrowserConstants.JsonCapNewTypeSettableAttributesControlableAcl);
ConvertExtension(newTypeSettableAttributesJson, newTypeSettableAttributes, BrowserConstants.CapNewTypeSettableAttributesKeys);
result.NewTypeSettableAttributes = newTypeSettableAttributes;
}
// handle extensions
ConvertExtension(json, result, BrowserConstants.CapKeys);
return result;
}
internal static AclCapabilities ConvertAclCapabilities(JsonObject json)
{
if (json == null)
{
return null;
}
AclCapabilities result = new AclCapabilities();
result.SupportedPermissions = GetEnum(json, BrowserConstants.JsonAclCapSupportedPermissions);
result.AclPropagation = GetEnum(json, BrowserConstants.JsonAclCapAclPropagation);
JsonArray permissions = GetJsonArray(json, BrowserConstants.JsonAclCapPermissions);
if (permissions != null)
{
IList permissionDefinitionList = new List();
foreach (object permission in permissions)
{
JsonObject permissionMap = GetJsonObject(permission);
if (permissionMap != null)
{
PermissionDefinition permDef = new PermissionDefinition();
permDef.Id = GetString(permissionMap, BrowserConstants.JsonAclCapPermissionPermission);
permDef.Description = GetString(permissionMap, BrowserConstants.JsonAclCapPermissionDescription);
ConvertExtension(permissionMap, permDef, BrowserConstants.AclCapPermissionKeys);
permissionDefinitionList.Add(permDef);
}
}
result.Permissions = permissionDefinitionList;
}
JsonArray permissionMapping = GetJsonArray(json, BrowserConstants.JsonAclCapPermissionMapping);
if (permissionMapping != null)
{
IDictionary permMap = new Dictionary();
foreach (object permission in permissionMapping)
{
JsonObject permissionMap = GetJsonObject(permission);
if (permissionMap != null)
{
PermissionMapping mapping = new PermissionMapping();
string key = GetString(permissionMap, BrowserConstants.JsonAclCapMappingKey);
mapping.Key = key;
JsonArray perms = GetJsonArray(permissionMap, BrowserConstants.JsonAclCapMappingPermission);
if (perms != null)
{
IList permList = new List();
foreach (object perm in perms)
{
if (perm != null)
{
permList.Add(perm.ToString());
}
}
mapping.Permissions = permList;
}
ConvertExtension(permissionMap, mapping, BrowserConstants.AclCapMappingKeys);
permMap.Add(key, mapping);
}
}
result.PermissionMapping = permMap;
}
// handle extensions
ConvertExtension(json, result, BrowserConstants.AclCapKeys);
return result;
}
internal static ITypeDefinition ConvertTypeDefinition(JsonObject json)
{
if (json == null)
{
return null;
}
AbstractTypeDefinition result = null;
string id = GetString(json, BrowserConstants.JsonTypeId);
// find base type
BaseTypeId? baseType = GetEnum(json, BrowserConstants.JsonTypeBaseId);
if (baseType == null)
{
throw new CmisInvalidArgumentException("Invalid base type: " + id);
}
switch (baseType)
{
case BaseTypeId.CmisFolder:
result = new FolderTypeDefinition();
break;
case BaseTypeId.CmisDocument:
result = new DocumentTypeDefinition();
((DocumentTypeDefinition)result).ContentStreamAllowed = GetEnum(json, BrowserConstants.JsonTypeContentstreamAllowed);
((DocumentTypeDefinition)result).IsVersionable = GetBoolean(json, BrowserConstants.JsonTypeVersionable);
break;
case BaseTypeId.CmisRelationship:
result = new RelationshipTypeDefinition();
JsonArray allowedSourceTypes = GetJsonArray(json, BrowserConstants.JsonTypeAllowedSourceTypes);
if (allowedSourceTypes != null)
{
IList types = new List();
foreach (object type in allowedSourceTypes)
{
if (type != null)
{
types.Add(type.ToString());
}
}
((RelationshipTypeDefinition)result).AllowedSourceTypeIds = types;
}
JsonArray allowedTargetTypes = GetJsonArray(json, BrowserConstants.JsonTypeAllowedTargetTypes);
if (allowedTargetTypes != null)
{
IList types = new List();
foreach (object type in allowedTargetTypes)
{
if (type != null)
{
types.Add(type.ToString());
}
}
((RelationshipTypeDefinition)result).AllowedTargetTypeIds = types;
}
break;
case BaseTypeId.CmisPolicy:
result = new PolicyTypeDefinition();
break;
case BaseTypeId.CmisItem:
result = new ItemTypeDefinition();
break;
case BaseTypeId.CmisSecondary:
result = new SecondaryTypeDefinition();
break;
default:
throw new CmisRuntimeException("Type '" + id + "' does not match a base type!");
}
result.BaseTypeId = (BaseTypeId)baseType;
result.Description = GetString(json, BrowserConstants.JsonTypeDescription);
result.DisplayName = GetString(json, BrowserConstants.JsonTypeDisplayname);
result.Id = id;
result.IsControllableAcl = GetBoolean(json, BrowserConstants.JsonTypeControlableAcl);
result.IsControllablePolicy = GetBoolean(json, BrowserConstants.JsonTypeControlablePolicy);
result.IsCreatable = GetBoolean(json, BrowserConstants.JsonTypeCreatable);
result.IsFileable = GetBoolean(json, BrowserConstants.JsonTypeFileable);
result.IsFulltextIndexed = GetBoolean(json, BrowserConstants.JsonTypeFulltextIndexed);
result.IsIncludedInSupertypeQuery = GetBoolean(json, BrowserConstants.JsonTypeIncludeInSuperTypeQuery);
result.IsQueryable = GetBoolean(json, BrowserConstants.JsonTypeQueryable);
result.LocalName = GetString(json, BrowserConstants.JsonTypeLocalName);
result.LocalNamespace = GetString(json, BrowserConstants.JsonTypeLocalNameSpace);
result.ParentTypeId = GetString(json, BrowserConstants.JsonTypeParentId);
result.QueryName = GetString(json, BrowserConstants.JsonTypeQueryName);
JsonObject typeMutabilityJson = GetJsonObject(json, BrowserConstants.JsonTypeTypeMutability);
if (typeMutabilityJson != null)
{
TypeMutability typeMutability = new TypeMutability();
typeMutability.CanCreate = GetBoolean(typeMutabilityJson, BrowserConstants.JsonTypeTypeMutablilityCreate);
typeMutability.CanUpdate = GetBoolean(typeMutabilityJson, BrowserConstants.JsonTypeTypeMutablilityUpdate);
typeMutability.CanDelete = GetBoolean(typeMutabilityJson, BrowserConstants.JsonTypeTypeMutablilityDelete);
ConvertExtension(typeMutabilityJson, typeMutability, BrowserConstants.JsonTypeTypeMutablilityKeys);
result.TypeMutability = typeMutability;
}
JsonObject propertyDefinitions = GetJsonObject(json, BrowserConstants.JsonTypePropertyDefinitions);
if (propertyDefinitions != null)
{
foreach (KeyValuePair propDef in propertyDefinitions)
{
result.AddPropertyDefinition(ConvertPropertyDefinition(GetJsonObject(propDef.Value)));
}
}
// handle extensions
ConvertExtension(json, result, BrowserConstants.TypeKeys);
return result;
}
internal static IPropertyDefinition ConvertPropertyDefinition(JsonObject json)
{
if (json == null)
{
return null;
}
PropertyDefinition result = null;
string id = GetString(json, BrowserConstants.JsonPropertyId);
// find property type
PropertyType? propertyType = GetEnum(json, BrowserConstants.JsonPropertyTypePropertyType);
if (propertyType == null)
{
throw new CmisRuntimeException("Invalid property type '" + id + "'! Data type not set!");
}
// find cardinality
Cardinality? cardinality = GetEnum(json, BrowserConstants.JsonPropertyTypeCardinality);
if (cardinality == null)
{
throw new CmisRuntimeException("Invalid property type '" + id + "'! Cardinality not set!");
}
// set specific values
switch (propertyType)
{
case PropertyType.String:
result = new PropertyStringDefinition();
((PropertyStringDefinition)result).MaxLength = GetInteger(json, BrowserConstants.JsonPropertyTypeMaxLength);
((PropertyStringDefinition)result).Choices = ConvertChoicesString(GetJsonArray(json, BrowserConstants.JsonPropertyTypeChoice));
((PropertyStringDefinition)result).DefaultValue = CopyDefaultValue(json);
break;
case PropertyType.Id:
result = new PropertyIdDefinition();
((PropertyIdDefinition)result).Choices = ConvertChoicesString(GetJsonArray(json, BrowserConstants.JsonPropertyTypeChoice));
((PropertyIdDefinition)result).DefaultValue = CopyDefaultValue(json);
break;
case PropertyType.Boolean:
result = new PropertyBooleanDefinition();
((PropertyBooleanDefinition)result).Choices = ConvertChoicesBoolean(GetJsonArray(json, BrowserConstants.JsonPropertyTypeChoice));
((PropertyBooleanDefinition)result).DefaultValue = CopyDefaultValue(json);
break;
case PropertyType.Integer:
result = new PropertyIntegerDefinition();
((PropertyIntegerDefinition)result).MinValue = GetInteger(json, BrowserConstants.JsonPropertyTypeMinValue);
((PropertyIntegerDefinition)result).MaxValue = GetInteger(json, BrowserConstants.JsonPropertyTypeMaxValue);
((PropertyIntegerDefinition)result).Choices = ConvertChoicesInteger(GetJsonArray(json, BrowserConstants.JsonPropertyTypeChoice));
((PropertyIntegerDefinition)result).DefaultValue = CopyDefaultValue(json);
break;
case PropertyType.DateTime:
result = new PropertyDateTimeDefinition();
((PropertyDateTimeDefinition)result).DateTimeResolution = GetEnum(json, BrowserConstants.JsonPropertyTypeResolution);
((PropertyDateTimeDefinition)result).Choices = ConvertChoicesDateTime(GetJsonArray(json, BrowserConstants.JsonPropertyTypeChoice));
((PropertyDateTimeDefinition)result).DefaultValue = CopyDefaultValue(json);
break;
case PropertyType.Decimal:
result = new PropertyDecimalDefinition();
((PropertyDecimalDefinition)result).MinValue = GetDecimal(json, BrowserConstants.JsonPropertyTypeMinValue);
((PropertyDecimalDefinition)result).MaxValue = GetDecimal(json, BrowserConstants.JsonPropertyTypeMaxValue);
((PropertyDecimalDefinition)result).Precision = GetEnum(json, BrowserConstants.JsonPropertyTypePrecision);
((PropertyDecimalDefinition)result).Choices = ConvertChoicesDecimal(GetJsonArray(json, BrowserConstants.JsonPropertyTypeChoice));
((PropertyDecimalDefinition)result).DefaultValue = CopyDefaultValue(json);
break;
case PropertyType.Html:
result = new PropertyHtmlDefinition();
((PropertyHtmlDefinition)result).Choices = ConvertChoicesString(GetJsonArray(json, BrowserConstants.JsonPropertyTypeChoice));
((PropertyHtmlDefinition)result).DefaultValue = CopyDefaultValue(json);
break;
case PropertyType.Uri:
result = new PropertyUriDefinition();
((PropertyUriDefinition)result).Choices = ConvertChoicesString(GetJsonArray(json, BrowserConstants.JsonPropertyTypeChoice));
((PropertyUriDefinition)result).DefaultValue = CopyDefaultValue(json);
break;
default:
throw new CmisRuntimeException("Property type '" + id + "' does not match a data type!");
}
// generic
result.Id = id;
result.PropertyType = (PropertyType)propertyType;
result.Cardinality = (Cardinality)cardinality;
result.LocalName = GetString(json, BrowserConstants.JsonPropertyTypeLocalName);
result.LocalNamespace = GetString(json, BrowserConstants.JsonPropertyTypeLocalNameSpace);
result.QueryName = GetString(json, BrowserConstants.JsonPropertyTypeQueryName);
result.Description = GetString(json, BrowserConstants.JsonPropertyTypeDescription);
result.DisplayName = GetString(json, BrowserConstants.JsonPropertyTypeDisplayname);
result.IsInherited = GetBoolean(json, BrowserConstants.JsonPropertyTypeInhertited);
result.IsOpenChoice = GetBoolean(json, BrowserConstants.JsonPropertyTypeOpenChoice);
result.IsOrderable = GetBoolean(json, BrowserConstants.JsonPropertyTypeOrderable);
result.IsQueryable = GetBoolean(json, BrowserConstants.JsonPropertyTypeQueryable);
result.IsRequired = GetBoolean(json, BrowserConstants.JsonPropertyTypeRequired);
result.Updatability = GetEnum(json, BrowserConstants.JsonPropertyTypeUpdatability);
// handle extensions
ConvertExtension(json, result, BrowserConstants.PropertyTypeKeys);
return result;
}
private static IList CopyDefaultValue(JsonObject json)
{
IList result = null;
object defaultValue = null;
if (json.TryGetValue(BrowserConstants.JsonPropertyTypeDefaultValue, out defaultValue))
{
if (defaultValue is JsonArray)
{
result = new List();
foreach (object value in (JsonArray)defaultValue)
{
if (value is T)
{
result.Add((T)value);
}
}
}
else if (defaultValue is T)
{
result = new List() { (T)defaultValue };
}
}
return result;
}
///
/// Converts choices.
///
private static IList> ConvertChoicesString(JsonArray choices)
{
if (choices == null)
{
return null;
}
IList> result = new List>();
foreach (object obj in choices)
{
if (!(obj is JsonObject))
{
continue;
}
JsonObject choiceJson = (JsonObject)obj;
Choice choice = new Choice();
choice.DisplayName = GetString(choiceJson, BrowserConstants.JsonPropertyTypeChoiceDisplayname);
object choiceValue = choiceJson[BrowserConstants.JsonPropertyTypeChoiceValue];
List values = new List();
if (choiceValue is JsonArray)
{
foreach (object value in (JsonArray)choiceValue)
{
values.Add((string)GetCmisValue(value, PropertyType.String));
}
}
else
{
values.Add((string)GetCmisValue(choiceValue, PropertyType.String));
}
choice.Value = values;
choice.Choices = ConvertChoicesString(GetJsonArray(choiceJson, BrowserConstants.JsonPropertyTypeChoiceChoice));
result.Add(choice);
}
return result;
}
///
/// Converts choices.
///
private static IList> ConvertChoicesBoolean(JsonArray choices)
{
if (choices == null)
{
return null;
}
IList> result = new List>();
foreach (object obj in choices)
{
if (!(obj is JsonObject))
{
continue;
}
JsonObject choiceJson = (JsonObject)obj;
if (choiceJson != null)
{
Choice choice = new Choice();
choice.DisplayName = GetString(choiceJson, BrowserConstants.JsonPropertyTypeChoiceDisplayname);
object choiceValue = choiceJson[BrowserConstants.JsonPropertyTypeChoiceValue];
List values = new List();
if (choiceValue is JsonArray)
{
foreach (object value in (JsonArray)choiceValue)
{
values.Add((bool?)GetCmisValue(value, PropertyType.Boolean));
}
}
else
{
values.Add((bool?)GetCmisValue(choiceValue, PropertyType.Boolean));
}
choice.Value = values;
choice.Choices = ConvertChoicesBoolean(GetJsonArray(choiceJson, BrowserConstants.JsonPropertyTypeChoiceChoice));
result.Add(choice);
}
}
return result;
}
///
/// Converts choices.
///
private static IList> ConvertChoicesInteger(JsonArray choices)
{
if (choices == null)
{
return null;
}
IList> result = new List>();
foreach (object obj in choices)
{
if (!(obj is JsonObject))
{
continue;
}
JsonObject choiceJson = (JsonObject)obj;
if (choiceJson != null)
{
Choice choice = new Choice();
choice.DisplayName = GetString(choiceJson, BrowserConstants.JsonPropertyTypeChoiceDisplayname);
object choiceValue = choiceJson[BrowserConstants.JsonPropertyTypeChoiceValue];
List values = new List();
if (choiceValue is JsonArray)
{
foreach (object value in (JsonArray)choiceValue)
{
values.Add((BigInteger?)GetCmisValue(value, PropertyType.Integer));
}
}
else
{
values.Add((BigInteger?)GetCmisValue(choiceValue, PropertyType.Integer));
}
choice.Value = values;
choice.Choices = ConvertChoicesInteger(GetJsonArray(choiceJson, BrowserConstants.JsonPropertyTypeChoiceChoice));
result.Add(choice);
}
}
return result;
}
///
/// Converts choices.
///
private static IList> ConvertChoicesDecimal(JsonArray choices)
{
if (choices == null)
{
return null;
}
IList> result = new List>();
foreach (object obj in choices)
{
if (!(obj is JsonObject))
{
continue;
}
JsonObject choiceJson = (JsonObject)obj;
if (choiceJson != null)
{
Choice choice = new Choice();
choice.DisplayName = GetString(choiceJson, BrowserConstants.JsonPropertyTypeChoiceDisplayname);
object choiceValue = choiceJson[BrowserConstants.JsonPropertyTypeChoiceValue];
List values = new List();
if (choiceValue is JsonArray)
{
foreach (object value in (JsonArray)choiceValue)
{
values.Add((decimal?)GetCmisValue(value, PropertyType.Decimal));
}
}
else
{
values.Add((decimal)GetCmisValue(choiceValue, PropertyType.Decimal));
}
choice.Value = values;
choice.Choices = ConvertChoicesDecimal(GetJsonArray(choiceJson, BrowserConstants.JsonPropertyTypeChoiceChoice));
result.Add(choice);
}
}
return result;
}
///
/// Converts choices.
///
private static IList> ConvertChoicesDateTime(JsonArray choices)
{
if (choices == null)
{
return null;
}
IList> result = new List>();
foreach (object obj in choices)
{
if (!(obj is JsonObject))
{
continue;
}
JsonObject choiceJson = (JsonObject)obj;
if (choiceJson != null)
{
Choice choice = new Choice();
choice.DisplayName = GetString(choiceJson, BrowserConstants.JsonPropertyTypeChoiceDisplayname);
object choiceValue = choiceJson[BrowserConstants.JsonPropertyTypeChoiceValue];
List values = new List();
if (choiceValue is JsonArray)
{
foreach (object value in (JsonArray)choiceValue)
{
values.Add((DateTime?)GetCmisValue(value, PropertyType.DateTime));
}
}
else
{
values.Add((DateTime)GetCmisValue(choiceValue, PropertyType.DateTime));
}
choice.Value = values;
choice.Choices = ConvertChoicesDateTime(GetJsonArray(choiceJson, BrowserConstants.JsonPropertyTypeChoiceChoice));
result.Add(choice);
}
}
return result;
}
///
/// Converts an object.
///
internal static JsonObject Convert(IObjectData objectData, ITypeCache typeCache, PropertyMode propertyMode, bool succinct, DateTimeFormat dateTimeFormat)
{
if (objectData == null)
{
return null;
}
JsonObject result = new JsonObject();
// properties
if (objectData.Properties != null)
{
if (succinct)
{
JsonObject properties = Convert(objectData.Properties, objectData.Id, typeCache, propertyMode, true,
dateTimeFormat);
if (properties != null)
{
result.Add(BrowserConstants.JsonObjectSuccinctProperties, properties);
}
}
else
{
JsonObject properties = Convert(objectData.Properties, objectData.Id, typeCache, propertyMode, false,
dateTimeFormat);
if (properties != null)
{
result.Add(BrowserConstants.JsonObjectProperties, properties);
}
}
JsonObject propertiesExtension = new JsonObject();
ConvertExtension(objectData.Properties, propertiesExtension);
if (propertiesExtension.Count > 0)
{
result.Add(BrowserConstants.JsonObjectPropertiesExtension, propertiesExtension);
}
}
// allowable actions
if (objectData.AllowableActions != null)
{
result.Add(BrowserConstants.JsonObjectAllowableActions, Convert(objectData.AllowableActions));
}
// relationships
if (IsNotEmpty(objectData.Relationships))
{
JsonArray relationships = new JsonArray();
foreach (ObjectData relationship in objectData.Relationships)
{
relationships.Add(Convert(relationship, typeCache, propertyMode, succinct, dateTimeFormat));
}
result.Add(BrowserConstants.JsonObjectRelationships, relationships);
}
// change event info
if (objectData.ChangeEventInfo != null && propertyMode == PropertyMode.Change)
{
JsonObject changeEventInfo = new JsonObject();
IChangeEventInfo cei = objectData.ChangeEventInfo;
changeEventInfo.Add(BrowserConstants.JsonChangeEventType, GetJsonEnumValue(cei.ChangeType));
changeEventInfo.Add(BrowserConstants.JsonChangeEventTime, GetJsonValue(cei.ChangeTime, dateTimeFormat));
ConvertExtension(objectData.ChangeEventInfo, changeEventInfo);
result.Add(BrowserConstants.JsonObjectChangeEventInfo, changeEventInfo);
}
// Acl
if ((objectData.Acl != null) && (objectData.Acl.Aces != null) && propertyMode != PropertyMode.Query)
{
result.Add(BrowserConstants.JsonObjectAcl, Convert(objectData.Acl));
}
SetIfNotNull(BrowserConstants.JsonObjectExactAcl, objectData.IsExactAcl, result);
// policy ids
if ((objectData.PolicyIds != null) && (objectData.PolicyIds.PolicyIds != null) && propertyMode != PropertyMode.Query)
{
JsonObject policyIds = new JsonObject();
JsonArray ids = new JsonArray();
policyIds.Add(BrowserConstants.JsonObjectPolicyIdsIds, ids);
foreach (string pi in objectData.PolicyIds.PolicyIds)
{
ids.Add(pi);
}
ConvertExtension(objectData.PolicyIds, policyIds);
result.Add(BrowserConstants.JsonObjectPolicyIds, policyIds);
}
// renditions
if (IsNotEmpty(objectData.Renditions))
{
JsonArray renditions = new JsonArray();
foreach (RenditionData rendition in objectData.Renditions)
{
renditions.Add(Convert(rendition));
}
result.Add(BrowserConstants.JsonObjectRenditions, renditions);
}
ConvertExtension(objectData, result);
return result;
}
///
/// Converts a bag of properties.
///
internal static JsonObject Convert(IProperties properties, string objectId, ITypeCache typeCache, PropertyMode propertyMode, bool succinct, DateTimeFormat dateTimeFormat)
{
if (properties == null)
{
return null;
}
// get the type
ITypeDefinition type = null;
if (typeCache != null)
{
IPropertyData typeProp = properties[PropertyIds.ObjectTypeId];
if (typeProp != null && typeProp.PropertyType == PropertyType.Id)
{
object typeId = typeProp.FirstValue;
if (typeId != null)
{
type = typeCache.GetTypeDefinition(typeId.ToString());
}
}
}
JsonObject result = new JsonObject();
foreach (IPropertyData property in properties.PropertyList)
{
IPropertyDefinition propDef = null;
if (typeCache != null)
{
propDef = typeCache.GetPropertyDefinition(property.Id);
}
if (propDef == null && type != null)
{
propDef = type[property.Id];
}
if (propDef == null && typeCache != null && objectId != null && propertyMode != PropertyMode.Change)
{
typeCache.GetTypeDefinitionForObject(objectId);
propDef = typeCache.GetPropertyDefinition(property.Id);
}
string propId = (propertyMode == PropertyMode.Query ? property.QueryName : property.Id);
if (propId == null)
{
throw new CmisRuntimeException("No query name or alias for property '" + property.Id + "'!");
}
result.Add(propId, Convert(property, propDef, succinct, dateTimeFormat));
}
return result;
}
///
/// Converts a property.
///
internal static object Convert(IPropertyData property, IPropertyDefinition propDef, bool succinct, DateTimeFormat dateTimeFormat)
{
if (property == null)
{
return null;
}
if (succinct)
{
object result = null;
if (propDef != null)
{
if (IsNullOrEmpty(property.Values))
{
result = null;
}
else if (propDef.Cardinality == Cardinality.Single)
{
result = GetJsonValue(property.Values[0], dateTimeFormat);
}
else
{
JsonArray values = new JsonArray();
foreach (object value in property.Values)
{
values.Add(GetJsonValue(value, dateTimeFormat));
}
result = values;
}
}
else
{
if (IsNullOrEmpty(property.Values))
{
result = null;
}
else
{
JsonArray values = new JsonArray();
foreach (object value in property.Values)
{
values.Add(GetJsonValue(value, dateTimeFormat));
}
result = values;
}
}
return result;
}
else
{
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonPropertyId, property.Id);
SetIfNotNull(BrowserConstants.JsonPropertyLocalName, property.LocalName, result);
SetIfNotNull(BrowserConstants.JsonPropertyDisplayname, property.DisplayName, result);
SetIfNotNull(BrowserConstants.JsonPropertyQueryName, property.QueryName, result);
if (propDef != null)
{
result.Add(BrowserConstants.JsonPropertyDatatype, propDef.PropertyType.GetCmisValue());
result.Add(BrowserConstants.JsonPropertyCardinality, GetJsonEnumValue(propDef.Cardinality));
if (IsNullOrEmpty(property.Values))
{
result.Add(BrowserConstants.JsonPropertyValue, null);
}
else if (propDef.Cardinality == Cardinality.Single)
{
result.Add(BrowserConstants.JsonPropertyValue, GetJsonValue(property.Values[0], dateTimeFormat));
}
else
{
JsonArray values = new JsonArray();
foreach (object value in property.Values)
{
values.Add(GetJsonValue(value, dateTimeFormat));
}
result.Add(BrowserConstants.JsonPropertyValue, values);
}
}
else
{
result.Add(BrowserConstants.JsonPropertyDatatype, property.PropertyType.GetCmisValue());
if (IsNullOrEmpty(property.Values))
{
result.Add(BrowserConstants.JsonPropertyValue, null);
}
else
{
JsonArray values = new JsonArray();
foreach (object value in property.Values)
{
values.Add(GetJsonValue(value, dateTimeFormat));
}
result.Add(BrowserConstants.JsonPropertyValue, values);
}
}
ConvertExtension(property, result);
return result;
}
}
///
/// Converts allowable actions.
///
internal static JsonObject Convert(IAllowableActions allowableActions)
{
if (allowableActions == null)
{
return null;
}
JsonObject result = new JsonObject();
ISet actionSet = allowableActions.Actions;
var values = Enum.GetValues(typeof(PortCMIS.Enums.Action));
foreach (var value in values)
{
PortCMIS.Enums.Action action = (PortCMIS.Enums.Action)Enum.ToObject(typeof(PortCMIS.Enums.Action), value);
result.Add(action.GetCmisValue(), actionSet.Contains(action));
}
ConvertExtension(allowableActions, result);
return result;
}
///
/// Converts an Acl.
///
internal static JsonObject Convert(IAcl acl)
{
if (acl == null || acl.Aces == null)
{
return null;
}
JsonArray aceObjects = new JsonArray();
foreach (IAce ace in acl.Aces)
{
JsonArray permissions = new JsonArray();
if (ace.Permissions != null)
{
foreach (string p in ace.Permissions)
{
permissions.Add(p);
}
}
JsonObject aceObject = new JsonObject();
JsonObject principalObject = new JsonObject();
principalObject.Add(BrowserConstants.JsonAcePrincipalId, ace.PrincipalId);
ConvertExtension(ace.Principal, principalObject);
aceObject.Add(BrowserConstants.JsonAcePrincipal, principalObject);
aceObject.Add(BrowserConstants.JsonAcePermissions, permissions);
aceObject.Add(BrowserConstants.JsonAceIsDirect, ace.IsDirect);
ConvertExtension(ace, aceObject);
aceObjects.Add(aceObject);
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonAclAces, aceObjects);
SetIfNotNull(BrowserConstants.JsonAclIsExact, acl.IsExact, result);
ConvertExtension(acl, result);
return result;
}
///
/// Converts a rendition.
///
internal static JsonObject Convert(IRenditionData rendition)
{
if (rendition == null)
{
return null;
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonRenditionStreamId, rendition.StreamId);
result.Add(BrowserConstants.JsonRenditionMimeType, rendition.MimeType);
result.Add(BrowserConstants.JsonRenditionLength, rendition.Length);
result.Add(BrowserConstants.JsonRenditionKind, rendition.Kind);
SetIfNotNull(BrowserConstants.JsonRenditionTitle, rendition.Title, result);
SetIfNotNull(BrowserConstants.JsonRenditionHeight, rendition.Height, result);
SetIfNotNull(BrowserConstants.JsonRenditionWidth, rendition.Width, result);
SetIfNotNull(BrowserConstants.JsonRenditionDocumentId, rendition.RenditionDocumentId, result);
ConvertExtension(rendition, result);
return result;
}
///
/// Converts a query object list.
///
internal static JsonObject Convert(IObjectList list, ITypeCache typeCache, PropertyMode propertyMode,
bool succinct, DateTimeFormat dateTimeFormat)
{
if (list == null)
{
return null;
}
JsonObject result = new JsonObject();
JsonArray objects = new JsonArray();
if (list.Objects != null)
{
foreach (IObjectData objectData in list.Objects)
{
objects.Add(Convert(objectData, typeCache, propertyMode, succinct, dateTimeFormat));
}
}
if (propertyMode == PropertyMode.Query)
{
result.Add(BrowserConstants.JsonQueryResultListResults, objects);
SetIfNotNull(BrowserConstants.JsonQueryResultListHasMoreItems, list.HasMoreItems, result);
SetIfNotNull(BrowserConstants.JsonQueryResultListNumItems, list.NumItems, result);
}
else
{
result.Add(BrowserConstants.JsonObjectListObjects, objects);
SetIfNotNull(BrowserConstants.JsonObjectListHasMoreItems, list.HasMoreItems, result);
SetIfNotNull(BrowserConstants.JsonObjectListNumItems, list.NumItems, result);
}
ConvertExtension(list, result);
return result;
}
///
/// Converts an object in a folder list.
///
internal static JsonObject Convert(IObjectInFolderData objectInFolder, ITypeCache typeCache,
bool succinct, DateTimeFormat dateTimeFormat)
{
if ((objectInFolder == null) || (objectInFolder.Object == null))
{
return null;
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonObjectInFolderObject, Convert(objectInFolder.Object, typeCache, PropertyMode.Object, succinct, dateTimeFormat));
SetIfNotNull(BrowserConstants.JsonObjectInFolderPathSegment, objectInFolder.PathSegment, result);
ConvertExtension(objectInFolder, result);
return result;
}
///
/// Converts a folder list.
///
internal static JsonObject Convert(IObjectInFolderList objectInFolderList, ITypeCache typeCache,
bool succinct, DateTimeFormat dateTimeFormat)
{
if (objectInFolderList == null)
{
return null;
}
JsonObject result = new JsonObject();
if (objectInFolderList.Objects != null)
{
JsonArray objects = new JsonArray();
foreach (ObjectInFolderData objectData in objectInFolderList.Objects)
{
objects.Add(Convert(objectData, typeCache, succinct, dateTimeFormat));
}
result.Add(BrowserConstants.JsonObjectInFolderListObjects, objects);
}
SetIfNotNull(BrowserConstants.JsonObjectInFolderListHasMoreItems, objectInFolderList.HasMoreItems, result);
SetIfNotNull(BrowserConstants.JsonObjectInFolderListNumItems, objectInFolderList.NumItems, result);
ConvertExtension(objectInFolderList, result);
return result;
}
///
/// Converts a folder container.
///
internal static JsonObject Convert(IObjectInFolderContainer container, ITypeCache typeCache, bool succinct, DateTimeFormat dateTimeFormat)
{
if (container == null)
{
return null;
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonObjectInFolderContainerObject, Convert(container.Object, typeCache, succinct, dateTimeFormat));
if (IsNotEmpty(container.Children))
{
JsonArray children = new JsonArray();
foreach (ObjectInFolderContainer descendant in container.Children)
{
children.Add(Convert(descendant, typeCache, succinct, dateTimeFormat));
}
result.Add(BrowserConstants.JsonObjectInFolderContainerChildren, children);
}
ConvertExtension(container, result);
return result;
}
///
/// Converts an object parent.
///
internal static JsonObject Convert(IObjectParentData parent, ITypeCache typeCache, bool succinct, DateTimeFormat dateTimeFormat)
{
if ((parent == null) || (parent.Object == null))
{
return null;
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonObjectParentsObject, Convert(parent.Object, typeCache, PropertyMode.Object, succinct, dateTimeFormat));
if (parent.RelativePathSegment != null)
{
result.Add(BrowserConstants.JsonObjectParentsRelativePathSegment, parent.RelativePathSegment);
}
ConvertExtension(parent, result);
return result;
}
///
/// Converts a type definition.
///
internal static JsonObject Convert(ITypeDefinition type, DateTimeFormat dateTimeFormat)
{
if (type == null)
{
return null;
}
JsonObject result = new JsonObject();
result.Add(BrowserConstants.JsonTypeId, type.Id);
result.Add(BrowserConstants.JsonTypeLocalName, type.LocalName);
result.Add(BrowserConstants.JsonTypeLocalNameSpace, type.LocalNamespace);
SetIfNotNull(BrowserConstants.JsonTypeDisplayname, type.DisplayName, result);
SetIfNotNull(BrowserConstants.JsonTypeQueryName, type.QueryName, result);
SetIfNotNull(BrowserConstants.JsonTypeDescription, type.Description, result);
result.Add(BrowserConstants.JsonTypeBaseId, type.BaseTypeId.GetCmisValue());
SetIfNotNull(BrowserConstants.JsonTypeParentId, type.ParentTypeId, result);
result.Add(BrowserConstants.JsonTypeCreatable, type.IsCreatable);
result.Add(BrowserConstants.JsonTypeFileable, type.IsFileable);
result.Add(BrowserConstants.JsonTypeQueryable, type.IsQueryable);
result.Add(BrowserConstants.JsonTypeFulltextIndexed, type.IsFulltextIndexed);
result.Add(BrowserConstants.JsonTypeIncludeInSuperTypeQuery, type.IsIncludedInSupertypeQuery);
result.Add(BrowserConstants.JsonTypeControlablePolicy, type.IsControllablePolicy);
result.Add(BrowserConstants.JsonTypeControlableAcl, type.IsControllableAcl);
if (type.TypeMutability != null)
{
ITypeMutability typeMutability = type.TypeMutability;
JsonObject typeMutabilityJson = new JsonObject();
typeMutabilityJson.Add(BrowserConstants.JsonTypeTypeMutablilityCreate, typeMutability.CanCreate);
typeMutabilityJson.Add(BrowserConstants.JsonTypeTypeMutablilityUpdate, typeMutability.CanUpdate);
typeMutabilityJson.Add(BrowserConstants.JsonTypeTypeMutablilityDelete, typeMutability.CanDelete);
ConvertExtension(typeMutability, typeMutabilityJson);
result.Add(BrowserConstants.JsonTypeTypeMutability, typeMutabilityJson);
}
if (type is DocumentTypeDefinition)
{
result.Add(BrowserConstants.JsonTypeVersionable, ((DocumentTypeDefinition)type).IsVersionable);
result.Add(BrowserConstants.JsonTypeContentstreamAllowed, GetJsonEnumValue(((DocumentTypeDefinition)type).ContentStreamAllowed));
}
if (type is RelationshipTypeDefinition)
{
result.Add(BrowserConstants.JsonTypeAllowedSourceTypes, GetJsonArrayFromList(((RelationshipTypeDefinition)type).AllowedSourceTypeIds));
result.Add(BrowserConstants.JsonTypeAllowedTargetTypes, GetJsonArrayFromList(((RelationshipTypeDefinition)type).AllowedTargetTypeIds));
}
if (IsNotEmpty(type.PropertyDefinitions))
{
JsonObject propertyDefs = new JsonObject();
foreach (IPropertyDefinition pd in type.PropertyDefinitions)
{
propertyDefs.Add(pd.Id, Convert(pd, dateTimeFormat));
}
result.Add(BrowserConstants.JsonTypePropertyDefinitions, propertyDefs);
}
ConvertExtension(type, result);
return result;
}
///
/// Converts a property type definition.
///
internal static JsonObject Convert(IPropertyDefinition propertyDefinition, DateTimeFormat dateTimeFormat)
{
if (propertyDefinition == null)
{
return null;
}
JsonObject result = new JsonObject();
// type specific
if (propertyDefinition is PropertyStringDefinition)
{
SetIfNotNull(BrowserConstants.JsonPropertyTypeMaxLength, ((PropertyStringDefinition)propertyDefinition).MaxLength, result);
AddCmisDefaultValue(((PropertyStringDefinition)propertyDefinition).DefaultValue, propertyDefinition.Cardinality, result, dateTimeFormat);
AddChoices(((PropertyStringDefinition)propertyDefinition).Choices, propertyDefinition.Cardinality, result, dateTimeFormat);
}
else if (propertyDefinition is PropertyIdDefinition)
{
AddCmisDefaultValue(((PropertyIdDefinition)propertyDefinition).DefaultValue, propertyDefinition.Cardinality, result, dateTimeFormat);
AddChoices(((PropertyIdDefinition)propertyDefinition).Choices, propertyDefinition.Cardinality, result, dateTimeFormat);
}
else if (propertyDefinition is PropertyIntegerDefinition)
{
SetIfNotNull(BrowserConstants.JsonPropertyTypeMinValue, ((PropertyIntegerDefinition)propertyDefinition).MinValue, result);
SetIfNotNull(BrowserConstants.JsonPropertyTypeMaxValue, ((PropertyIntegerDefinition)propertyDefinition).MaxValue, result);
AddCmisDefaultValue(((PropertyIntegerDefinition)propertyDefinition).DefaultValue, propertyDefinition.Cardinality, result, dateTimeFormat);
AddChoices(((PropertyIntegerDefinition)propertyDefinition).Choices, propertyDefinition.Cardinality, result, dateTimeFormat);
}
else if (propertyDefinition is PropertyDecimalDefinition)
{
SetIfNotNull(BrowserConstants.JsonPropertyTypeMinValue, ((PropertyDecimalDefinition)propertyDefinition).MinValue, result);
SetIfNotNull(BrowserConstants.JsonPropertyTypeMaxValue, ((PropertyDecimalDefinition)propertyDefinition).MaxValue, result);
DecimalPrecision? precision = ((PropertyDecimalDefinition)propertyDefinition).Precision;
if (precision != null)
{
result.Add(BrowserConstants.JsonPropertyTypePrecision, precision.GetCmisValue());
}
AddCmisDefaultValue(((PropertyDecimalDefinition)propertyDefinition).DefaultValue, propertyDefinition.Cardinality, result, dateTimeFormat);
AddChoices(((PropertyDecimalDefinition)propertyDefinition).Choices, propertyDefinition.Cardinality, result, dateTimeFormat);
}
else if (propertyDefinition is PropertyBooleanDefinition)
{
AddCmisDefaultValue(((PropertyBooleanDefinition)propertyDefinition).DefaultValue, propertyDefinition.Cardinality, result, dateTimeFormat);
AddChoices(((PropertyBooleanDefinition)propertyDefinition).Choices, propertyDefinition.Cardinality, result, dateTimeFormat);
}
else if (propertyDefinition is PropertyDateTimeDefinition)
{
DateTimeResolution? resolution = ((PropertyDateTimeDefinition)propertyDefinition).DateTimeResolution;
if (resolution != null)
{
result.Add(BrowserConstants.JsonPropertyTypeResolution, resolution.GetCmisValue());
}
AddCmisDefaultValue(((PropertyDateTimeDefinition)propertyDefinition).DefaultValue, propertyDefinition.Cardinality, result, dateTimeFormat);
AddChoices(((PropertyDateTimeDefinition)propertyDefinition).Choices, propertyDefinition.Cardinality, result, dateTimeFormat);
}
else if (propertyDefinition is PropertyHtmlDefinition)
{
AddCmisDefaultValue(((PropertyHtmlDefinition)propertyDefinition).DefaultValue, propertyDefinition.Cardinality, result, dateTimeFormat);
AddChoices(((PropertyHtmlDefinition)propertyDefinition).Choices, propertyDefinition.Cardinality, result, dateTimeFormat);
}
else if (propertyDefinition is PropertyUriDefinition)
{
AddCmisDefaultValue(((PropertyUriDefinition)propertyDefinition).DefaultValue, propertyDefinition.Cardinality, result, dateTimeFormat);
AddChoices(((PropertyUriDefinition)propertyDefinition).Choices, propertyDefinition.Cardinality, result, dateTimeFormat);
}
else
{
throw new CmisRuntimeException("Invalid property definition!");
}
// generic
result.Add(BrowserConstants.JsonPropertyTypeId, propertyDefinition.Id);
result.Add(BrowserConstants.JsonPropertyTypeLocalName, propertyDefinition.LocalName);
SetIfNotNull(BrowserConstants.JsonPropertyTypeLocalNameSpace, propertyDefinition.LocalNamespace, result);
SetIfNotNull(BrowserConstants.JsonPropertyTypeDisplayname, propertyDefinition.DisplayName, result);
SetIfNotNull(BrowserConstants.JsonPropertyTypeQueryName, propertyDefinition.QueryName, result);
SetIfNotNull(BrowserConstants.JsonPropertyTypeDescription, propertyDefinition.Description, result);
result.Add(BrowserConstants.JsonPropertyTypePropertyType, propertyDefinition.PropertyType.GetCmisValue());
result.Add(BrowserConstants.JsonPropertyTypeCardinality, GetJsonEnumValue(propertyDefinition.Cardinality));
result.Add(BrowserConstants.JsonPropertyTypeUpdatability, GetJsonEnumValue(propertyDefinition.Updatability));
SetIfNotNull(BrowserConstants.JsonPropertyTypeInhertited, propertyDefinition.IsInherited, result);
result.Add(BrowserConstants.JsonPropertyTypeRequired, propertyDefinition.IsRequired);
result.Add(BrowserConstants.JsonPropertyTypeQueryable, propertyDefinition.IsQueryable);
result.Add(BrowserConstants.JsonPropertyTypeOrderable, propertyDefinition.IsOrderable);
SetIfNotNull(BrowserConstants.JsonPropertyTypeOpenChoice, propertyDefinition.IsOpenChoice, result);
ConvertExtension(propertyDefinition, result);
return result;
}
private static void AddCmisDefaultValue(IList defaultValue, Cardinality? cardinality, JsonObject target, DateTimeFormat dateTimeFormat)
{
if (defaultValue == null)
{
return;
}
if (cardinality == Cardinality.Single)
{
if (defaultValue.Count > 0)
{
target.Add(BrowserConstants.JsonPropertyTypeDefaultValue, GetJsonValue(defaultValue[0], dateTimeFormat));
}
}
else
{
JsonArray values = new JsonArray();
foreach (object value in defaultValue)
{
values.Add(GetJsonValue(value, dateTimeFormat));
}
target.Add(BrowserConstants.JsonPropertyTypeDefaultValue, values);
}
}
private static void AddChoices(IList> choices, Cardinality? cardinality, JsonObject target, DateTimeFormat dateTimeFormat)
{
if (choices == null)
{
return;
}
target.Add(BrowserConstants.JsonPropertyTypeChoice, ConvertChoices(choices, cardinality, dateTimeFormat));
}
///
/// Converts choices.
///
private static JsonArray ConvertChoices(IList> choices, Cardinality? cardinality, DateTimeFormat dateTimeFormat)
{
if (choices == null)
{
return null;
}
JsonArray result = new JsonArray();
foreach (IChoice