Project

General

Profile

1
var patchEditorControllers = angular.module('patchEditorUIControllers', []);
2

    
3
function Patch()
4
{
5
    this.operation="";
6
    this.fieldType="";
7
    this.value="";
8
    
9
}
10

    
11
patchEditorControllers.controller('patchEditorCtrl', [
12
	'$scope', '$http', '$sce', '$location', '$routeParams',
13
    function ($scope, $http, $sce, $location, $routeParams) {
14
		 $scope.navbarItems=[{"name":"Title","link":"Title"}, 
15
             {"name":"Keyword","link":"Keyword"},
16
             {"name":"LCSH", "link":"Lcsh"},
17
             {"name":"Descritpion","link":"Descritpion"},
18
             {"name":"Rights","link":"Rights"},
19
             {"name":"Digital Object","link":"DigitalObject"},
20
             {"name":"Country","link":"country"},
21
             {"name":"Year","link":"year"},
22
             {"name":"Language","link":"language"},
23
             {"name":"Colour","link":"colour"},
24
             {"name":"Sound","link":"sound"},
25
             {"name":"Gauge","link":"gauge"},
26
             {"name":"Duration","link":"duration"},
27
             {"name":"Provenance","link":"provenance"}
28
             ];
29
		$scope.recordId = $routeParams.recordId;
30
		$scope.patchedRecord = '';
31
		$scope.booleanValues = [
32
	        {
33
	            id: 'true', name: 'true'},
34
	        {
35
	            id: 'false', name: 'false'}];
36
		
37
		$scope.loadRecord = function () {
38
			$http.get('patcheditor/record?recordId=' + $scope.recordId)
39
				.success(
40
					function (data) {
41
						$scope.patchedRecord = data;
42
						$scope.repositoryId = data.repositoryId;
43
						$scope.entityType = data.entityType;
44
						$scope.rightsStatus = $scope.patchedRecord.rights.status;
45
						$scope.holder = $scope.patchedRecord.rights.holder;
46
						$scope.holderUrl = $scope.patchedRecord.rights.holderUrl;
47
						if ($scope.entityType=='avcreation') {
48
							$scope.originalRecords={
49
							        'rights':{"status":$scope.patchedRecord.rights.status,"holderUrl":$scope.patchedRecord.rights.holderUrl, "holder":$scope.patchedRecord.rights.holder},
50
							        "countryOfReference":$scope.patchedRecord.countryOfReference,
51
							        "digitalObject":{"isShownAt":$scope.patchedRecord.digitalObject.isShownAt,"aggregator":$scope.patchedRecord.digitalObject.aggregator,"thumbnail":$scope.patchedRecord.digitalObject.thumbnail,"isShownBy":$scope.patchedRecord.digitalObject.isShownBy},
52
							        "productionYear":$scope.patchedRecord.productionYear,
53
							        "colour":{"text":$scope.patchedRecord.colour.text,"hasColor":$scope.patchedRecord.colour.hasColor},
54
							        "provenance":$scope.patchedRecord.provenance,        
55
							        "duration":$scope.patchedRecord.duration,
56
							        "sound":{"text":$scope.patchedRecord.sound.text,"hasSound":$scope.patchedRecord.sound.hasSound},
57
							        "gauge":$scope.patchedRecord.gauge       
58
							    };
59
						}
60
						else if ($scope.entityType=='nonavcreation') {
61
							$scope.originalRecords={
62
							        'rights':{"status":$scope.patchedRecord.rights.status,"holderUrl":$scope.patchedRecord.rights.holderUrl, "holder":$scope.patchedRecord.rights.holder},
63
							        "digitalObject":{"isShownAt":$scope.patchedRecord.digitalObject.isShownAt,"aggregator":$scope.patchedRecord.digitalObject.aggregator,"thumbnail":$scope.patchedRecord.digitalObject.thumbnail,"isShownBy":$scope.patchedRecord.digitalObject.isShownBy},
64
							        "colour":{"text":$scope.patchedRecord.colour.text,"hasColor":$scope.patchedRecord.colour.hasColor},
65
							        "provenance":$scope.patchedRecord.provenance,        
66
							        "gauge":$scope.patchedRecord.gauge       
67
							    };
68
						}
69
						$scope.searchPatchNotIndexed();
70
					}
71
				)
72
				.error(
73
					function () {
74
						show_notification('error', 'Cannot load record ');
75
					}
76
				);
77
		}
78
		$scope.loadRecord();
79
		
80
		$scope.searchPatchNotIndexed = function() {
81
	        $http.get('patcheditor/existPatchNotIndexed?repositoryId=' + $scope.repositoryId + '&recordId=' + $scope.recordId).success(function (data) {
82
	        	if (data == 'true') {
83
	        		$scope.existPatchNotIndexed = true;
84
	        	}
85
	        	else {
86
	        		$scope.existPatchNotIndexed = undefined;
87
	        	}
88
	        }).error(function () {
89
	            show_notification('error', 'Error searching not indexed patches ');
90
	        });
91

    
92
		}
93
		
94
		$scope.loadLanguages = function() {
95
	        $http.get('patcheditor/languages').success(function (data) {
96
	            $scope.languages = data;
97
	        }).error(function () {
98
	            show_notification('error', 'Error getting languages ');
99
	        });
100

    
101
		}		
102
		$scope.loadLanguages();
103
		
104
		$scope.loadCountry = function() {
105
	        $http.get('patcheditor/countries').success(function (data) {
106
	            $scope.countries = data;
107
	        }).error(function () {
108
	            show_notification('error', 'Error getting countries ');
109
	        });
110

    
111
		}	
112
		$scope.loadCountry();
113
		
114
		$scope.loadTitleTypes = function() {
115
	        $http.get('patcheditor/titleTypes').success(function (data) {
116
	            $scope.titleTypes = data;
117
	        }).error(function () {
118
	            show_notification('error', 'Error getting titleTypes ');
119
	        });
120

    
121
		}		
122
		$scope.loadTitleTypes();
123
		
124
		$scope.loadKeywordTypes = function() {
125
	        $http.get('patcheditor/keywordTypes').success(function (data) {
126
	            $scope.keywordTypes = data;
127
	        }).error(function () {
128
	            show_notification('error', 'Error getting keywordTypes ');
129
	        });
130

    
131
		}		
132
		$scope.loadKeywordTypes();
133
		
134
		$scope.loadLSCH = function() {
135
	        $http.get('patcheditor/lsch').success(function (data) {
136
	            $scope.lsch = data;
137
	        }).error(function () {
138
	            show_notification('error', 'Error getting LSCH ');
139
	        });
140

    
141
		}		
142
		$scope.loadLSCH();
143
		
144
		$scope.loadDescriptionTypes = function() {
145
	        $http.get('patcheditor/descriptionTypes').success(function (data) {
146
	            $scope.descriptionTypes = data;
147
	        }).error(function () {
148
	            show_notification('error', 'Error getting descriptionTypes ');
149
	        });
150

    
151
		}		
152
		$scope.loadDescriptionTypes();
153
		
154
		
155
		$scope.loadRightsStatus = function() {
156
	        $http.get('patcheditor/rightsStatus').success(function (data) {
157
	            $scope.rightsStatusValues = data;
158
	        }).error(function () {
159
	            show_notification('error', 'Error getting RightsStatus ');
160
	        });
161

    
162
		}		
163
		$scope.loadRightsStatus();
164
		
165
		$scope.loadLanguageUsages = function() {
166
	        $http.get('patcheditor/languageUsages').success(function (data) {
167
	            $scope.languageUsages = data;
168
	        }).error(function () {
169
	            show_notification('error', 'Error getting LanguageUsages ');
170
	        });
171

    
172
		}		
173
		$scope.loadLanguageUsages();
174
		
175
		
176
		$scope.loadColours = function() {
177
	        $http.get('patcheditor/colours').success(function (data) {
178
	            $scope.colours = data;
179
	        }).error(function () {
180
	            show_notification('error', 'Error getting Colours ');
181
	        });
182

    
183
		}		
184
		$scope.loadColours();		
185

    
186
		$scope.loadSounds = function() {
187
	        $http.get('patcheditor/sounds').success(function (data) {
188
	            $scope.sounds = data;
189
	        }).error(function () {
190
	            show_notification('error', 'Error getting Sounds ');
191
	        });
192

    
193
		}		
194
		$scope.loadSounds();
195
		
196
		$scope.loadGauge = function() {
197
	        $http.get('patcheditor/gauge').success(function (data) {
198
	            $scope.gauge = data;
199
	        }).error(function () {
200
	            show_notification('error', 'Error getting Gauge ');
201
	        });
202

    
203
		}		
204
		$scope.loadGauge();
205
		
206
		$scope.addTitle=function()
207
	    {
208
	        var NewRecord=[{"text":"","language":"","type":"", "edited":"true"}];
209
	        NewRecord=NewRecord.concat($scope.patchedRecord.titles);        
210
	        $scope.patchedRecord.titles=NewRecord;
211
	    }
212
		
213
		$scope.removeTitle=function(id)
214
	    {
215
		    var titles=$scope.patchedRecord.titles;
216
	        titles.splice(id,1);       
217
	    }
218
		
219
		$scope.addKeyword=function()
220
	    {
221
	        var NewRecord=[{"id":"","term":"","type":"", "edited":"true"}];
222
	        NewRecord=NewRecord.concat($scope.patchedRecord.keywords);        
223
	        $scope.patchedRecord.keywords=NewRecord;        
224
	    }
225
	    
226
	    $scope.removeKeyword=function(id)
227
	    {
228
	        
229
	        var keywords=$scope.patchedRecord.keywords;
230
	        keywords.splice(id,1);       
231
	    }
232
	    
233
	    $scope.addLCSH=function()
234
	    {
235
	        var NewRecord=[{"id":" ","term":"","type":"", "edited":"true"}];
236
	        NewRecord=NewRecord.concat($scope.patchedRecord.keywords);        
237
	        $scope.patchedRecord.keywords=NewRecord;      
238
	    }
239
	    
240
	    $scope.addDescription=function()
241
	    {
242
	        var NewRecord=[{"text":"","language":"","type":"", "edited":"true"}];
243
	        NewRecord=NewRecord.concat($scope.patchedRecord.descriptions);        
244
	        $scope.patchedRecord.descriptions=NewRecord;      
245
	    }
246
	    
247
	    $scope.removeDescription=function(id)
248
	    {
249
	        
250
	        var Descriptions=$scope.patchedRecord.descriptions;
251
	        Descriptions.splice(id,1);       
252
	    }
253
	    
254
	    $scope.addLanguage=function()
255
	    {
256
	        var NewRecord=[{"text":"","usage":"", "edited":"true"}];
257
	        NewRecord=NewRecord.concat($scope.patchedRecord.languages);        
258
	        $scope.patchedRecord.languages=NewRecord;      
259
	    }
260
	    
261
	    $scope.removeLanguage=function(id)
262
	    {
263
	        
264
	        var languages=$scope.patchedRecord.languages;
265
	        languages.splice(id,1);       
266
	    }
267
		
268
		$scope.commit=function()
269
	    {
270
			var patches = $scope.searchPatch ();
271
	        if (patches.length >0)
272
	    	{
273
	        	$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
274
				$http.post('applyPatches.do', $.param({
275
					'id': $scope.recordId,
276
					'patches': JSON.stringify(patches)
277
				}))
278
					.success(function (data) {
279
						show_notification('success', 'Patch applied ');
280
						$scope.searchPatchNotIndexed();
281
					}).error(function (error) {
282
						show_notification('error', 'Error applying patches: ' + error.message);
283
				});
284
	        }
285
	        else {
286
	        	show_notification('info', 'No patches to commit');
287
	        }
288
	    }
289
		
290
		$scope.getLCSHObject=function(id)
291
	    {
292
	        for(var i=0,j=$scope.lsch.length; i<j; i++){
293
	          if ($scope.lsch[i].id==id)
294
	            return $scope.lsch[i];
295
	        };
296
	    }
297
		
298
		$scope.searchPatch=function()
299
	    {
300
	        var patches=[];   
301
	        
302
	        for(var i=0,j=$scope.patchedRecord.titles.length; i<j; i++)
303
	        {
304
		          var currentTitle =$scope.patchedRecord.titles[i];
305
		          if (currentTitle.edited!=null)
306
		          {
307
		              if ((currentTitle.text!=null) || (currentTitle.text.length))
308
		              {
309
		                  var tmp= new Patch();
310
		                  tmp.fieldType="title"
311
		                  tmp.operation="ADDSUBTREE"
312
		                  tmp.value=currentTitle.text+"::-::"+currentTitle.type+"::-::"+currentTitle.language;
313
		                  if (currentTitle.text.length)
314
		                    patches.push(tmp);                  
315
		              }
316
		          }         
317
		        }
318
	        
319
	        for(var i=0,j=$scope.patchedRecord.keywords.length; i<j; i++)
320
	        {
321
	          var currentkeyword =$scope.patchedRecord.keywords[i];
322
	          if (currentkeyword.edited!=null)
323
	          {
324
	          	   var tmp= new Patch();
325
	              	tmp.fieldType="keyword"
326
	              	tmp.operation="ADDSUBTREE"
327
	              	if(currentkeyword.id.length)
328
	              	{ 	
329
	                  var currentLSCHterm= $scope.getLCSHObject (currentkeyword.term);
330
	                  tmp.value=currentLSCHterm.id+"::-::"+currentLSCHterm.term+"::-::"+currentLSCHterm.URL+"::-::LCSH::-::EN";
331
	                  patches.push(tmp); 
332
	                 }                  
333
	                 else
334
	              		{
335
	                  	tmp.value="undef::-::"+currentkeyword.term+"::-::undef"+"::-::"+currentkeyword.type +"::-::"+currentkeyword.language;  
336
	                  	if (currentkeyword.term.length)
337
	                    patches.push(tmp); 
338
	              		}    
339
	              	
340
	          }         
341
	        }
342
	        
343
	        for(var i=0,j=$scope.patchedRecord.descriptions.length; i<j; i++){
344
	          var currentDescriptions =$scope.patchedRecord.descriptions[i];
345
	          if (currentDescriptions.edited!=null)
346
	          {
347
	              if ((currentDescriptions.text!=null) || (currentDescriptions.text.length))
348
	              {
349
	                  var tmp= new Patch();
350
	                  tmp.fieldType="description"
351
	                  tmp.operation="ADDSUBTREE"
352
	                  tmp.value=currentDescriptions.text+"::-::"+currentDescriptions.type+"::-::"+currentDescriptions.language;
353
	                  if (currentDescriptions.text.length)
354
	                    patches.push(tmp);                  
355
	              }
356
	          }         
357
	        }
358
	        
359
	        if ($scope.originalRecords.digitalObject.isShownAt != $scope.patchedRecord.digitalObject.isShownAt)
360
	        {
361
	            var tmp= new Patch();
362
	            tmp.fieldType="isShownAt";
363
	            if ($scope.patchedRecord.digitalObject.isShownAt==null)
364
	                tmp.operation="DELETENODE"
365
	            else
366
	                tmp.operation="EDIT";                           
367
	            tmp.value= $scope.patchedRecord.digitalObject.isShownAt;
368
	            patches.push(tmp);             
369
	        }
370
	        
371
	        if ($scope.originalRecords.digitalObject.isShownBy != $scope.patchedRecord.digitalObject.isShownBy)
372
	        {
373
	            var tmp= new Patch();
374
	            tmp.fieldType="isShownBy";
375
	            if ($scope.patchedRecord.digitalObject.isShownBy==null)
376
	                tmp.operation="DELETENODE"
377
	            else
378
	                tmp.operation="EDIT";                           
379
	            tmp.value= $scope.patchedRecord.digitalObject.isShownBy;
380
	            patches.push(tmp);             
381
	        }
382
	        
383
	        if ($scope.originalRecords.digitalObject.aggregator != $scope.patchedRecord.digitalObject.aggregator)
384
	        {
385
	            var tmp= new Patch();
386
	            tmp.fieldType="aggregator";
387
	            if (($scope.patchedRecord.digitalObject.aggregator==null)|| (!$scope.patchedRecord.digitalObject.aggregator.length) )
388
	                tmp.operation="DELETENODE"
389
	            else
390
	                tmp.operation="EDIT";                           
391
	            tmp.value= $scope.patchedRecord.digitalObject.aggregator;
392
	            patches.push(tmp);             
393
	        }
394
	        
395
	        if ($scope.originalRecords.digitalObject.thumbnail != $scope.patchedRecord.digitalObject.thumbnail)
396
	        {
397
	            var tmp= new Patch();
398
	            tmp.fieldType="thumbnail";
399
	            if (($scope.patchedRecord.digitalObject.thumbnail==null) || (!$scope.patchedRecord.digitalObject.thumbnail.length))
400
	                tmp.operation="DELETENODE"
401
	            else
402
	                tmp.operation="EDIT";                           
403
	            tmp.value= $scope.patchedRecord.digitalObject.thumbnail;
404
	            patches.push(tmp);             
405
	        }
406
	        
407
	        if ($scope.originalRecords.rights.status != $scope.patchedRecord.rights.status)
408
	        {
409
	            var tmp= new Patch();
410
	            tmp.fieldType="rightsStatus";
411
	            if (($scope.patchedRecord.rights.status==null) || (!$scope.patchedRecord.rights.status.length))
412
	                tmp.operation="DELETENODE"
413
	            else
414
	                tmp.operation="EDIT";                           
415
	            tmp.value= $scope.patchedRecord.rights.status;
416
	            patches.push(tmp);             
417
	        }
418
	        
419
	        if (($scope.originalRecords.rights.holderUrl != $scope.patchedRecord.rights.holderUrl) | ($scope.originalRecords.rights.holder != $scope.patchedRecord.rights.holder) )
420
	        {
421
	            var tmp= new Patch();
422
	            tmp.fieldType="rightsholderUrl";
423
	            if ( (($scope.patchedRecord.rights.holderUrl==null) || (!$scope.patchedRecord.rights.holderUrl.length)) && (($scope.patchedRecord.rights.holder==null) || (!$scope.patchedRecord.rights.holder.length)))
424
	            {	            	
425
	                tmp.operation="DELETENODE"
426
	            }
427
	            else
428
	                tmp.operation="EDIT";
429
	                
430
	            var mholder=$scope.patchedRecord.rights.holder;
431
	            if ((mholder== null) | (!mholder.length))
432
	            	mholder= "undef"
433
	            var mholderUrl=$scope.patchedRecord.rights.holderUrl;
434
	            if ((mholderUrl== null) | (!mholderUrl.length))
435
	            	mholderUrl= "undef"
436
	                
437
	                                        
438
	            tmp.value= mholder+"::-::"+mholderUrl;
439
	            patches.push(tmp);             
440
	        }
441
	        
442
	        if ($scope.entityType=='avcreation') {
443
		        if ($scope.originalRecords.countryOfReference != $scope.patchedRecord.countryOfReference)
444
		        {
445
		            var tmp= new Patch();
446
		            tmp.fieldType="countryOfReference";
447
		            if ($scope.patchedRecord.countryOfReference==null)
448
		                tmp.operation="DELETENODE"
449
		            else
450
		                tmp.operation="EDIT";                           
451
		            tmp.value= $scope.patchedRecord.countryOfReference;
452
		            patches.push(tmp);             
453
		        }
454
	        }
455
	
456
	        if ($scope.entityType=='avcreation') {
457
		        if ($scope.originalRecords.productionYear != $scope.patchedRecord.productionYear)
458
		        {
459
		            var tmp= new Patch();
460
		            tmp.fieldType="productionYear";
461
		            if ($scope.patchedRecord.productionYear==null)
462
		                tmp.operation="DELETENODE"
463
		            else
464
		                tmp.operation="EDIT";                           
465
		            tmp.value= $scope.patchedRecord.productionYear;
466
		            patches.push(tmp);             
467
		        }
468
	        }
469
	 
470
	        for(var i=0,j=$scope.patchedRecord.languages.length; i<j; i++){
471
	          var currentLanguages =$scope.patchedRecord.languages[i];
472
	          if (currentLanguages.edited!=null)
473
	          {
474
	              if ((currentLanguages.text!=null) || (currentLanguages.text.length))
475
	              {
476
	                  var tmp= new Patch();
477
	                  tmp.fieldType="language"
478
	                  tmp.operation="ADDSUBTREE"
479
	                  tmp.value=currentLanguages.text+"::-::"+currentLanguages.usage;
480
	                  if (currentLanguages.text.length)
481
	                    patches.push(tmp);                  
482
	              }
483
	          }         
484
	        }
485
	      
486
	        if (($scope.originalRecords.colour.text != $scope.patchedRecord.colour.text)|| ($scope.originalRecords.colour.hasColor != $scope.patchedRecord.colour.hasColor)  )
487
	        {
488
	            var tmp= new Patch();
489
	            tmp.fieldType="colour";
490
	            if ($scope.patchedRecord.colour.text==null)
491
	                tmp.operation="DELETENODE";
492
	            else
493
	                tmp.operation="EDIT";                           
494
	            tmp.value= $scope.patchedRecord.colour.text+"::-::"+ $scope.patchedRecord.colour.hasColor;
495
	            patches.push(tmp);             
496
	        }  
497
	        
498
	        if ($scope.entityType=='avcreation') {
499
		        if (($scope.originalRecords.sound.text != $scope.patchedRecord.sound.text)|| ($scope.originalRecords.sound.hasSound != $scope.patchedRecord.sound.hasSound)  )
500
		        {
501
		            var tmp= new Patch();
502
		            if ($scope.patchedRecord.sound.text==null)
503
		                tmp.operation="DELETENODE";
504
		            else
505
		                tmp.operation="EDIT";
506
		            tmp.fieldType="sound";                                       
507
		            tmp.value= $scope.patchedRecord.sound.text+"::-::"+ $scope.patchedRecord.sound.hasSound;
508
		            patches.push(tmp);             
509
		        }
510
	        }
511
	        if ($scope.originalRecords.gauge != $scope.patchedRecord.gauge)
512
	        {
513
	            var tmp= new Patch();
514
	            tmp.fieldType="gauge";
515
	            if($scope.patchedRecord.gauge==null)
516
	                tmp.operation="DELETENODE"
517
	            else 
518
	                tmp.operation="EDIT";                           
519
	            tmp.value= $scope.patchedRecord.gauge;
520
	            patches.push(tmp);             
521
	        }
522
	        
523
	        if ($scope.entityType=='avcreation') {
524
		        if ($scope.originalRecords.duration != $scope.patchedRecord.duration)
525
		        {
526
		            var tmp= new Patch();
527
		            tmp.fieldType="duration";
528
		            if ($scope.patchedRecord.duration==null)
529
		                tmp.operation="DELETENODE"
530
		            else
531
		                tmp.operation="EDIT";                           
532
		            tmp.value= $scope.patchedRecord.duration;
533
		            patches.push(tmp);             
534
		        }
535
	        }
536
	        if ($scope.originalRecords.provenance != $scope.patchedRecord.provenance)
537
	        {
538
	            var tmp= new Patch();
539
	            tmp.fieldType="provenance";
540
	            if ($scope.patchedRecord.provenance==null)
541
	                tmp.operation="DELETENODE"
542
	            else
543
	                tmp.operation="EDIT";                           
544
	            tmp.value= $scope.patchedRecord.provenance;
545
	            patches.push(tmp);             
546
	        }
547
	        return patches;             
548
	    }
549
		
550
		
551
	}
552
]);
(1-1/2)