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