-
Notifications
You must be signed in to change notification settings - Fork 460
/
BrowserAjax.java
1688 lines (1428 loc) · 67.7 KB
/
BrowserAjax.java
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.dotmarketing.portlets.browser.ajax;
import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.beans.Inode;
import com.dotmarketing.beans.WebAsset;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.PermissionAPI;
import com.dotmarketing.business.Role;
import com.dotmarketing.business.util.HostNameComparator;
import com.dotmarketing.business.web.HostWebAPI;
import com.dotmarketing.business.web.UserWebAPI;
import com.dotmarketing.business.web.WebAPILocator;
import com.dotmarketing.cache.StructureCache;
import com.dotmarketing.db.HibernateUtil;
import com.dotmarketing.exception.*;
import com.dotmarketing.factories.InodeFactory;
import com.dotmarketing.factories.PublishFactory;
import com.dotmarketing.factories.WebAssetFactory;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo;
import com.dotmarketing.portlets.fileassets.business.FileAsset;
import com.dotmarketing.portlets.fileassets.business.FileAssetAPI;
import com.dotmarketing.portlets.files.business.FileAPI;
import com.dotmarketing.portlets.files.model.File;
import com.dotmarketing.portlets.folders.business.FolderAPI;
import com.dotmarketing.portlets.folders.model.Folder;
import com.dotmarketing.portlets.htmlpages.business.HTMLPageAPI;
import com.dotmarketing.portlets.htmlpages.factories.HTMLPageFactory;
import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
import com.dotmarketing.portlets.links.factories.LinkFactory;
import com.dotmarketing.portlets.links.model.Link;
import com.dotmarketing.portlets.structure.model.Structure;
import com.dotmarketing.portlets.workflows.business.WorkflowAPI;
import com.dotmarketing.portlets.workflows.model.WorkflowAction;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.InodeUtils;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
import com.dotmarketing.viewtools.BrowserAPI;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.struts.ActionException;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import static com.dotmarketing.business.PermissionAPI.*;
/**
*
* @author david
*
*/
public class BrowserAjax {
private static PermissionAPI permissionAPI = APILocator.getPermissionAPI();
private UserWebAPI userAPI = WebAPILocator.getUserWebAPI();
private HostAPI hostAPI = APILocator.getHostAPI();
private HostWebAPI hostWebAPI = WebAPILocator.getHostWebAPI();
private FolderAPI folderAPI = APILocator.getFolderAPI();
private FileAPI fileAPI = APILocator.getFileAPI();
private HTMLPageAPI pageAPI = APILocator.getHTMLPageAPI();
private ContentletAPI contAPI = APILocator.getContentletAPI();
private WorkflowAPI workflowAPI = APILocator.getWorkflowAPI();
private BrowserAPI browserAPI = new BrowserAPI();
String activeHostId = "";
String activeFolderInode = "";
List<String> openFolders = new ArrayList<String> ();
String lastSortBy = "name";
boolean lastSortDirectionDesc = false;
/**
* @param permissionAPI the permissionAPI to set
*/
public static void setPermissionAPI(PermissionAPI permissionAPI) {
BrowserAjax.permissionAPI = permissionAPI;
}
/**
* This methods is used to load the entire tree by first time.
* @return The whole folders tree structure.
* @throws DotDataException
* @throws DotSecurityException
*/
public List<Map> getTree(String hostId) throws DotDataException, DotSecurityException {
WebContext ctx = WebContextFactory.get();
User usr = getUser(ctx.getHttpServletRequest());
User systemUser = userAPI.getSystemUser();
Role[] roles = new Role[]{};
try {
roles = com.dotmarketing.business.APILocator.getRoleAPI().loadRolesForUser(usr.getUserId()).toArray(new Role[0]);
} catch (DotDataException e1) {
Logger.error(BrowserAjax.class,e1.getMessage(),e1);
}
List<Host> hosts=new ArrayList<Host>();
if(!UtilMethods.isSet(hostId) || hostId.equals("allHosts")){
hostId = "allHosts";
hosts.addAll(hostAPI.findAll(usr, false));
}
else{
hosts.add(hostAPI.find(hostId, usr, false));
}
List<Map> retList = new ArrayList<Map>();
for (Host host : hosts) {
//Ignore system host
if(host.isSystemHost()||host.isArchived())
continue;
//Obtain maps from hosts to be returned by ajax
Map<String,Object> hostMap = (Map<String,Object>)host.getMap();
if (activeHostId.equalsIgnoreCase(host.getIdentifier())|| hosts.size()==1 ) {
hostMap.put("open", true);
List<Map> children = getFoldersTree (host, roles);
hostMap.put("childrenFolders", children);
hostMap.put("childrenFoldersCount", children.size());
} else {
hostMap.put("open", false);
// hostMap.put("childrenFoldersCount", getSubFoldersCount(host));
}
java.util.List permissions = new ArrayList();
try {
permissions = permissionAPI.getPermissionIdsFromRoles(host, roles, usr);
} catch (DotDataException e) {
Logger.error(this, "Could not load permissions : ",e);
}
hostMap.put("permissions", permissions);
retList.add(hostMap);
}
return retList;
}
/**
* Action called every time a user opens a folder using the + (left hand side)
* @param parentInode Parent folder to be opened
* @return The subtree structure of folders
* @throws SystemException
* @throws PortalException
* @throws DotSecurityException
* @throws DotDataException
*/
public List<Map> openHostTree (String hostId) throws PortalException, SystemException, DotDataException, DotSecurityException {
WebContext ctx = WebContextFactory.get();
User usr = getUser(ctx.getHttpServletRequest());
boolean respectFrontend = !userAPI.isLoggedToBackend(ctx.getHttpServletRequest());
Host host = hostAPI.find(hostId, usr, respectFrontend);
if(! UtilMethods.isSet(hostId) || host == null){
Host browseHost = hostAPI.find(hostId, APILocator.getUserAPI().getSystemUser(), respectFrontend);
if(browseHost != null){
Logger.warn(this, "User " + usr.getUserId() + " cannot browse host id " + hostId + " aka "+ browseHost.getHostname());
}
else{
Logger.warn(this, "User " + usr.getUserId() + " cannot browse host id " + hostId );
}
return new ArrayList<Map>();
}
activeHostId = hostId;
Role[] roles = new Role[]{};
try {
roles = com.dotmarketing.business.APILocator.getRoleAPI().loadRolesForUser(usr.getUserId()).toArray(new Role[0]);
} catch (DotDataException e) {
Logger.error(BrowserAjax.class,e.getMessage(),e);
}
return getFoldersTree (host, roles);
}
/**
* Action called every time a user opens a folder using the + (left hand side)
* @param parentInode Parent folder to be opened
* @return The subtree structure of folders
* @throws DotDataException
* @throws DotSecurityException
* @throws DotHibernateException
*/
public List<Map> openFolderTree (String parentInode) throws DotHibernateException, DotSecurityException, DotDataException {
WebContext ctx = WebContextFactory.get();
User usr = getUser(ctx.getHttpServletRequest());
Role[] roles = new Role[]{};
if(usr != null){
try {
roles = com.dotmarketing.business.APILocator.getRoleAPI().loadRolesForUser(usr.getUserId()).toArray(new Role[0]);
}catch (NullPointerException e) {
Logger.debug(this, "array was null");
} catch (DotDataException e) {
Logger.error(BrowserAjax.class,e.getMessage(),e);
}
}
Folder f = (Folder) APILocator.getFolderAPI().find(parentInode, usr, false);
openFolders.add(parentInode);
return getFoldersTree (f, roles);
}
/**
* Action called everytime a user closes a folder using the - (left hand side)
* @param parentInode Parent folder to be opened
* @return The subtree structure of folders
*/
public void closeFolderTree (String parentInode) {
openFolders.remove(parentInode);
}
@SuppressWarnings("unchecked")
public List<Map<String, Object>> openFolderContent (String parentInode, String sortBy, boolean showArchived) throws DotHibernateException, DotSecurityException, DotDataException {
activeFolderInode = parentInode;
this.lastSortBy = sortBy;
if (sortBy != null && UtilMethods.isSet(sortBy)) {
if (sortBy.equals(lastSortBy)) {
this.lastSortDirectionDesc = !this.lastSortDirectionDesc;
}
this.lastSortBy = sortBy;
}
Map<String, Object> resultsMap = getFolderContent(parentInode, 0, -1, "", null, null, showArchived, false, false, this.lastSortBy, this.lastSortDirectionDesc);
return (List<Map<String, Object>>) resultsMap.get("list");
}
public Map<String, Object> getFolderContent (String folderId, int offset, int maxResults, String filter, List<String> mimeTypes,
List<String> extensions, boolean showArchived, boolean noFolders, boolean onlyFiles, String sortBy, boolean sortByDesc) throws DotHibernateException, DotSecurityException, DotDataException {
WebContext ctx = WebContextFactory.get();
HttpServletRequest req = ctx.getHttpServletRequest();
User usr = getUser(req);
return browserAPI.getFolderContent(usr, folderId, offset, maxResults, filter, mimeTypes, extensions, showArchived, noFolders, onlyFiles, sortBy, sortByDesc);
}
public void saveFileAction(String selectedItem,String wfActionAssign,String wfActionId,String wfActionComments, String wfConId, String wfPublishDate,
String wfPublishTime, String wfExpireDate, String wfExpireTime, String wfNeverExpire) throws DotSecurityException, ServletException{
WebContext ctx = WebContextFactory.get();
User usr = getUser(ctx.getHttpServletRequest());
Contentlet c = null;
WorkflowAPI wapi = APILocator.getWorkflowAPI();
try {
WorkflowAction action = wapi.findAction(wfActionId, usr);
if (action == null) {
throw new ServletException("No such workflow action");
}
c = APILocator.getContentletAPI().find(wfConId, usr, false);
c.setStringProperty("wfActionId", action.getId());
c.setStringProperty("wfActionComments", wfActionComments);
c.setStringProperty("wfActionAssign", wfActionAssign);
c.setStringProperty("wfPublishDate", wfPublishDate);
c.setStringProperty("wfPublishTime", wfPublishTime);
c.setStringProperty("wfExpireDate", wfExpireDate);
c.setStringProperty("wfExpireTime", wfExpireTime);
c.setStringProperty("wfNeverExpire", wfNeverExpire);
wapi.fireWorkflowNoCheckin(c, usr);
} catch (Exception e) {
Logger.error(BrowserAjax.class, e.getMessage(), e);
throw new ServletException(e.getMessage());
}
}
public Map<String, Object> getFileInfo(String fileId) throws DotDataException, DotSecurityException, PortalException, SystemException {
WebContext ctx = WebContextFactory.get();
HttpServletRequest req = ctx.getHttpServletRequest();
ServletContext servletContext = ctx.getServletContext();
User user = userAPI.getLoggedInUser(req);
boolean respectFrontendRoles = userAPI.isLoggedToFrontend(req);
Identifier ident = APILocator.getIdentifierAPI().find(fileId);
if(ident!=null && InodeUtils.isSet(ident.getId()) && ident.getAssetType().equals("file_asset")) {
File file = fileAPI.getWorkingFileById(fileId, user, respectFrontendRoles);
String mimeType = servletContext.getMimeType(file.getFileName().toLowerCase());
Map<String, Object> fileMap = file.getMap();
fileMap.put("mimeType", mimeType);
fileMap.put("path", file.getPath());
return fileMap;
}
if(ident!=null && InodeUtils.isSet(ident.getId()) && ident.getAssetType().equals("htmlpage")) {
HTMLPage page = pageAPI.loadWorkingPageById(fileId, user, respectFrontendRoles);
Map<String, Object> pageMap = page.getMap();
pageMap.put("mimeType", "application/dotpage");
pageMap.put("pageURI", page.getURI());
return pageMap;
}
if(ident!=null && InodeUtils.isSet(ident.getId()) && ident.getAssetType().equals("contentlet")) {
ContentletVersionInfo vinfo=APILocator.getVersionableAPI().getContentletVersionInfo(ident.getId(), APILocator.getLanguageAPI().getDefaultLanguage().getId());
boolean live = respectFrontendRoles || vinfo.getLiveInode()!=null;
Contentlet cont = contAPI.findContentletByIdentifier(ident.getId(),live, APILocator.getLanguageAPI().getDefaultLanguage().getId() , user, respectFrontendRoles);
FileAsset fileAsset = APILocator.getFileAssetAPI().fromContentlet(cont);
java.io.File file = fileAsset.getFileAsset();
String mimeType = servletContext.getMimeType(file.getName().toLowerCase());
Map<String, Object> fileMap = fileAsset.getMap();
fileMap.put("mimeType", mimeType);
fileMap.put("path", fileAsset.getPath());
fileMap.put("type", "contentlet");
return fileMap;
}
return null;
}
@SuppressWarnings("unchecked")
private List<Map> getFoldersTree (Host host, Role[] roles) throws DotStateException, DotDataException, DotSecurityException {
FolderAPI folderAPI = APILocator.getFolderAPI();
List<Folder> children = new ArrayList<Folder>();
try {
children = folderAPI.findSubFolders(host,userAPI.getSystemUser(),false);
} catch (Exception e) {
Logger.error(this, "Could not load folders : ",e);
}
return getFoldersTree(host.getIdentifier(), children, roles);
}
@SuppressWarnings("unchecked")
private List<Map> getFoldersTree (Folder parent, Role[] roles) throws DotStateException, DotDataException, DotSecurityException {
FolderAPI folderAPI = APILocator.getFolderAPI();
List<Folder> children = new ArrayList<Folder>();
try {
children = folderAPI.findSubFolders(parent,userAPI.getSystemUser(),false);
} catch (Exception e) {
Logger.error(this, "Could not load folders : ",e);
}
return getFoldersTree(parent.getInode(), children, roles);
}
private List<Map> getFoldersTree (String parentInode, List<Folder> children, Role[] roles) throws DotStateException, DotDataException, DotSecurityException {
WebContext ctx = WebContextFactory.get();
User usr = getUser(ctx.getHttpServletRequest());
ArrayList<Map> folders = new ArrayList<Map> ();
for (Folder f : children) {
Map<String, Object> folderMap = f.getMap();
if (openFolders.contains(f.getInode())) {
List<Map> childrenMaps = getFoldersTree (f, roles);
folderMap.put("open", true);
folderMap.put("childrenFolders", childrenMaps);
} else {
folderMap.put("open", false);
}
if(f.getInode().equalsIgnoreCase(activeFolderInode))
folderMap.put("selected", true);
else
folderMap.put("selected", false);
folderMap.put("parent", parentInode);
List permissions = new ArrayList();
try {
permissions = permissionAPI.getPermissionIdsFromRoles(f, roles, usr);
} catch (DotDataException e) {
Logger.error(this, "Could not load permissions : ",e);
}
folderMap.put("permissions", permissions);
folders.add(folderMap);
}
return folders;
}
public Map<String, Object> renameFolder (String inode, String newName) throws DotDataException, DotSecurityException {
WebContext ctx = WebContextFactory.get();
User usr = getUser(ctx.getHttpServletRequest());
HashMap<String, Object> result = new HashMap<String, Object> ();
Folder folder = APILocator.getFolderAPI().find(inode, usr, false);
result.put("lastName", folder.getName());
result.put("extension", "");
result.put("newName", newName);
result.put("inode", folder.getInode());
try {
if (folderAPI.renameFolder(folder, newName,usr,false)) {
result.put("result", 0);
} else {
result.put("result", 1);
result.put("errorReason", "There is another folder that has the same name");
}
} catch (Exception e) {
Logger.error(this, "Problem occured in the method renameFolder: ",e);
}
return result;
}
/**
* Copies a given inode folder/host reference into another given folder
*
* @param inode folder inode
* @param newFolder This could be the inode of a folder or a host
* @return Confirmation message
* @throws Exception
*/
public boolean copyFolder ( String inode, String newFolder ) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser( req );
UserWebAPI userWebAPI = WebAPILocator.getUserWebAPI();
HostAPI hostAPI = APILocator.getHostAPI();
//Searching for the folder to copy
Folder folder = APILocator.getFolderAPI().find( inode, user, false );
if ( !folderAPI.exists( newFolder ) ) {
Host parentHost = hostAPI.find( newFolder, user, !userWebAPI.isLoggedToBackend( req ) );
if ( !permissionAPI.doesUserHavePermission( folder, PERMISSION_WRITE, user ) || !permissionAPI.doesUserHavePermission( parentHost, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( "The user doesn't have the required permissions." );
}
folderAPI.copy( folder, parentHost, user, false );
refreshIndex(null, null, user, parentHost, folder );
} else {
Folder parentFolder = APILocator.getFolderAPI().find( newFolder, user, false );
if ( !permissionAPI.doesUserHavePermission( folder, PermissionAPI.PERMISSION_WRITE, user ) || !permissionAPI.doesUserHavePermission( parentFolder, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( "The user doesn't have the required permissions." );
}
if ( parentFolder.getInode().equalsIgnoreCase( folder.getInode() ) ) {
//Trying to move a folder over itself
return false;
}
if ( folderAPI.isChildFolder( parentFolder, folder ) ) {
//Trying to move a folder over one of its children
return false;
}
folderAPI.copy( folder, parentFolder, user, false );
refreshIndex(null, parentFolder, user, null, folder );
}
return true;
}
/**
* Moves a given inode folder/host reference into another given folder
*
* @param inode folder inode
* @param newFolder This could be the inode of a folder or a host
* @return Confirmation message
* @throws Exception
*/
public boolean moveFolder ( String inode, String newFolder ) throws Exception {
HibernateUtil.startTransaction();
try {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser( req );
boolean respectFrontendRoles = !userAPI.isLoggedToBackend( req );
//Searching for the folder to move
Folder folder = APILocator.getFolderAPI().find( inode, user, false );
if ( !folderAPI.exists( newFolder ) ) {
Host parentHost = hostAPI.find( newFolder, user, respectFrontendRoles );
if ( !permissionAPI.doesUserHavePermission( folder, PERMISSION_WRITE, user ) || !permissionAPI.doesUserHavePermission( parentHost, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( "The user doesn't have the required permissions." );
}
if ( !folderAPI.move( folder, parentHost, user, respectFrontendRoles ) ) {
//A folder with the same name already exists on the destination
return false;
}
refreshIndex(null, null, user, parentHost, folder );
} else {
Folder parentFolder = APILocator.getFolderAPI().find( newFolder, user, false );
if ( !permissionAPI.doesUserHavePermission( folder, PERMISSION_WRITE, user ) || !permissionAPI.doesUserHavePermission( parentFolder, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( "The user doesn't have the required permissions." );
}
if ( parentFolder.getInode().equalsIgnoreCase( folder.getInode() ) ) {
//Trying to move a folder over itself
return false;
}
if ( folderAPI.isChildFolder( parentFolder, folder ) ) {
//Trying to move a folder over one of its children
return false;
}
if ( !folderAPI.move( folder, parentFolder, user, respectFrontendRoles ) ) {
//A folder with the same name already exists on the destination
return false;
}
refreshIndex(null, parentFolder, user, null, folder );
APILocator.getPermissionAPI().resetPermissionReferences(folder);
}
} catch ( Exception e ) {
HibernateUtil.rollbackTransaction();
} finally {
HibernateUtil.commitTransaction();
}
return true;
}
public Map<String, Object> renameFile (String inode, String newName) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = null;
try {
user = com.liferay.portal.util.PortalUtil.getUser(req);
} catch (Exception e) {
Logger.error(this, "Error trying to obtain the current liferay user from the request.", e);
throw new DotRuntimeException ("Error trying to obtain the current liferay user from the request.");
}
HashMap<String, Object> result = new HashMap<String, Object> ();
Identifier id = APILocator.getIdentifierAPI().findFromInode(inode);
if(id!=null && id.getAssetType().equals("contentlet")){
Contentlet cont = APILocator.getContentletAPI().find(inode, user, false);
result.put("lastName", cont.get(FileAssetAPI.FILE_NAME_FIELD));
result.put("extension", UtilMethods.getFileExtension(cont.getStringProperty(FileAssetAPI.FILE_NAME_FIELD)));
result.put("newName", newName);
result.put("inode", inode);
if(!cont.isLocked()){
if(APILocator.getFileAssetAPI().renameFile(cont, newName, user, false)){
result.put("result", 0);
}else{
result.put("result", 1);
result.put("errorReason", "Another file with the same name already exists on this folder");
}
}else{
result.put("result", 1);
result.put("errorReason", "The file is locked");
}
}else{
File file = (File) InodeFactory.getInode(inode, File.class);
result.put("lastName", file.getNameOnly());
result.put("extension", file.getExtension());
result.put("newName", newName);
result.put("inode", inode);
if (APILocator.getFileAPI().renameFile(file, newName, user, false)) {
result.put("result", 0);
} else {
result.put("result", 1);
if (file.isLocked())
result.put("errorReason", "The file is locked");
else
result.put("errorReason", "Another file with the same name already exists on this folder");
}
}
return result;
}
/**
* Copies a given inode reference to a given folder
*
* @param inode Contentlet inode
* @param newFolder This could be the inode of a folder or a host
* @return Confirmation message
* @throws Exception
*/
public String copyFile ( String inode, String newFolder ) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser( req );
//Contentlet file identifier
Identifier id = APILocator.getIdentifierAPI().findFromInode( inode );
// gets folder parent
Folder parent = null;
try {
parent = APILocator.getFolderAPI().find( newFolder, user, false );
} catch ( Exception ignored ) {
//Probably what we have here is a host
}
Host host = null;
if ( parent == null ) {//If we didn't find a parent folder lets verify if this is a host
host = APILocator.getHostAPI().find( newFolder, user, false );
}
// Checking permissions
String permissionsError = "File-failed-to-copy-check-you-have-the-required-permissions";
if ( !permissionAPI.doesUserHavePermission( id, PERMISSION_WRITE, user ) ) {
return permissionsError;
} else if ( parent != null && !permissionAPI.doesUserHavePermission( parent, PERMISSION_WRITE, user ) ) {
return permissionsError;
} else if ( host != null && !permissionAPI.doesUserHavePermission( host, PERMISSION_WRITE, user ) ) {
return permissionsError;
}
if ( id != null && id.getAssetType().equals( "contentlet" ) ) {
//Getting the contentlet file
Contentlet cont = APILocator.getContentletAPI().find( inode, user, false );
if ( parent != null ) {
FileAsset fileAsset = APILocator.getFileAssetAPI().fromContentlet( cont );
if ( UtilMethods.isSet( fileAsset.getFileName() ) && !folderAPI.matchFilter( parent, fileAsset.getFileName() ) ) {
return "message.file_asset.error.filename.filters";
}
}
if ( parent != null ) {
APILocator.getContentletAPI().copyContentlet( cont, parent, user, false );
} else {
APILocator.getContentletAPI().copyContentlet( cont, host, user, false );
}
// issues/1788
// issues/1967
Folder srcFolder = APILocator.getFolderAPI().find(cont.getFolder(),user,false);
refreshIndex(null, parent, user, host, srcFolder );
return "File-copied";
}
File file = (File) InodeFactory.getInode( inode, File.class );
// CHECK THE FOLDER PATTERN //DOTCMS-6017
if ( UtilMethods.isSet( file.getFileName() ) && (parent != null && !folderAPI.matchFilter( parent, file.getFileName() )) ) {
return "message.file_asset.error.filename.filters";
}
// Checking permissions
if ( !permissionAPI.doesUserHavePermission( file, PERMISSION_WRITE, user ) ) {
return "File-failed-to-copy-check-you-have-the-required-permissions";
}
refreshIndex(file, parent, user, host, null );
if ( parent != null ) {
APILocator.getFileAPI().copyFile( file, parent, user, false );
} else {
APILocator.getFileAPI().copyFile( file, host, user, false );
}
return "File-copied";
}
/**
* Moves a given inode reference to a given folder
*
* @param inode Contentlet inode
* @param folder This could be the inode of a folder or a host
* @return true if success, false otherwise
* @throws Exception
*/
public boolean moveFile ( String inode, String folder ) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser( req );
//Contentlet file identifier
Identifier id = APILocator.getIdentifierAPI().findFromInode( inode );
// gets folder parent
Folder parent = null;
try {
parent = APILocator.getFolderAPI().find( folder, user, false );
} catch ( Exception ignored ) {
//Probably what we have here is a host
}
Host host = null;
if ( parent == null ) {//If we didn't find a parent folder lets verify if this is a host
host = APILocator.getHostAPI().find( folder, user, false );
}
// Checking permissions
if ( !permissionAPI.doesUserHavePermission( id, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( "The user doesn't have the required permissions." );
} else if ( parent != null && !permissionAPI.doesUserHavePermission( parent, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( "The user doesn't have the required permissions." );
} else if ( host != null && !permissionAPI.doesUserHavePermission( host, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( "The user doesn't have the required permissions." );
}
if ( id != null && id.getAssetType().equals( "contentlet" ) ) {
//Getting the contentlet file
Contentlet contentlet = APILocator.getContentletAPI().find( inode, user, false );
Folder srcFolder = APILocator.getFolderAPI().find(contentlet.getFolder(),user,false);
refreshIndex(null, parent, user, host, srcFolder );
if ( parent != null ) {
return APILocator.getFileAssetAPI().moveFile( contentlet, parent, user, false );
} else {
return APILocator.getFileAssetAPI().moveFile( contentlet, host, user, false );
}
}
File file = (File) InodeFactory.getInode( inode, File.class );
// Checking permissions
if ( !permissionAPI.doesUserHavePermission( file, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( "The user doesn't have the required permissions." );
}
refreshIndex(file, parent, user, host, null );
if ( parent != null ) {
return APILocator.getFileAPI().moveFile( file, parent, user, false );
} else {
return APILocator.getFileAPI().moveFile( file, host, user, false );
}
}
public Map<String, Object> renameHTMLPage (String inode, String newName) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser(req);
HashMap<String, Object> result = new HashMap<String, Object> ();
HTMLPage page = (HTMLPage) InodeFactory.getInode(inode, HTMLPage.class);
String pageURL = page.getPageUrl();
result.put("lastName", pageURL.substring(0, pageURL.lastIndexOf(".")));
result.put("extension", Config.getStringProperty("VELOCITY_PAGE_EXTENSION"));
result.put("newName", newName);
result.put("inode", inode);
if (HTMLPageFactory.renameHTMLPage(page, newName, user)) {
result.put("result", 0);
} else {
result.put("result", 1);
if (page.isLocked())
result.put("errorReason", "The page is locked");
else
result.put("errorReason", "Another page with the same name already exists on this folder");
}
return result;
}
/**
* Copies a given inode HTMLPage to a given folder
*
* @param inode HTMLPage inode
* @param newFolder This could be the inode of a folder or a host
* @return true if success, false otherwise
* @throws Exception
*/
public boolean copyHTMLPage ( String inode, String newFolder ) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser( req );
HTMLPage page = (HTMLPage) InodeFactory.getInode( inode, HTMLPage.class );
// gets folder parent
Folder parent = null;
try {
parent = APILocator.getFolderAPI().find( newFolder, user, false );
} catch ( Exception ignored ) {
//Probably what we have here is a host
}
Host host = null;
if ( parent == null ) {//If we didn't find a parent folder lets verify if this is a host
host = APILocator.getHostAPI().find( newFolder, user, false );
}
// Checking permissions
String permissionsError = "The user doesn't have the required permissions.";
if ( !permissionAPI.doesUserHavePermission( page, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
} else if ( parent != null && !permissionAPI.doesUserHavePermission( parent, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
} else if ( host != null && !permissionAPI.doesUserHavePermission( host, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
}
if ( parent != null ) {
HTMLPageFactory.copyHTMLPage( page, parent );
} else {
HTMLPageFactory.copyHTMLPage( page, host );
}
return true;
}
/**
* Moves a given inode HTMLPage to a given folder
*
* @param inode HTMLPage inode
* @param newFolder This could be the inode of a folder or a host
* @return true if success, false otherwise
* @throws Exception
*/
public boolean moveHTMLPage ( String inode, String newFolder ) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser( req );
HTMLPage page = (HTMLPage) InodeFactory.getInode( inode, HTMLPage.class );
// gets folder parent
Folder parent = null;
try {
parent = APILocator.getFolderAPI().find( newFolder, user, false );
} catch ( Exception ignored ) {
//Probably what we have here is a host
}
Host host = null;
if ( parent == null ) {//If we didn't find a parent folder lets verify if this is a host
host = APILocator.getHostAPI().find( newFolder, user, false );
}
// Checking permissions
String permissionsError = "The user doesn't have the required permissions.";
if ( !permissionAPI.doesUserHavePermission( page, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
} else if ( parent != null && !permissionAPI.doesUserHavePermission( parent, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
} else if ( host != null && !permissionAPI.doesUserHavePermission( host, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
}
if ( parent != null ) {
return HTMLPageFactory.moveHTMLPage( page, parent );
} else {
return HTMLPageFactory.moveHTMLPage( page, host );
}
}
public Map<String, Object> renameLink (String inode, String newName) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = null;
try {
user = com.liferay.portal.util.PortalUtil.getUser(req);
} catch (Exception e) {
Logger.error(this, "Error trying to obtain the current liferay user from the request.", e);
throw new DotRuntimeException ("Error trying to obtain the current liferay user from the request.");
}
HashMap<String, Object> result = new HashMap<String, Object> ();
Link link = (Link) InodeFactory.getInode(inode, Link.class);
String oldName = link.getTitle();
result.put("lastName", oldName);
result.put("extension", "");
result.put("newName", newName);
result.put("inode", inode);
if (LinkFactory.renameLink(link, newName, user)) {
result.put("result", 0);
} else {
result.put("result", 1);
if (link.isLocked())
result.put("errorReason", "The link is locked");
else
result.put("errorReason", "Another link with the same name already exists on this folder");
}
return result;
}
/**
* Copies a given inode Link to a given folder
*
* @param inode Link inode
* @param newFolder This could be the inode of a folder or a host
* @return true if success, false otherwise
* @throws Exception
*/
public boolean copyLink ( String inode, String newFolder ) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser( req );
Link link = (Link) InodeFactory.getInode( inode, Link.class );
// gets folder parent
Folder parent = null;
try {
parent = APILocator.getFolderAPI().find( newFolder, user, false );
} catch ( Exception ignored ) {
//Probably what we have here is a host
}
Host host = null;
if ( parent == null ) {//If we didn't find a parent folder lets verify if this is a host
host = APILocator.getHostAPI().find( newFolder, user, false );
}
// Checking permissions
String permissionsError = "The user doesn't have the required permissions.";
if ( !permissionAPI.doesUserHavePermission( link, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
} else if ( parent != null && !permissionAPI.doesUserHavePermission( parent, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
} else if ( host != null && !permissionAPI.doesUserHavePermission( host, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
}
if ( parent != null ) {
LinkFactory.copyLink( link, parent );
} else {
LinkFactory.copyLink( link, host );
}
return true;
}
/**
* Moves a given inode Link to a given folder
*
* @param inode Link inode
* @param newFolder This could be the inode of a folder or a host
* @return true if success, false otherwise
* @throws Exception
*/
public boolean moveLink ( String inode, String newFolder ) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser( req );
Link link = (Link) InodeFactory.getInode( inode, Link.class );
// gets folder parent
Folder parent = null;
try {
parent = APILocator.getFolderAPI().find( newFolder, user, false );
} catch ( Exception ignored ) {
//Probably what we have here is a host
}
Host host = null;
if ( parent == null ) {//If we didn't find a parent folder lets verify if this is a host
host = APILocator.getHostAPI().find( newFolder, user, false );
}
// Checking permissions
String permissionsError = "The user doesn't have the required permissions.";
if ( !permissionAPI.doesUserHavePermission( link, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
} else if ( parent != null && !permissionAPI.doesUserHavePermission( parent, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
} else if ( host != null && !permissionAPI.doesUserHavePermission( host, PERMISSION_WRITE, user ) ) {
throw new DotRuntimeException( permissionsError );
}
if ( parent != null ) {
return LinkFactory.moveLink( link, parent );
} else {
return LinkFactory.moveLink( link, host );
}
}
public boolean publishAsset (String inode) throws WebAssetException, Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
User user = getUser(req);
Identifier id = APILocator.getIdentifierAPI().findFromInode(inode);
if (!permissionAPI.doesUserHavePermission(id, PERMISSION_PUBLISH, user))
throw new DotRuntimeException("The user doesn't have the required permissions.");
if(id!=null && id.getAssetType().equals("contentlet")){
Contentlet cont = APILocator.getContentletAPI().find(inode, user, false);
APILocator.getContentletAPI().publish(cont, user, false);
return true;
}
java.util.List relatedAssets = new java.util.ArrayList();
Inode inodeObj = InodeFactory.getInode(inode,Inode.class);
HTMLPage htmlPage = null;
if( inodeObj instanceof HTMLPage ) {
htmlPage =(HTMLPage)inodeObj;
}
if (htmlPage != null && InodeUtils.isSet(htmlPage.getInode())) {