update extension reports with existing grade or we get too many copies
[xtuple] / scripts / lib / util / convert_specialized.js
1 /*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
2 regexp:true, undef:true, strict:true, trailing:true, white:true */
3 /*global _:true */
4
5 (function () {
6   "use strict";
7
8   var path = require('path');
9
10   var convertFromMetasql = function (content, filename, defaultSchema) {
11     var lines = content.split("\n"),
12       schema = defaultSchema ? "'" + defaultSchema + "'" : "NULL",
13       group,
14       i = 2,
15       name,
16       notes = "",
17       grade = 0,
18       deleteSql,
19       insertSql;
20
21     if (lines[0].indexOf("-- Group: ") !== 0 ||
22         lines[1].indexOf("-- Name: ") !== 0 ||
23         lines[2].indexOf("-- Notes:") !== 0) {
24       throw new Error("Improperly formatted metasql: " + filename);
25     }
26     group = lines[0].substring("-- Group: ".length).trim();
27     name = lines[1].substring("-- Name: ".length).trim();
28     while (lines[i].indexOf("--") === 0) {
29       notes = notes + lines[i].substring(2) + "\n";
30       i++;
31     }
32     notes = notes.substring(" Notes:".length);
33     if (notes.indexOf("must be grade 10") >= 0) {
34       grade = 10;
35     }
36
37     insertSql = "select saveMetasql (" +
38       "'" + group + "'," +
39       "'" + name + "'," +
40       "$$" + notes + "$$," +
41       "$$" + content + "$$," +
42       "true, " + schema + ", " + grade + ");";
43
44     return insertSql;
45   };
46
47   var convertFromReport = function (content, filename, defaultSchema) {
48     var lines = content.split("\n"),
49       name,
50       grade = "0",
51       tableName = defaultSchema ? defaultSchema + ".pkgreport" : "report",
52       description,
53       upsertSql;
54
55     if (lines[3].indexOf(" <name>") !== 0 ||
56         lines[4].indexOf(" <description>") !== 0) {
57       throw new Error("Improperly formatted report");
58     }
59     name = lines[3].substring(" <name>".length).trim();
60     name = name.substring(0, name.indexOf("<"));
61     description = lines[4].substring(" <description>".length).trim();
62     description = description.substring(0, description.indexOf("<"));
63     if (lines[5].indexOf("grade") >= 0) {
64       grade = lines[5].substring(" <grade>".length).trim();
65       grade = grade.substring(0, grade.indexOf("<"));
66     }
67
68     upsertSql = "do language plpgsql $do$" +
69                 "declare _grade integer := null;" +
70                 " begin" +
71                 "  select min(report_grade) into _grade" +
72                 "    from " + tableName +
73                 "   where report_name = '" + name + "';" +
74                 "  if _grade is null then" +
75                 "    insert into " + tableName + " (report_name, report_descrip," +
76                 "        report_source, report_loaddate, report_grade)" +
77                 "      select '" + name + "', $$" + description + "$$," +
78                 "        $$" + content + "$$, now(), min(sequence_value)" +
79                 "        from sequence" +
80                 "       where sequence_value >= " + grade + "" +
81                 "         and sequence_value not in (" +
82                 "        select report_grade from report" +
83                 "         where report_name = '" + name + "'" +
84                 "       );" +
85                 "  else " +
86                 "    update " + tableName + " set" +
87                 "      report_descrip = $$" + description + "$$," +
88                 "      report_source = $$" + content + "$$," +
89                 "      report_loaddate = now() " +
90                 "     where report_name = '" + name + "'" +
91                 "      and report_grade = _grade;" +
92                 "  end if;" +
93                 " end $do$;";
94     return upsertSql;
95   };
96
97   var convertFromScript = function (content, filename, defaultSchema) {
98     var name = path.basename(filename, '.js'),
99       tableName = defaultSchema ? defaultSchema + ".pkgscript" : "unknown",
100       notes = "", //"xtMfg package",
101       insertSql,
102       updateSql;
103
104     insertSql = "insert into " + tableName + " (script_name, script_order, script_enabled, " +
105       "script_source, script_notes) select " +
106       "'" + name + "', 0, TRUE, " +
107       "$$" + content + "$$," +
108       "'" + notes + "'" +
109       " where not exists (select c.script_id from " + tableName + " c " +
110       "where script_name = '" + name + "');";
111
112     updateSql = "update " + tableName + " set " +
113       "script_name = '" + name + "', script_order = 0, script_enabled = TRUE, " +
114       "script_source = $$" + content +
115       "$$, script_notes = '" + notes + "' " +
116       "where script_name = '" + name + "';";
117
118     return insertSql + updateSql;
119   };
120
121   var convertFromUiform = function (content, filename, defaultSchema) {
122     var name = path.basename(filename, '.ui'),
123       tableName = defaultSchema ? defaultSchema + ".pkguiform" : "unknown",
124       notes = "", //"xtMfg package",
125       insertSql,
126       updateSql;
127
128     insertSql = "insert into " + tableName + " (uiform_name, uiform_order, uiform_enabled, " +
129       "uiform_source, uiform_notes) select " +
130       "'" + name + "', 0, TRUE, " +
131       "$$" + content + "$$," +
132       "'" + notes + "' " +
133       " where not exists (select c.uiform_id from " + tableName + " c " +
134       "where uiform_name = '" + name + "');";
135
136     updateSql = "update " + tableName + " set uiform_name = '" +
137       name + "', uiform_order = 0, uiform_enabled = TRUE, " +
138       "uiform_source = $$" + content + "$$, uiform_notes = '" + notes + "' " +
139       "where uiform_name = '" + name + "';";
140
141     return insertSql + updateSql;
142   };
143
144   exports.conversionMap = {
145     mql: convertFromMetasql,
146     xml: convertFromReport,
147     js: convertFromScript,
148     ui: convertFromUiform,
149     sql: function (content) {
150       // no op
151       return content;
152     }
153   };
154 }());