Fix #5725 - disable create branch until ticket selected
[gitlive] / RooProject.vala
1 /**
2 This a mostly a static class that manages an array of it'self...
3
4 */
5
6 static RooProject  _RooProject;
7 public class RooProject  : Object 
8 {
9         
10         public string  id; // not really important that they are numbers..
11         public string code;
12         public string name;
13         public string type;
14
15         
16         // --- static from here on....
17         
18         
19         
20         static Gee.ArrayList<RooProject> _projects;
21         static int loadcount = 0;
22         const string roourl = "https://roojs.com/admin.php/Roo";  
23         
24         
25         static RooProject singleton()
26     {
27        if (_RooProject == null) {
28             _RooProject = new RooProject();
29                 RooProject.loadcount = 0;
30             RooProject.loadProjects();
31
32         }
33         return _RooProject;
34     }
35         
36         
37         public static Gee.ArrayList<RooProject> projects()
38         {
39                 RooProject.singleton();
40
41                 return RooProject._projects;
42         }
43         
44         
45         public static  RooProject? getProjectByRepo(GitRepo repo)
46         {
47                 RooProject.singleton(); // init...
48                 
49                 // fixme -- needs to get from rep list..
50  
51                 var pid = "";
52                 foreach(var roo_repo in RooRepo.repos()) {
53                         if (roo_repo.shortname == repo.name) {
54                                 pid = roo_repo.project_id;
55                                 break;
56                         }
57                 }
58                 if (pid == "") {
59                         GLib.debug("getProjectByRepo: repo has no project");            
60                         return null;
61                 }
62                 // get project by id...
63                 foreach(var roo_project in RooProject.projects()) {
64                         if (roo_project.id == pid) {
65                                 GLib.debug("getProjectByRepo: project_id = %s", pid);
66                                 return roo_project;
67                         }
68                 }
69                 GLib.debug("getProjectByRepo: can not find project");                           
70                 return null;
71         
72         }
73         
74         
75         public static  RooProject addProject(Json.Object t)
76         {
77                 RooProject.singleton(); // init...
78                 
79                 var add = new RooProject();
80                 add.id = t.get_string_member("id");
81                 add.name = t.get_string_member("name");
82                 add.type = t.get_string_member("type");
83                 add.code = t.get_string_member("code");                                         
84                 RooProject._projects.add(add);
85                 GLib.debug("ADD project  %s : %s : %s", add.id, add.code, add.name);
86                 return add;
87         }
88
89         public static void reload()  /// has to be called on singleton..
90         {
91                 RooProject.loadcount = 0;
92                 RooProject.singleton();
93                 if (RooProject.loadcount == 0 ) {
94                         RooProject.loadProjects();
95                 }
96         }
97         
98         static  void loadProjects() // called from singleton...
99         {
100                 RooProject._projects = new Gee.ArrayList<RooProject>();
101          
102         RooProject.loadcount ++;        
103                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
104          
105                 table.insert("query[project_filter]","P,N,U");
106                 table.insert("limit","200");
107                 table.insert("sort","name");
108                 table.insert("dir","ASC");
109
110                 var params = Soup.Form.encode_hash(table);
111                 
112                 var url = "%s/%s?%s" . printf(roourl, "core_project", params);
113                 
114                 GLib.debug("request %s", url);
115                 
116                 var session = new Soup.Session ();
117                 session.timeout = 0;
118                 var message = new Soup.Message ("GET", url);
119                 
120                 
121                 RooTicket.setAuth(message);
122                 
123                 session.send_message (message);
124                 
125                 var data = (string) message.response_body.flatten().data;
126                 //GLib.debug("got %s", data);
127                 try {
128                         var parser = new Json.Parser ();
129                         parser.load_from_data (data, -1);
130
131                         var response = parser.get_root().get_object();
132                         var status = response.get_boolean_member("success");
133                 
134                         if(!status){
135                                 GLib.error(response.get_string_member("errorMsg"));
136                                 return;
137                         }
138                         var rd = response.get_array_member ("data");
139                         
140                         // got a valid result...
141
142                         for(var i = 0; i < rd.get_length(); i++) {
143                                 RooProject.addProject(rd.get_object_element(i));
144                         }
145                          
146                 
147                 } catch (Error e) {
148                         GLib.error(e.message);
149                         return;
150                 }
151                 
152         }
153          
154         
155         
156         
157         
158 }
159