/*
* 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.Data;
using PortCMIS.Enums;
using PortCMIS.Exceptions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace PortCMIS.Client.Impl
{
///
/// Default IObjectFactory implementation.
///
public class ObjectFactory : IObjectFactory
{
private ISession session;
///
public virtual void Initialize(ISession session, IDictionary parameters)
{
this.session = session;
}
// Acl and ACE
///
public virtual IAcl ConvertAces(IList aces)
{
if (aces == null) { return null; }
Acl result = new Acl()
{
Aces = new List()
};
foreach (IAce ace in aces)
{
result.Aces.Add(ace);
}
return result;
}
///
public virtual IAcl CreateAcl(IList aces)
{
Acl acl = new Acl()
{
Aces = aces
};
return acl;
}
///
public virtual IAce CreateAce(string principal, IList permissions)
{
Ace ace = new Ace();
ace.IsDirect = true;
Principal acePrincipal = new Principal();
acePrincipal.Id = principal;
ace.Principal = acePrincipal;
ace.Permissions = permissions;
return ace;
}
// policies
///
public virtual IList ConvertPolicies(IList policies)
{
if (policies == null) { return null; }
IList result = new List();
foreach (IPolicy policy in policies)
{
if (policy != null && policy.Id != null)
{
result.Add(policy.Id);
}
}
return result;
}
// renditions
///
public virtual IRendition ConvertRendition(string objectId, IRenditionData rendition)
{
if (rendition == null)
{
throw new ArgumentNullException(nameof(rendition));
}
return new Rendition(this.session, objectId, rendition.StreamId, rendition.MimeType, rendition.Length, rendition.Kind,
rendition.Title, rendition.Height, rendition.Height, rendition.RenditionDocumentId);
}
// content stream
///
public virtual IContentStream CreateContentStream(string filename, long length, string mimetype, Stream stream)
{
ContentStream result = new ContentStream()
{
FileName = filename,
Length = length,
MimeType = mimetype,
Stream = stream
};
return result;
}
// types
///
public virtual IObjectType ConvertTypeDefinition(ITypeDefinition typeDefinition)
{
switch (typeDefinition.BaseTypeId)
{
case BaseTypeId.CmisDocument:
return new DocumentType(session, (IDocumentTypeDefinition)typeDefinition);
case BaseTypeId.CmisFolder:
return new FolderType(session, (IFolderTypeDefinition)typeDefinition);
case BaseTypeId.CmisRelationship:
return new RelationshipType(session, (IRelationshipTypeDefinition)typeDefinition);
case BaseTypeId.CmisPolicy:
return new PolicyType(session, (IPolicyTypeDefinition)typeDefinition);
case BaseTypeId.CmisItem:
return new ItemType(session, (IItemTypeDefinition)typeDefinition);
case BaseTypeId.CmisSecondary:
return new SecondaryType(session, (ISecondaryTypeDefinition)typeDefinition);
default:
throw new CmisRuntimeException("Unknown base type!");
}
}
///
public virtual IObjectType GetTypeFromObjectData(IObjectData objectData)
{
if (objectData == null || objectData.Properties == null)
{
return null;
}
IPropertyData typeProperty = objectData.Properties[PropertyIds.ObjectTypeId];
if (typeProperty == null)
{
return null;
}
string typeId = typeProperty.FirstValue as string;
if (typeId == null)
{
return null;
}
return session.GetTypeDefinition(typeId);
}
// properties
///
public virtual IProperty CreateProperty(IPropertyDefinition type, IList values)
{
return new Property(type, (IList