RooTicket.vala
[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();
48                 
49                 // fixme -- needs to get from rep list..
50                 var rt = RooTicket.singleton();         
51                 if (rt.repos.size < 1) {
52                         rt.loadRepos();
53                 }
54                  
55                 
56                 var pid = "";
57                 foreach(var roo_repo in rt.repos) {
58                         if (roo_repo.shortname == repo.name) {
59                                 pid = roo_repo.project_id;
60                                 break;
61                         }
62                 }
63                 if (pid == "") {
64                         GLib.debug("getProjectByRepo: repo has no project");            
65                         return null;
66                 }
67                 // get project by id...
68                 foreach(var roo_project in RooProject.projects()) {
69                         if (roo_project.id == pid) {
70                                 GLib.debug("getProjectByRepo: project_id = %s", pid);
71                                 return roo_project;
72                         }
73                 }
74                 GLib.debug("getProjectByRepo: can not find project");                           
75                 return null;
76         
77         }
78         
79         
80         public static  RooProject addProject(Json.Object t)
81         {
82                 RooProject.singleton(); // init...
83                 
84                 var add = new RooProject();
85                 add.id = t.get_string_member("id");
86                 add.name = t.get_string_member("name");
87                 add.type = t.get_string_member("type");
88                 add.code = t.get_string_member("code");                                         
89                 RooProject._projects.add(add);
90                 GLib.debug("ADD project  %s : %s : %s", add.id, add.code, add.name);
91                 return add;
92         }
93
94         public static void reload()  /// has to be called on singleton..
95         {
96                 RooProject.loadcount = 0;
97                 RooProject.singleton();
98                 if (RooProject.loadcount == 0 ) {
99                         RooProject.loadProjects();
100                 }
101         }
102         
103         static  void loadProjects() // called from singleton...
104         {
105                 RooProject._projects = new Gee.ArrayList<RooProject>();
106          
107         RooProject.loadcount ++;        
108                 var table = new GLib.HashTable<string, string>(str_hash, str_equal);
109          
110                 table.insert("query[project_filter]","P,N,U");
111                 table.insert("limit","200");
112                 table.insert("sort","name");
113                 table.insert("dir","ASC");
114
115                 var params = Soup.Form.encode_hash(table);
116                 
117                 var url = "%s/%s?%s" . printf(roourl, "core_project", params);
118                 
119                 GLib.debug("request %s", url);
120                 
121                 var session = new Soup.Session ();
122                 session.timeout = 0;
123                 var message = new Soup.Message ("GET", url);
124                 
125                 
126                 RooTicket.setAuth(message);
127                 
128                 session.send_message (message);
129                 
130                 var data = (string) message.response_body.flatten().data;
131                 //GLib.debug("got %s", data);
132                 try {
133                         var parser = new Json.Parser ();
134                         parser.load_from_data (data, -1);
135
136                         var response = parser.get_root().get_object();
137                         var status = response.get_boolean_member("success");
138                 
139                         if(!status){
140                                 GLib.error(response.get_string_member("errorMsg"));
141                                 return;
142                         }
143                         var rd = response.get_array_member ("data");
144                         
145                         // got a valid result...
146
147                         for(var i = 0; i < rd.get_length(); i++) {
148                                 RooProject.addProject(rd.get_object_element(i));
149                         }
150                          
151                 
152                 } catch (Error e) {
153                         GLib.error(e.message);
154                         return;
155                 }
156                 
157         }
158          
159         
160         
161         
162         
163 }
164