let administrators modify package contents even if they are not marked as such to...
[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       updateSql,
54       insertSql;
55
56     if (lines[3].indexOf(" <name>") !== 0 ||
57         lines[4].indexOf(" <description>") !== 0) {
58       throw new Error("Improperly formatted report");
59     }
60     name = lines[3].substring(" <name>".length).trim();
61     name = name.substring(0, name.indexOf("<"));
62     description = lines[4].substring(" <description>".length).trim();
63     description = description.substring(0, description.indexOf("<"));
64     if (lines[5].indexOf("grade") >= 0) {
65       grade = lines[5].substring(" <grade>".length).trim();
66       grade = grade.substring(0, grade.indexOf("<"));
67     }
68
69     insertSql = "insert into " + tableName + " (report_name, report_descrip, " +
70       "report_source, report_loaddate, report_grade) select " +
71       "'" + name + "'," +
72       "$$" + description + "$$," +
73       "$$" + content + "$$," +
74       "now(), " + grade +
75       " where not exists (select c.report_id from " + tableName + " c " +
76       "where report_name = '" + name +
77       "' and report_grade = " + grade + ");";
78
79     updateSql = "update " + tableName + " set " +
80       " report_descrip = $$" + description +
81       "$$, report_source = $$" + content +
82       "$$, report_loaddate = now() " +
83       "where report_name = '" + name +
84       "' and report_grade = " + grade + ";";
85
86     return insertSql + updateSql;
87   };
88
89   var convertFromScript = function (content, filename, defaultSchema) {
90     var name = path.basename(filename, '.js'),
91       tableName = defaultSchema ? defaultSchema + ".pkgscript" : "unknown",
92       notes = "", //"xtMfg package",
93       insertSql,
94       updateSql;
95
96     insertSql = "insert into " + tableName + " (script_name, script_order, script_enabled, " +
97       "script_source, script_notes) select " +
98       "'" + name + "', 0, TRUE, " +
99       "$$" + content + "$$," +
100       "'" + notes + "'" +
101       " where not exists (select c.script_id from " + tableName + " c " +
102       "where script_name = '" + name + "');";
103
104     updateSql = "update " + tableName + " set " +
105       "script_name = '" + name + "', script_order = 0, script_enabled = TRUE, " +
106       "script_source = $$" + content +
107       "$$, script_notes = '" + notes + "' " +
108       "where script_name = '" + name + "';";
109
110     return insertSql + updateSql;
111   };
112
113   var convertFromUiform = function (content, filename, defaultSchema) {
114     var name = path.basename(filename, '.ui'),
115       tableName = defaultSchema ? defaultSchema + ".pkguiform" : "unknown",
116       notes = "", //"xtMfg package",
117       insertSql,
118       updateSql;
119
120     insertSql = "insert into " + tableName + " (uiform_name, uiform_order, uiform_enabled, " +
121       "uiform_source, uiform_notes) select " +
122       "'" + name + "', 0, TRUE, " +
123       "$$" + content + "$$," +
124       "'" + notes + "' " +
125       " where not exists (select c.uiform_id from " + tableName + " c " +
126       "where uiform_name = '" + name + "');";
127
128     updateSql = "update " + tableName + " set uiform_name = '" +
129       name + "', uiform_order = 0, uiform_enabled = TRUE, " +
130       "uiform_source = $$" + content + "$$, uiform_notes = '" + notes + "' " +
131       "where uiform_name = '" + name + "';";
132
133     return insertSql + updateSql;
134   };
135
136   exports.conversionMap = {
137     mql: convertFromMetasql,
138     xml: convertFromReport,
139     js: convertFromScript,
140     ui: convertFromUiform,
141     sql: function (content) {
142       // no op
143       return content;
144     }
145   };
146 }());