Ejemplo n.º 1
0
            /// <summary>
            /// Initializes a new instance of the <see cref="odTimeTracker.Model.Activity"/> class
            /// from string describing the activity.
            /// </summary>
            /// <description>
            /// Here are some examples of valid activity strings:
            ///
            /// - "Updating README.md@odTimeTracker;Projects#Updating README.md with examples of usage."
            /// - "Simple activity without any values"
            /// - "Activity with project specified@Project name"
            /// - "Activity with some tags;tag1,tag2"
            ///
            /// Order of values (name, project, tags, description) is important. But the only
            /// required value is `name`.
            /// </description>
            /// <param name="activityString">Activity string.</param>
            public Activity(string activityString, SqliteStorage storage)
            {
                string name        = "";
                string projectName = "";
                string tags        = "";
                string description = "";

                string rest = activityString;

                string[] parts;

                bool hasProjectName = (rest.IndexOf('@') >= 0);
                bool hasTags        = (rest.IndexOf(';') >= 0);
                bool hasDescription = (rest.IndexOf('#') >= 0);

                // Project name
                if (hasProjectName == true)
                {
                    parts = activityString.Split('@');
                    name  = parts[0];
                    rest  = parts[1];
                }

                // Tags
                if (hasTags)
                {
                    parts = rest.Split(';');

                    if (hasProjectName == true)
                    {
                        projectName = parts[0];
                    }
                    else
                    {
                        name = parts[0];
                    }

                    rest = parts[1];
                }

                // Description
                if (hasDescription == true)
                {
                    parts = rest.Split('#');

                    if (hasProjectName == false && hasTags == false)
                    {
                        name = parts[0];
                    }
                    else if (hasProjectName == false && hasTags == true)
                    {
                        tags = parts[0];
                    }
                    else if (hasProjectName == true && hasTags == false)
                    {
                        projectName = parts[0];
                    }
                    else                     /*if (hasProjectName == true && hasTags == true)*/
                    {
                        tags = parts[0];
                    }

                    description = parts[1];
                }

                if (hasTags == true && tags.Length == 0)
                {
                    tags = rest;
                }
                else if (hasProjectName == true && projectName.Length == 0)
                {
                    projectName = rest;
                }
                else if (name.Length == 0)
                {
                    name = rest;
                }

                // Check if project with given name exists and if no create it!
                long projectId = 0;

                if (hasProjectName == true && projectName.Length > 0)
                {
                    var fProject = storage.SelectProjectByName(projectName);

                    if (fProject[0] == null)
                    {
                        Project newProject = new Project();
                        newProject.Name    = projectName;
                        newProject.Created = DateTime.UtcNow;
                        newProject         = storage.InsertProject(newProject);
                        projectId          = newProject.ProjectId;
                    }
                    else
                    {
                        projectId = fProject[0].ProjectId;
                    }

                    ProjectId = projectId;
                }

                ProjectId   = projectId;
                Name        = name;
                Description = description;
                Tags        = tags;
                Created     = DateTime.UtcNow;
            }