-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
204 lines (175 loc) · 5.23 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* JBoss, Home of Professional Open Source
* Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview
* Data pump to pull data from Magnolia projectData REST end point
* and push into specified DCP REST end point.
*
* @author [email protected] (Lukas Vlcek)
*/
$(document).ready(function(){
var messageCodes = {
INFO : "class='info'",
DEBUG : "class='debug'",
ERROR : "class='error'"
};
var log = $("#log");
var startButton = $("#startButton");
startButton.bind("click", startButtonClicked);
/**
* Click handler that is bind to a start button.
*/
function startButtonClicked() {
log.empty();
indeterminateProgressBar();
var source_url = $("#source_url").val();
logMessage("Getting source data from " +source_url);
disableInputElements();
$.ajax({
dataType: "json",
url: source_url
}).fail(function(err){
logMessage("Got error: " + err);
resetProgressBar();
}).done(function(data){
logMessage("Got source data.");
processData(data);
});
}
function processData(hits) {
if (hits) {
//var hits = data.hits.hits;
var dfr = $.Deferred();
if (hits.length && hits.length > 0) {
var target_url = $("#target_url").val();
var totalHits = hits.length;
logMessage("Sending out " + totalHits + " requests...");
resetProgressBar(totalHits);
// prepare common XHR config
var ajaxconf = {
type: 'POST',
crossDomain: true,
dataType: 'JSON',
processData: false,
contentType: 'application/json'
// ,async:false
};
// setup credentials if provided
//var username = $.trim($("#username").val());
//var password = $.trim($("#password").val());
//if (username.length > 0 && password.length > 0) {
// ajaxconf.headers = { Authorization: 'Basic ' + btoa(username + ':' + password) };
ajaxconf.xhrFields = { withCredentials: true };
//}
$.each(hits, function(i, hit){
var conf = {};
$.extend(true, conf, ajaxconf);
preprocessTheDcpSysFields(hit);
conf["data"] = JSON.stringify(hit);
conf["url"] = [target_url, "/content/", hit.sys_content_type, "/", hit.sys_content_id].join('');
dfr.then(
function() {
return $.ajax(conf)
.done( function() { logMessage(["[OK] hit [", (i+1), "] for [", hit.sys_content_id, "]"].join('')); } )
.fail( function() { logMessage(["[ERROR] hit [", (i+1), "] for [", hit.sys_content_id, "]"].join(''), messageCodes.ERROR); } )
.always( function() { setProgressBar(getProgressBarValue()+1); } );
}
);
});
}
dfr.then(function(){
logMessage("Done.");
resetProgressBar();
enableInputElements();
});
dfr.resolve();
} else {
logMessage("Malformed data!");
resetProgressBar();
enableInputElements();
}
}
function preprocessTheDcpSysFields( data ) {
data.sys_content_id = data.nodePath.substring(1).replace(/\//g,"_");
data.sys_content_provider = "jbossorg";
data.sys_content_type = "jbossorg_project_info";
data.sys_description = data.description;
data.sys_id = "jbossorg_project_info-"+data.sys_content_id;
data.sys_title = data.projectName;
data.sys_type = "project_info";
data.sys_updated = new Date().toISOString();
data.sys_url_view = data.homePage || data.nodePath || data.downloadsLink || data.docsLink;
}
/**
* Indeterminate progress bar.
*/
function indeterminateProgressBar() {
var progressBar = $("#progressBar");
progressBar.attr('max',null);
progressBar.attr('value',null);
}
/**
* @return {number}
*/
function getProgressBarValue() {
var value = $("#progressBar").attr('value');
return +value; // force number type
}
/**
* Update progress bar to given value.
* @param {number} value
*/
function setProgressBar(value) {
var progressBar = $("#progressBar");
progressBar.attr('value',value);
}
/**
* Disable input elements.
*/
function disableInputElements() {
xsableInputElements_(true);
}
/**
* Enable input elements.
*/
function enableInputElements() {
xsableInputElements_(false);
}
/**
* Reset progress bar and set new max value. Defaults to 100.
* @param {number=} opt_max
*/
function resetProgressBar(opt_max) {
var max = opt_max || 100;
var progressBar = $("#progressBar");
progressBar.attr('max',max);
progressBar.attr('value',0);
}
function logMessage(message, opt_code) {
var code = opt_code || messageCodes.INFO;
log.append(["<li ", code, ">", message, "</li>"].join(''));
}
/**
* @private
*/
function xsableInputElements_(boolean) {
$.each(["#source_url", "#target_url", "#startButton"], function(i, element){
$(element).attr('disabled', boolean)
});
}
});