KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > executer > ImporterActionExecuter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.action.executer;
18
19 import java.io.File JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.model.ContentModel;
23 import org.alfresco.repo.action.ParameterDefinitionImpl;
24 import org.alfresco.repo.content.MimetypeMap;
25 import org.alfresco.repo.importer.ACPImportPackageHandler;
26 import org.alfresco.service.cmr.action.Action;
27 import org.alfresco.service.cmr.action.ParameterDefinition;
28 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
29 import org.alfresco.service.cmr.repository.ContentReader;
30 import org.alfresco.service.cmr.repository.ContentService;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.repository.NodeService;
33 import org.alfresco.service.cmr.view.ImporterService;
34 import org.alfresco.service.cmr.view.Location;
35 import org.alfresco.util.TempFileProvider;
36
37 /**
38  * Importer action executor
39  *
40  * @author gavinc
41  */

42 public class ImporterActionExecuter extends ActionExecuterAbstractBase
43 {
44     public static final String JavaDoc NAME = "import";
45     public static final String JavaDoc PARAM_ENCODING = "encoding";
46     public static final String JavaDoc PARAM_DESTINATION_FOLDER = "destination";
47
48     private static final String JavaDoc TEMP_FILE_PREFIX = "alf";
49     private static final String JavaDoc TEMP_FILE_SUFFIX = ".acp";
50     
51     /**
52      * The importer service
53      */

54     private ImporterService importerService;
55     
56     /**
57      * The node service
58      */

59     private NodeService nodeService;
60     
61     /**
62      * The content service
63      */

64     private ContentService contentService;
65     
66     /**
67      * Sets the ImporterService to use
68      *
69      * @param importerService The ImporterService
70      */

71     public void setImporterService(ImporterService importerService)
72     {
73         this.importerService = importerService;
74     }
75     
76     /**
77      * Sets the NodeService to use
78      *
79      * @param nodeService The NodeService
80      */

81     public void setNodeService(NodeService nodeService)
82     {
83        this.nodeService = nodeService;
84     }
85     
86     /**
87      * Sets the ContentService to use
88      *
89      * @param contentService The ContentService
90      */

91     public void setContentService(ContentService contentService)
92     {
93        this.contentService = contentService;
94     }
95
96     /**
97      * @see org.alfresco.repo.action.executer.ActionExecuter#execute(org.alfresco.repo.ref.NodeRef, org.alfresco.repo.ref.NodeRef)
98      */

99     public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef)
100     {
101         if (this.nodeService.exists(actionedUponNodeRef) == true)
102         {
103            // The node being passed in should be an Alfresco content package
104
ContentReader reader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
105            if (reader != null)
106            {
107                if (MimetypeMap.MIMETYPE_ACP.equals(reader.getMimetype()))
108                {
109                    File JavaDoc zipFile = null;
110                    try
111                    {
112                        // unfortunately a ZIP file can not be read directly from an input stream so we have to create
113
// a temporary file first
114
zipFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);
115                        reader.getContent(zipFile);
116                       
117                        ACPImportPackageHandler importHandler = new ACPImportPackageHandler(zipFile,
118                              (String JavaDoc)ruleAction.getParameterValue(PARAM_ENCODING));
119                        NodeRef importDest = (NodeRef)ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
120                       
121                        this.importerService.importView(importHandler, new Location(importDest), null, null);
122                    }
123                    finally
124                    {
125                       // now the import is done, delete the temporary file
126
if (zipFile != null)
127                       {
128                          zipFile.delete();
129                       }
130                    }
131                }
132            }
133         }
134     }
135
136     /**
137      * @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
138      */

139     protected void addParameterDefintions(List JavaDoc<ParameterDefinition> paramList)
140     {
141         paramList.add(new ParameterDefinitionImpl(PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF,
142               true, getParamDisplayLabel(PARAM_DESTINATION_FOLDER)));
143         paramList.add(new ParameterDefinitionImpl(PARAM_ENCODING, DataTypeDefinition.TEXT,
144               true, getParamDisplayLabel(PARAM_ENCODING)));
145     }
146
147 }
148
Popular Tags