]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsUtils.java
7864dde8b645999e094529dce4facb70bfdf441e
[lgpl/argeo-commons.git] / CmsUtils.java
1 package org.argeo.cms.util;
2
3 import java.io.InputStream;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9 import javax.servlet.http.HttpServletRequest;
10
11 import org.apache.commons.io.IOUtils;
12 import org.argeo.cms.CmsException;
13 import org.argeo.cms.ui.CmsConstants;
14 import org.argeo.cms.ui.CmsView;
15 import org.argeo.eclipse.ui.specific.UiContext;
16 import org.argeo.jcr.JcrUtils;
17 import org.argeo.node.NodeConstants;
18 import org.argeo.node.NodeUtils;
19 import org.argeo.node.security.NodeAuthenticated;
20 import org.eclipse.rap.rwt.RWT;
21 import org.eclipse.rap.rwt.service.ResourceManager;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.graphics.ImageData;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.layout.RowData;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.Table;
33 import org.eclipse.swt.widgets.Widget;
34
35 /** Static utilities for the CMS framework. */
36 public class CmsUtils implements CmsConstants {
37 // private final static Log log = LogFactory.getLog(CmsUtils.class);
38
39 /**
40 * The CMS view related to this display, or null if none is available from
41 * this call.
42 */
43 public static CmsView getCmsView() {
44 return UiContext.getData(NodeAuthenticated.KEY);
45 }
46
47 public static StringBuilder getServerBaseUrl(HttpServletRequest request) {
48 try {
49 URL url = new URL(request.getRequestURL().toString());
50 StringBuilder buf = new StringBuilder();
51 buf.append(url.getProtocol()).append("://").append(url.getHost());
52 if (url.getPort() != -1)
53 buf.append(':').append(url.getPort());
54 return buf;
55 } catch (MalformedURLException e) {
56 throw new CmsException("Cannot extract server base URL from " + request.getRequestURL(), e);
57 }
58 }
59
60 //
61 public static String getDataUrl(Node node, HttpServletRequest request) throws RepositoryException {
62 try {
63 StringBuilder buf = getServerBaseUrl(request);
64 buf.append(getDataPath(node));
65 return new URL(buf.toString()).toString();
66 } catch (MalformedURLException e) {
67 throw new CmsException("Cannot build data URL for " + node, e);
68 }
69 }
70
71 // private final static String PATH_DATA = "/data";
72 // private final static String WEBDAV_PUBLIC = PATH_DATA + "/public";
73 // private final static String WEBDAV_PRIVATE = PATH_DATA + "/files";
74
75 /** A path in the node repository */
76 public static String getDataPath(Node node) throws RepositoryException {
77 return getDataPath(NodeConstants.NODE, node);
78 }
79
80 public static String getDataPath(String cn, Node node) throws RepositoryException {
81 return NodeUtils.getDataPath(cn, node);
82 // assert node != null;
83 // String userId = node.getSession().getUserID();
84 // if (log.isTraceEnabled())
85 // log.trace(userId + " : " + node.getPath());
86 // StringBuilder buf = new StringBuilder();
87 // boolean isAnonymous =
88 // userId.equalsIgnoreCase(NodeConstants.ROLE_ANONYMOUS);
89 // if (isAnonymous)
90 // buf.append(WEBDAV_PUBLIC);
91 // else
92 // buf.append(WEBDAV_PRIVATE);
93 // Session session = node.getSession();
94 // Repository repository = session.getRepository();
95 // String cn;
96 // if (repository.isSingleValueDescriptor(NodeConstants.CN)) {
97 // cn = repository.getDescriptor(NodeConstants.CN);
98 // } else {
99 // log.warn("No cn defined in repository, using " + NodeConstants.NODE);
100 // cn = NodeConstants.NODE;
101 // }
102 // return
103 // buf.append('/').append(cn).append('/').append(session.getWorkspace().getName()).append(node.getPath())
104 // .toString();
105 }
106 //
107 // public static String getCanonicalUrl(Node node, HttpServletRequest
108 // request) throws RepositoryException {
109 // try {
110 // StringBuilder buf = getServerBaseUrl(request);
111 // buf.append('/').append('!').append(node.getPath());
112 // return new URL(buf.toString()).toString();
113 // } catch (MalformedURLException e) {
114 // throw new CmsException("Cannot build data URL for " + node, e);
115 // }
116 // // return request.getRequestURL().append('!').append(node.getPath())
117 // // .toString();
118 // }
119
120 /** @deprecated Use rowData16px() instead. GridData should not be reused. */
121 @Deprecated
122 public static RowData ROW_DATA_16px = new RowData(16, 16);
123
124 public static GridLayout noSpaceGridLayout() {
125 return noSpaceGridLayout(new GridLayout());
126 }
127
128 public static GridLayout noSpaceGridLayout(GridLayout layout) {
129 layout.horizontalSpacing = 0;
130 layout.verticalSpacing = 0;
131 layout.marginWidth = 0;
132 layout.marginHeight = 0;
133 return layout;
134 }
135
136 //
137 // GRID DATA
138 //
139 public static GridData fillWidth() {
140 return grabWidth(SWT.FILL, SWT.FILL);
141 }
142
143 public static GridData fillAll() {
144 return new GridData(SWT.FILL, SWT.FILL, true, true);
145 }
146
147 public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
148 return new GridData(horizontalAlignment, horizontalAlignment, true, false);
149 }
150
151 public static RowData rowData16px() {
152 return new RowData(16, 16);
153 }
154
155 /** Style widget */
156 public static void style(Widget widget, String style) {
157 widget.setData(CmsConstants.STYLE, style);
158 }
159
160 /** Enable markups on widget */
161 public static void markup(Widget widget) {
162 widget.setData(CmsConstants.MARKUP, true);
163 }
164
165 public static void setItemHeight(Table table, int height) {
166 table.setData(CmsConstants.ITEM_HEIGHT, height);
167 }
168
169 // /** @return the path or null if not instrumented */
170 // @Deprecated
171 // public static String getDataPath(Widget widget) {
172 // // JCR item
173 // Object data = widget.getData();
174 // if (data != null && data instanceof Item) {
175 // try {
176 // return ((Item) data).getPath();
177 // } catch (RepositoryException e) {
178 // throw new CmsException("Cannot find data path of " + data + " for " +
179 // widget);
180 // }
181 // }
182 //
183 // // JCR path
184 // data = widget.getData(Property.JCR_PATH);
185 // if (data != null)
186 // return data.toString();
187 //
188 // return null;
189 // }
190
191 /** Dispose all children of a Composite */
192 public static void clear(Composite composite) {
193 for (Control child : composite.getChildren())
194 child.dispose();
195 }
196
197 //
198 // JCR
199 //
200 public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
201 if (has(parent, child))
202 return child(parent, child);
203 return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
204 }
205
206 public static Node child(Node parent, Enum<?> en) throws RepositoryException {
207 return parent.getNode(en.name());
208 }
209
210 public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
211 return parent.hasNode(en.name());
212 }
213
214 public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
215 return getOrAdd(parent, en, null);
216 }
217
218 public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
219 if (has(parent, en))
220 return child(parent, en);
221 else if (primaryType == null)
222 return parent.addNode(en.name());
223 else
224 return parent.addNode(en.name(), primaryType);
225 }
226
227 // IMAGES
228 public static String img(String src, String width, String height) {
229 return imgBuilder(src, width, height).append("/>").toString();
230 }
231
232 public static String img(String src, Point size) {
233 return img(src, Integer.toString(size.x), Integer.toString(size.y));
234 }
235
236 public static StringBuilder imgBuilder(String src, String width, String height) {
237 return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
238 .append("' src='").append(src).append("'");
239 }
240
241 public static String noImg(Point size) {
242 ResourceManager rm = RWT.getResourceManager();
243 return CmsUtils.img(rm.getLocation(NO_IMAGE), size);
244 }
245
246 public static String noImg() {
247 return noImg(NO_IMAGE_SIZE);
248 }
249
250 public static Image noImage(Point size) {
251 ResourceManager rm = RWT.getResourceManager();
252 InputStream in = null;
253 try {
254 in = rm.getRegisteredContent(NO_IMAGE);
255 ImageData id = new ImageData(in);
256 ImageData scaled = id.scaledTo(size.x, size.y);
257 Image image = new Image(Display.getCurrent(), scaled);
258 return image;
259 } finally {
260 IOUtils.closeQuietly(in);
261 }
262 }
263
264 private CmsUtils() {
265 }
266 }