KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > repository > content > ContentRepositoryOutputHandler


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12 */

13 package org.pentaho.core.repository.content;
14
15 import java.util.Map JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.pentaho.core.repository.IContentItem;
20 import org.pentaho.core.repository.IContentLocation;
21 import org.pentaho.core.repository.IContentRepository;
22 import org.pentaho.core.session.IPentahoSession;
23 import org.pentaho.core.solution.IOutputDef;
24 import org.pentaho.core.solution.IOutputHandler;
25 import org.pentaho.core.system.PentahoSystem;
26 import org.pentaho.messages.Messages;
27
28 public class ContentRepositoryOutputHandler implements IOutputHandler {
29
30   public static final String JavaDoc DefaultMimeType = "application/octet-stream"; //$NON-NLS-1$
31
public static final String JavaDoc BackgroundFolder = "background"; //$NON-NLS-1$
32
public static final String JavaDoc DefaultExtension = ".bin"; //$NON-NLS-1$
33

34   private boolean contentGenerated;
35   private IContentItem outputContentItem;
36   private int outputType = OUTPUT_TYPE_DEFAULT;
37   private String JavaDoc location;
38   private String JavaDoc contentGUID;
39   private String JavaDoc solution;
40   private String JavaDoc mimeType;
41   private String JavaDoc extension;
42   private int writeMode = IContentItem.WRITEMODE_OVERWRITE;
43   private IPentahoSession userSession;
44   
45   
46   public ContentRepositoryOutputHandler(String JavaDoc location,
47       String JavaDoc contentGUID,
48       String JavaDoc solution,
49       IPentahoSession session) {
50     this(location, contentGUID, solution, DefaultMimeType, DefaultExtension, session);
51   }
52   
53   public ContentRepositoryOutputHandler(String JavaDoc location,
54       String JavaDoc contentGUID,
55       String JavaDoc solution,
56       String JavaDoc mimeType,
57       String JavaDoc extension,
58       IPentahoSession session) {
59
60     this.contentGUID = contentGUID;
61     this.location = location;
62     this.solution = solution;
63     this.mimeType = mimeType;
64     this.extension = extension;
65     userSession = session;
66   }
67
68   public void setMimeType(String JavaDoc value) {
69     mimeType = value;
70   }
71   
72   public void setExtension(String JavaDoc value) {
73     extension = value;
74   }
75   
76   public String JavaDoc getMimeType() {
77     return mimeType;
78   }
79   
80   public String JavaDoc getExtension() {
81     return extension;
82   }
83   
84   public void setWriteMode(int value) {
85     writeMode = value;
86   }
87   
88   public int getWriteMode() {
89     return writeMode;
90   }
91   
92   public Log getLogger() {
93     return LogFactory.getLog(ContentRepositoryOutputHandler.class);
94   }
95   
96   public boolean allowFeedback() {
97     return false;
98   }
99
100   public boolean contentDone() {
101     return contentGenerated;
102   }
103
104   public IContentItem getFeedbackContentItem() {
105     return null;
106   }
107
108
109   public IContentItem getOutputContentItem(String JavaDoc objectName, String JavaDoc contentName) {
110       return getOutputContentItem( objectName, contentName, contentGUID, null);
111   }
112     
113   public IContentItem getOutputContentItem(String JavaDoc objectName, String JavaDoc contentName, String JavaDoc title, String JavaDoc url) {
114         contentGenerated = true;
115       if (outputContentItem == null) {
116       // We need to create one now because someone is asking for it.
117
IContentRepository contentRepository = PentahoSystem.getContentRepository(userSession);
118       if (contentRepository == null) {
119           getLogger().error(Messages.getErrorString("RuntimeContext.ERROR_0024_NO_CONTENT_REPOSITORY")); //$NON-NLS-1$
120
return null;
121       }
122       // Find the location if it's already there.
123
IContentLocation contentLocation = null;
124       try {
125           contentLocation = contentRepository.getContentLocationByPath(location);
126       } catch (Exception JavaDoc ex) {
127           // ignored
128
}
129       if (contentLocation == null) {
130           contentLocation = contentRepository.newContentLocation(location, contentName, contentName, solution, true);
131       }
132       
133       if (contentLocation == null) {
134           getLogger().error(Messages.getErrorString("RuntimeContext.ERROR_0025_INVALID_CONTENT_LOCATION")); //$NON-NLS-1$
135
return null;
136       }
137       
138       // Get the content item from the location - if it's there.
139
IContentItem contentItem = null;
140       try {
141           contentItem = contentLocation.getContentItemByName(contentGUID);
142       } catch (Exception JavaDoc ex) {
143           // Ignored
144
}
145       if (contentItem == null) {
146         // Create a contentItem with the ID that came from the contentGUID.
147
// This must be a GUID or conflicts in the repository will happen.
148

149         contentItem = contentLocation.newContentItem(contentGUID, contentGUID, title, extension, mimeType, url, writeMode);
150       }
151       outputContentItem = contentItem;
152     }
153     return outputContentItem;
154   }
155
156   public IOutputDef getOutputDef(String JavaDoc name) {
157     // TODO Auto-generated method stub
158
return null;
159   }
160
161   public Map JavaDoc getOutputDefs() {
162     // TODO Auto-generated method stub
163
return null;
164   }
165
166   public int getOutputPreference() {
167     // TODO Auto-generated method stub
168
return outputType;
169   }
170
171   public void setContentItem(IContentItem content, String JavaDoc objectName,
172       String JavaDoc contentName) {
173     outputContentItem = content;
174   }
175
176   public void setOutput(String JavaDoc name, Object JavaDoc value) {
177     if (IOutputHandler.CONTENT.equalsIgnoreCase(name)) {
178       if ( value instanceof IContentItem ) {
179         outputContentItem = (IContentItem)value;
180         contentGenerated = true;
181       }
182     }
183   }
184
185   public void setOutputPreference(int value) {
186     this.outputType = value;
187   }
188
189 }
190
Popular Tags