/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using ASC.Api.CRM.Wrappers;
using ASC.Common.Web;
using ASC.CRM.Core.Entities;
using ASC.Web.Api.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ASC.Api.CRM
{
public partial class CRMController
{
///
/// Creates a new task template container with the type and title specified in the request
///
/// Type
/// Title
/// Create task template container
/// Task Templates
///
/// Task template container
///
///
/// false
[Create(@"{entityType:(contact|person|company|opportunity|case)}/tasktemplatecontainer")]
public TaskTemplateContainerWrapper CreateTaskTemplateContainer(string entityType, string title)
{
if (string.IsNullOrEmpty(title)) throw new ArgumentException();
var taskTemplateContainer = new TaskTemplateContainer
{
EntityType = ToEntityType(entityType),
Title = title
};
taskTemplateContainer.ID = DaoFactory.GetTaskTemplateContainerDao().SaveOrUpdate(taskTemplateContainer);
return ToTaskTemplateContainerWrapper(taskTemplateContainer);
}
///
/// Returns the complete list of all the task template containers available on the portal
///
/// Type
/// Get task template container list
/// Task Templates
///
/// Task template container list
///
/// false
[Read(@"{entityType:(contact|person|company|opportunity|case)}/tasktemplatecontainer")]
public IEnumerable GetTaskTemplateContainers(string entityType)
{
return ToTaskListTemplateContainerWrapper(DaoFactory.GetTaskTemplateContainerDao().GetItems(ToEntityType(entityType)));
}
///
/// Deletes the task template container with the ID specified in the request
///
/// Task template container ID
/// Delete task template container
/// Task Templates
///
/// Deleted task template container
///
///
///
/// false
[Delete(@"tasktemplatecontainer/{containerid:int}")]
public TaskTemplateContainerWrapper DeleteTaskTemplateContainer(int containerid)
{
if (containerid <= 0) throw new ArgumentException();
var result = ToTaskTemplateContainerWrapper(DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid));
if (result == null) throw new ItemNotFoundException();
DaoFactory.GetTaskTemplateContainerDao().Delete(containerid);
return result;
}
///
/// Updates the task template container with the ID specified in the request
///
/// Task template container ID
/// Title
/// Update task template container
/// Task Templates
///
/// Task template container
///
///
///
/// false
[Update(@"tasktemplatecontainer/{containerid:int}")]
public TaskTemplateContainerWrapper UpdateTaskTemplateContainer(int containerid, string title)
{
if (containerid <= 0 || string.IsNullOrEmpty(title)) throw new ArgumentException();
var result = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (result == null) throw new ItemNotFoundException();
result.Title = title;
DaoFactory.GetTaskTemplateContainerDao().SaveOrUpdate(result);
return ToTaskTemplateContainerWrapper(result);
}
///
/// Returns the detailed information on the task template container with the ID specified in the request
///
/// Task template container ID
/// Get task template container by ID
/// Task Templates
///
/// Task template container
///
///
///
/// false
[Read(@"tasktemplatecontainer/{containerid:int}")]
public TaskTemplateContainerWrapper GetTaskTemplateContainerByID(int containerid)
{
if (containerid <= 0) throw new ArgumentException();
var item = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (item == null) throw new ItemNotFoundException();
return ToTaskTemplateContainerWrapper(item);
}
///
/// Returns the list of all tasks in the container with the ID specified in the request
///
/// Task template container ID
/// Get task template list by contaier ID
/// Task Templates
///
/// Task template list
///
///
///
/// false
[Read(@"tasktemplatecontainer/{containerid:int}/tasktemplate")]
public IEnumerable GetTaskTemplates(int containerid)
{
if (containerid <= 0) throw new ArgumentException();
var container = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (container == null) throw new ItemNotFoundException();
return DaoFactory.GetTaskTemplateDao().GetList(containerid).ConvertAll(ToTaskTemplateWrapper);
}
///
/// Creates a new task template with the parameters specified in the request in the container with the selected ID
///
/// Task template container ID
/// Title
/// Description
/// Responsible ID
/// Category ID
/// Responsible notification: notify or not
/// Ticks offset
///
/// Create task template
/// Task Templates
/// Task template
///
///
/// false
[Create(@"tasktemplatecontainer/{containerid:int}/tasktemplate")]
public TaskTemplateWrapper CreateTaskTemplate(
int containerid,
string title,
string description,
Guid responsibleid,
int categoryid,
bool isNotify,
long offsetTicks,
bool deadLineIsFixed
)
{
if (containerid <= 0 || string.IsNullOrEmpty(title) || categoryid <= 0) throw new ArgumentException();
var container = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (container == null) throw new ItemNotFoundException();
var item = new TaskTemplate
{
CategoryID = categoryid,
ContainerID = containerid,
DeadLineIsFixed = deadLineIsFixed,
Description = description,
isNotify = isNotify,
ResponsibleID = responsibleid,
Title = title,
Offset = TimeSpan.FromTicks(offsetTicks)
};
item.ID = DaoFactory.GetTaskTemplateDao().SaveOrUpdate(item);
return ToTaskTemplateWrapper(item);
}
///
/// Updates the selected task template with the parameters specified in the request in the container with the selected ID
///
/// Task template ID
/// Task template container ID
/// Title
/// Description
/// Responsible ID
/// Category ID
/// Responsible notification: notify or not
/// Ticks offset
///
/// Update task template
/// Task Templates
/// Task template
///
///
/// false
[Update(@"tasktemplatecontainer/{containerid:int}/tasktemplate")]
public TaskTemplateWrapper UpdateTaskTemplate(
int id,
int containerid,
string title,
string description,
Guid responsibleid,
int categoryid,
bool isNotify,
long offsetTicks,
bool deadLineIsFixed
)
{
if (containerid <= 0 || string.IsNullOrEmpty(title) || categoryid <= 0) throw new ArgumentException();
var updatingItem = DaoFactory.GetTaskTemplateDao().GetByID(id);
if (updatingItem == null) throw new ItemNotFoundException();
var container = DaoFactory.GetTaskTemplateContainerDao().GetByID(containerid);
if (container == null) throw new ItemNotFoundException();
var item = new TaskTemplate
{
CategoryID = categoryid,
ContainerID = containerid,
DeadLineIsFixed = deadLineIsFixed,
Description = description,
isNotify = isNotify,
ResponsibleID = responsibleid,
Title = title,
ID = id,
Offset = TimeSpan.FromTicks(offsetTicks)
};
item.ID = DaoFactory.GetTaskTemplateDao().SaveOrUpdate(item);
return ToTaskTemplateWrapper(item);
}
///
/// Deletes the task template with the ID specified in the request
///
/// Task template ID
/// Delete task template
/// Task Templates
/// Task template
///
///
/// false
[Delete(@"tasktemplatecontainer/tasktemplate/{id:int}")]
public TaskTemplateWrapper DeleteTaskTemplate(int id)
{
if (id <= 0) throw new ArgumentException();
var taskTemplate = DaoFactory.GetTaskTemplateDao().GetByID(id);
if (taskTemplate == null) throw new ItemNotFoundException();
var result = ToTaskTemplateWrapper(taskTemplate);
DaoFactory.GetTaskTemplateDao().Delete(id);
return result;
}
///
/// Return the task template with the ID specified in the request
///
/// Task template ID
/// Get task template by ID
/// Task Templates
/// Task template
///
///
/// false
[Read(@"tasktemplatecontainer/tasktemplate/{id:int}")]
public TaskTemplateWrapper GetTaskTemplateByID(int id)
{
if (id <= 0) throw new ArgumentException();
var taskTemplate = DaoFactory.GetTaskTemplateDao().GetByID(id);
if (taskTemplate == null) throw new ItemNotFoundException();
return ToTaskTemplateWrapper(taskTemplate);
}
protected TaskTemplateWrapper ToTaskTemplateWrapper(TaskTemplate taskTemplate)
{
return new TaskTemplateWrapper
{
Category = GetTaskCategoryByID(taskTemplate.CategoryID),
ContainerID = taskTemplate.ContainerID,
DeadLineIsFixed = taskTemplate.DeadLineIsFixed,
Description = taskTemplate.Description,
ID = taskTemplate.ID,
isNotify = taskTemplate.isNotify,
Title = taskTemplate.Title,
OffsetTicks = taskTemplate.Offset.Ticks,
Responsible = EmployeeWraper.Get(taskTemplate.ResponsibleID)
};
}
protected IEnumerable ToTaskListTemplateContainerWrapper(IEnumerable items)
{
var result = new List();
var taskTemplateDictionary = DaoFactory.GetTaskTemplateDao().GetAll()
.GroupBy(item => item.ContainerID)
.ToDictionary(x => x.Key, y => y.Select(ToTaskTemplateWrapper));
foreach (var item in items)
{
var taskTemplateContainer = new TaskTemplateContainerWrapper
{
Title = item.Title,
EntityType = item.EntityType.ToString(),
ID = item.ID
};
if (taskTemplateDictionary.ContainsKey(taskTemplateContainer.ID))
{
taskTemplateContainer.Items = taskTemplateDictionary[taskTemplateContainer.ID];
}
result.Add(taskTemplateContainer);
}
return result;
}
protected TaskTemplateContainerWrapper ToTaskTemplateContainerWrapper(TaskTemplateContainer item)
{
return ToTaskListTemplateContainerWrapper(new List
{
item
}).FirstOrDefault();
}
}
}