1
1
package com .ecmarchitect .share .behavior ;
2
- import java .io .Serializable ;
3
- import java .util .HashMap ;
4
- import java .util .Map ;
2
+ import static org .alfresco .repo .site .SiteModel .PROP_SITE_PRESET ;
3
+ import static org .alfresco .repo .site .SiteModel .TYPE_SITE ;
4
+
5
+ import java .util .List ;
5
6
6
7
import org .alfresco .model .ContentModel ;
7
8
import org .alfresco .repo .node .NodeServicePolicies ;
8
9
import org .alfresco .repo .policy .Behaviour ;
9
10
import org .alfresco .repo .policy .Behaviour .NotificationFrequency ;
10
11
import org .alfresco .repo .policy .JavaBehaviour ;
11
12
import org .alfresco .repo .policy .PolicyComponent ;
12
- import org .alfresco .repo .site .SiteModel ;
13
13
import org .alfresco .service .cmr .model .FileExistsException ;
14
14
import org .alfresco .service .cmr .model .FileFolderService ;
15
15
import org .alfresco .service .cmr .model .FileNotFoundException ;
19
19
import org .alfresco .service .cmr .repository .StoreRef ;
20
20
import org .alfresco .service .cmr .search .ResultSet ;
21
21
import org .alfresco .service .cmr .search .SearchService ;
22
+ import org .alfresco .service .cmr .site .SiteService ;
22
23
import org .alfresco .service .namespace .NamespaceService ;
23
24
import org .alfresco .service .namespace .QName ;
24
25
import org .apache .log4j .Logger ;
25
26
26
- import static org .alfresco .repo .site .SiteModel .ASPECT_SITE_CONTAINER ;
27
- import static org .alfresco .repo .site .SiteModel .PROP_SITE_PRESET ;
28
- import static org .alfresco .repo .site .SiteModel .TYPE_SITE ;
29
-
30
27
public class ShareDocumentLibraryFromTemplate implements NodeServicePolicies .OnCreateNodePolicy {
31
28
29
+ private static final String DOCUMENT_LIBRARY = "documentLibrary" ;
32
30
// Dependencies
33
31
private NodeService nodeService ;
34
32
private PolicyComponent policyComponent ;
35
33
private FileFolderService fileFolderService ;
36
34
private SearchService searchService ;
35
+ private SiteService siteService ;
37
36
38
37
// Behaviors
39
38
private Behaviour onCreateNode ;
@@ -42,7 +41,7 @@ public class ShareDocumentLibraryFromTemplate implements NodeServicePolicies.OnC
42
41
43
42
public void init () {
44
43
if (logger .isDebugEnabled ()) logger .debug ("Initializing rateable behaviors" );
45
-
44
+
46
45
// Create behaviors
47
46
this .onCreateNode = new JavaBehaviour (this , "onCreateNode" , NotificationFrequency .TRANSACTION_COMMIT );
48
47
@@ -102,32 +101,42 @@ public void onCreateNode(ChildAssociationRef childAssocRef) {
102
101
if (spaceTemplate == null ) {
103
102
logger .debug ("Space template doesn't exist" );
104
103
return ;
104
+ } else {
105
+ logger .debug ("Found space template: " + nodeService .getProperty (spaceTemplate , ContentModel .PROP_NAME ));
105
106
}
106
-
107
- //otherwise, create the documentLibrary folder as a child of this site folder
108
- //using the space template found above
109
- NodeRef documentLibrary ;
110
- try {
111
- documentLibrary = fileFolderService .copy (spaceTemplate , siteFolder , "documentLibrary" ).getNodeRef ();
112
-
113
- logger .debug ("Successfully created the document library node from a template" );
114
-
115
- //add the site container aspect, set the descriptions, set the component ID
116
- Map <QName , Serializable > props = new HashMap <QName , Serializable >();
117
- props .put (ContentModel .PROP_DESCRIPTION , "Document Library" );
118
- props .put (SiteModel .PROP_COMPONENT_ID , "documentLibrary" );
119
- nodeService .addAspect (documentLibrary , ASPECT_SITE_CONTAINER , props );
120
-
121
- } catch (FileExistsException e ) {
122
- logger .debug ("The document library node already exists. Each child needs to be copied." );
123
- // TODO implement this piece
124
- //iterate over the children of the source space template and copy them into the target
125
-
126
- } catch (FileNotFoundException e ) {
127
- //can't find the space template, just bail
128
- logger .warn ("Share site tried to use a space template, but the source space template could not be found." );
107
+
108
+ // otherwise, create the documentLibrary folder
109
+ String siteId = (String ) nodeService .getProperty (siteFolder , ContentModel .PROP_NAME );
110
+ logger .debug ("Site ID: " + siteId );
111
+
112
+ // use the site service to do this so that permissions get set correctly
113
+ NodeRef documentLibrary = siteService .getContainer (siteId , DOCUMENT_LIBRARY );
114
+ if (documentLibrary == null ) {
115
+ // create the document library container using the site service
116
+ documentLibrary = siteService .createContainer (siteId , DOCUMENT_LIBRARY , null , null );
117
+ if (documentLibrary == null ) {
118
+ logger .error ("Document library could not be created for: " + siteId );
119
+ }
129
120
}
130
121
122
+ // now, for each child in the space template, do a copy to the documentLibrary
123
+ List <ChildAssociationRef > children = nodeService .getChildAssocs (spaceTemplate );
124
+ for (ChildAssociationRef childRef : children ) {
125
+ // we only want contains associations
126
+ if (childRef .getQName ().equals (ContentModel .ASSOC_CONTAINS )) {
127
+ continue ;
128
+ }
129
+ NodeRef child = childRef .getChildRef ();
130
+ try {
131
+ fileFolderService .copy (child , documentLibrary , null );
132
+ logger .debug ("Successfully copied a child node from the template" );
133
+ } catch (FileExistsException e ) {
134
+ logger .debug ("The child node already exists in the document library." );
135
+ } catch (FileNotFoundException e ) {
136
+ //can't find the space template, just bail
137
+ logger .warn ("Share site tried to use a space template, but the source space template could not be found." );
138
+ }
139
+ }
131
140
}
132
141
133
142
public NodeService getNodeService () {
@@ -165,5 +174,13 @@ public void setSearchService(SearchService searchService) {
165
174
this .searchService = searchService ;
166
175
}
167
176
177
+ public SiteService getSiteService () {
178
+ return siteService ;
179
+ }
180
+
181
+ public void setSiteService (SiteService siteService ) {
182
+ this .siteService = siteService ;
183
+ }
184
+
168
185
}
169
186
0 commit comments