372 lines
16 KiB
C#
372 lines
16 KiB
C#
using EnVisage;
|
|
using EnVisage.Code.BLL;
|
|
using PrevuWebAPI.Code.Managers;
|
|
using PrevuWebAPI.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using EnVisage.Models;
|
|
using PrevuWebAPI;
|
|
using Code.Utils;
|
|
using Newtonsoft.Json;
|
|
using MongoDB.Driver;
|
|
using MongoDB.Bson.Serialization;
|
|
using MongoDB.Bson;
|
|
using PrevuWebAPI.Code;
|
|
|
|
namespace Code.Managers
|
|
{
|
|
public class APIProjectManager: BaseManager
|
|
{
|
|
private CompanyManager _companyManager = new CompanyManager(PrevuEFContext.PrevuEntity);
|
|
private APIExternalContactsManager _externalcontactManager = new APIExternalContactsManager();
|
|
private APIInternalContactsManager _internalcontactManager = new APIInternalContactsManager();
|
|
private APIStatusManager _statusManager = new APIStatusManager();
|
|
private APIProjectTypeManager _typeManager = new APIProjectTypeManager();
|
|
private APIScenarioManager _scenarioManager = new APIScenarioManager();
|
|
private APIStrategicGoalManager _strategicgoalManager = new APIStrategicGoalManager();
|
|
private APITeamManager _teamManager = new APITeamManager();
|
|
private ClientManager _clientManger = new ClientManager(PrevuEFContext.PrevuEntity);
|
|
private APIClientInformationCallManager _ClientInfoControlManager = new APIClientInformationCallManager();
|
|
public APIClientCallBackManager _callBackManager = new APIClientCallBackManager();
|
|
private Guid RequestID = Guid.NewGuid();
|
|
private List<string> reason = new List<string>();
|
|
public List<APIProjectModel> getProjects()
|
|
{
|
|
ProjectManager _manager = new ProjectManager(null);
|
|
List<APIProjectModel> results = new List<APIProjectModel>();
|
|
var envProjects=_manager.DataTable.ToList<Project>();
|
|
foreach(Project p in envProjects)
|
|
{
|
|
var apim = GetAPIModel(p);
|
|
if (apim != null)
|
|
results.Add(apim);
|
|
}
|
|
return results;
|
|
}
|
|
public APIProjectModel getProject(Guid id)
|
|
{
|
|
ProjectManager _manager = new ProjectManager(null);
|
|
var p= _manager.Load(id, true);
|
|
return GetAPIModel(p);
|
|
}
|
|
public APIProjectModel getProject(string projectNumber)
|
|
{
|
|
ProjectManager _manager = new ProjectManager(null);
|
|
var p = _manager.DataTable.Where(x => x.ProjectNumber == projectNumber).FirstOrDefault();
|
|
return GetAPIModel(p);
|
|
}
|
|
public ResultModel Save(APIProjectModel p)
|
|
{
|
|
ResultModel rt = new ResultModel();
|
|
rt.RequestID = RequestID;
|
|
reason = new List<string>();
|
|
|
|
EnVisageEntities prevuEF = PrevuEFContext.PrevuEntity;
|
|
ProjectManager _manager = new ProjectManager(prevuEF);
|
|
ProjectModel modelToSave;
|
|
ProjectModel storeInMongoPreUpdate;
|
|
ProjectModel m = null;
|
|
try
|
|
{
|
|
m = (ProjectModel) _manager.DataTable.Where(x => x.ProjectNumber == p.ProjectNumber).FirstOrDefault();
|
|
|
|
|
|
|
|
}
|
|
catch (Exception dds) {
|
|
m = null;
|
|
|
|
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in get api model!");
|
|
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
|
|
"------------------------------------------------------------------------------"+ Environment.NewLine + RequestID + Environment.NewLine+ dds+
|
|
"------------------------------------------------------------------------------"+
|
|
Environment.NewLine+ Environment.NewLine );
|
|
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + RequestID);
|
|
MongoManager.StoreAPIProjectInMongo(p, reason);
|
|
rt.Result = (int) RejectReason.Exception;
|
|
rt.Messages = reason;
|
|
return rt;
|
|
}
|
|
if (m != null)
|
|
{
|
|
storeInMongoPreUpdate = m.Clone();
|
|
rt.RequestType = "Update";
|
|
modelToSave = UpdateModel(p, m);
|
|
if (modelToSave == null)
|
|
{
|
|
|
|
|
|
MongoManager.StoreAPIProjectInMongo(p, reason);
|
|
rt.Result = (int) RejectReason.Exception;
|
|
rt.Messages = reason;
|
|
return rt;
|
|
}
|
|
if (modelToSave.AssignedTeams.Count == 0)
|
|
{
|
|
Settings.Logger.Log(NLog.LogLevel.Debug, "No Project Teams for update");
|
|
|
|
reason.Add("Missing Team entry. Team is a required value.");
|
|
MongoManager.StoreAPIProjectInMongo(p, reason);
|
|
rt.Result =(int)RejectReason.MissingTeam;
|
|
rt.Messages = reason;
|
|
return rt;
|
|
}
|
|
else {
|
|
MongoManager.StoreProjectSnapShotInMongo(p, storeInMongoPreUpdate, modelToSave);
|
|
Project projDataObj = _manager.Save(modelToSave);
|
|
reason = new List<string>();
|
|
reason.Add("record processed");
|
|
rt.Result = (int) RejectReason.OK;
|
|
rt.Messages = reason;
|
|
|
|
}
|
|
|
|
}
|
|
else {
|
|
modelToSave = GetModel(p);
|
|
if (modelToSave == null)
|
|
{
|
|
MongoManager.StoreAPIProjectInMongo(p, reason);
|
|
rt.Result = (int) RejectReason.Exception;
|
|
rt.Messages = reason;
|
|
return rt;
|
|
}
|
|
if (modelToSave.Parts.Count == 1 && !modelToSave.HasChildren)
|
|
{
|
|
if (modelToSave.Parts[0].AssignedTeams.Count > 0)
|
|
{
|
|
MongoManager.StoreProjectSnapShotInMongo(p, null, modelToSave);
|
|
_manager.Save(modelToSave);
|
|
reason = new List<string>();
|
|
reason.Add("record processed");
|
|
rt.Result = (int) RejectReason.OK;
|
|
rt.Messages = reason;
|
|
|
|
}
|
|
else
|
|
{
|
|
Settings.Logger.Log(NLog.LogLevel.Debug, "No Project Teams for update");
|
|
reason.Add("Missing Team entry. Team is a required value.");
|
|
MongoManager.StoreAPIProjectInMongo(p, reason);
|
|
rt.Result = (int) RejectReason.MissingTeam;
|
|
rt.Messages = reason;
|
|
return rt;
|
|
}
|
|
}
|
|
|
|
}
|
|
prevuEF.SaveChanges();
|
|
return rt;
|
|
}
|
|
|
|
|
|
private ProjectModel UpdateModel(APIProjectModel m, ProjectModel pm)
|
|
{
|
|
try {
|
|
var goals = _strategicgoalManager.ValidateGoals(m.StrategicGoals);
|
|
pm.Name = m.ProjectName;
|
|
pm.Number = m.ProjectNumber;
|
|
pm.Priority = m.PriotiryInt();
|
|
var s = pm.Scenarios.Where(x => x.Type == EnVisage.Code.ScenarioType.Portfolio && x.Status == EnVisage.Code.ScenarioStatus.Active).FirstOrDefault();
|
|
if (s != null && m.ScenarioStartDate.HasValue && m.ScenarioEndDate.HasValue)
|
|
{
|
|
s.StartDate = m.ScenarioStartDate;
|
|
s.EndDate = m.ScenarioEndDate;
|
|
}
|
|
pm.CompanyName = m.CompanyName;
|
|
pm.Color = GetColorByType(m.ProjectType);
|
|
pm.TypeId = _typeManager.ValidateType(m.ProjectType);
|
|
pm.StatusId = _statusManager.ValidateStatus(m.ProjectStatus);
|
|
pm.StrategicGoals = goals;
|
|
pm.InternalContacts = _internalcontactManager.ValidateContacts(m.InternalContacts);
|
|
pm.ExternalContacts = _externalcontactManager.ValidateContacts(m.InternalContacts);
|
|
pm.ClientId = this.ValidateClientName(m.ClientName);
|
|
pm.AssignedTeams = _teamManager.ValidateTeams(m.Teams);
|
|
|
|
if (pm.Parts.Count == 1 && !pm.HasChildren)
|
|
{
|
|
pm.Parts[0].AssignedTeams = _teamManager.ValidateTeams(m.Teams);
|
|
pm.Parts[0].TypeId = pm.TypeId;
|
|
pm.Parts[0].StatusId = pm.StatusId;
|
|
pm.Parts[0].Probability = GetProbability(m.Probability);
|
|
pm.Parts[0].Priority = pm.Priority;
|
|
pm.Parts[0].StrategicGoals = goals;
|
|
pm.Parts[0].InternalContacts = pm.InternalContacts;
|
|
pm.Parts[0].ExternalContacts = pm.ExternalContacts;
|
|
pm.Parts[0].ClientId = pm.ClientId;
|
|
}
|
|
Settings.Logger.Log(NLog.LogLevel.Debug, "StrategicGoals count" + goals.Count);
|
|
}
|
|
catch(Exception dd)
|
|
{
|
|
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in Update model!");
|
|
//List<string> reason = new List<string>();
|
|
Guid code = new Guid();
|
|
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
|
|
"------------------------------------------------------------------------------" + Environment.NewLine + code + Environment.NewLine + dd +
|
|
"------------------------------------------------------------------------------" +
|
|
Environment.NewLine + Environment.NewLine);
|
|
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + code);
|
|
MongoManager.StoreAPIProjectInMongo(m, reason);
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
private string GetColorByType(string typeStr)
|
|
{
|
|
Settings s = new Settings();
|
|
return s.GetSettingValue(typeStr);
|
|
}
|
|
private ProjectModel GetModel(APIProjectModel m)
|
|
{
|
|
try {
|
|
List<ProjectPartModel> partlist = new List<ProjectPartModel>();
|
|
partlist.Add(GetProjectPartModel(m));
|
|
CompanyModel companyModel = _companyManager.GetCompanyByName(m.CompanyName);
|
|
Guid? companyid = null;
|
|
if (companyModel != null)
|
|
companyid = companyModel.Id;
|
|
|
|
string color = GetColorByType(m.ProjectType);
|
|
|
|
return new ProjectModel()
|
|
{
|
|
CompanyId = companyid,
|
|
Name = m.ProjectName,
|
|
Number = m.ProjectNumber,
|
|
ClientName = m.ClientName,
|
|
Color = color,
|
|
Priority = m.PriotiryInt(),
|
|
CompanyName = m.CompanyName,
|
|
TypeId = _typeManager.ValidateType(m.ProjectType),
|
|
StatusId = _statusManager.ValidateStatus(m.ProjectStatus),
|
|
StrategicGoals = _strategicgoalManager.ValidateGoals(m.StrategicGoals),
|
|
InternalContacts = _internalcontactManager.ValidateContacts(m.InternalContacts),
|
|
ExternalContacts = _externalcontactManager.ValidateContacts(m.InternalContacts),
|
|
ClientId = this.ValidateClientName(m.ClientName),
|
|
Parts = partlist
|
|
|
|
};
|
|
}
|
|
catch (Exception dd)
|
|
{
|
|
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in create model!");
|
|
//List<string> reason = new List<string>();
|
|
Guid code = new Guid();
|
|
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
|
|
"------------------------------------------------------------------------------" + Environment.NewLine + code + Environment.NewLine + dd +
|
|
"------------------------------------------------------------------------------" +
|
|
Environment.NewLine + Environment.NewLine);
|
|
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + code);
|
|
MongoManager.StoreAPIProjectInMongo(m, reason);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
private ProjectPartModel GetProjectPartModel(APIProjectModel m)
|
|
{
|
|
try {
|
|
return new ProjectPartModel()
|
|
{
|
|
|
|
Probability = GetProbability(m.Probability),
|
|
Priority = m.PriotiryInt(),
|
|
TypeId = _typeManager.ValidateType(m.ProjectType),
|
|
StatusId = _statusManager.ValidateStatus(m.ProjectStatus),
|
|
StrategicGoals = _strategicgoalManager.ValidateGoals(m.StrategicGoals),
|
|
InternalContacts = _internalcontactManager.ValidateContacts(m.InternalContacts),
|
|
ExternalContacts = _externalcontactManager.ValidateContacts(m.InternalContacts),
|
|
ClientId = this.ValidateClientName(m.ClientName),
|
|
AssignedTeams= _teamManager.ValidateTeams(m.Teams)
|
|
};
|
|
}
|
|
catch (Exception dd)
|
|
{
|
|
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in create project part model!");
|
|
//List<string> reason = new List<string>();
|
|
Guid code = new Guid();
|
|
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
|
|
"------------------------------------------------------------------------------" + Environment.NewLine + code + Environment.NewLine + dd +
|
|
"------------------------------------------------------------------------------" +
|
|
Environment.NewLine + Environment.NewLine);
|
|
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + code);
|
|
MongoManager.StoreAPIProjectInMongo(m, reason);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
private short GetProbability(string p)
|
|
{
|
|
try
|
|
{
|
|
short s = short.Parse(p);
|
|
return s;
|
|
}
|
|
catch (Exception d) { }
|
|
return 0;
|
|
}
|
|
public Guid ValidateClientName(string clientname)
|
|
{
|
|
var clientmodel =_clientManger.LoadClientModelByName(clientname);
|
|
if (clientmodel == null || clientmodel.Id == Guid.Empty)
|
|
clientmodel = _callBackManager.GetClientInfoFromClient(clientname);
|
|
return clientmodel.Id;
|
|
|
|
}
|
|
private APIProjectModel GetAPIModel(Project p)
|
|
{
|
|
try {
|
|
if (p == null)
|
|
return null;
|
|
if (!p.ParentProjectId.HasValue)
|
|
{
|
|
DateTime? S_StartDate = null;
|
|
DateTime? S_EndDate = null;
|
|
Scenario s = _scenarioManager.GetActiveScenario(p.Id);
|
|
if (s != null)
|
|
{
|
|
S_StartDate = s.EndDate;
|
|
S_EndDate = s.StartDate;
|
|
}
|
|
|
|
var model = new APIProjectModel()
|
|
{
|
|
CompanyName = (p.Company == null) ? null : p.Company.Name,
|
|
ProjectName = p.Name,
|
|
ProjectNumber = p.ProjectNumber,
|
|
ExternalContacts = _externalcontactManager.GetContacts(p.Id),
|
|
InternalContacts = _internalcontactManager.GetContacts(p.Id),
|
|
Priority = p.Priority.ToString(),
|
|
ProjectStatus = _statusManager.getStatus(p.StatusId),
|
|
ProjectType = _typeManager.getProjectType(p.TypeId),
|
|
StrategicGoals = _strategicgoalManager.getGoalsForProject(p.Id),
|
|
Teams = _teamManager.getTeamsForProject(p.Id),
|
|
ScenarioStartDate = S_StartDate,
|
|
ScenarioEndDate = S_EndDate,
|
|
ClientName = p.Client.Name,
|
|
ProjectRoles = new List<APIProjectRoleModel>()
|
|
|
|
};
|
|
return model;
|
|
}
|
|
}
|
|
catch (Exception dd)
|
|
{
|
|
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in get api model!");
|
|
Settings.Logger.Log(NLog.LogLevel.Error, dd);
|
|
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|