1
14
15 package com.liferay.portal.service.impl;
16
17 import com.liferay.portal.NoSuchImageException;
18 import com.liferay.portal.SystemException;
19 import com.liferay.portal.kernel.image.ImageBag;
20 import com.liferay.portal.kernel.image.ImageProcessorUtil;
21 import com.liferay.portal.kernel.log.Log;
22 import com.liferay.portal.kernel.log.LogFactoryUtil;
23 import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
24 import com.liferay.portal.kernel.util.FileUtil;
25 import com.liferay.portal.kernel.util.PropsKeys;
26 import com.liferay.portal.model.Image;
27 import com.liferay.portal.model.impl.ImageImpl;
28 import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
29 import com.liferay.portal.util.PropsUtil;
30
31 import java.awt.image.RenderedImage;
32
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37
38 import java.util.Arrays;
39 import java.util.Date;
40 import java.util.List;
41
42
47 public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
48
49 public void afterPropertiesSet() {
50 ClassLoader classLoader = getClass().getClassLoader();
51
52 try {
53 InputStream is = classLoader.getResourceAsStream(
54 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
55
56 if (is == null) {
57 _log.error("Default spacer is not available");
58 }
59
60 _defaultSpacer = getImage(is);
61 }
62 catch (IOException ioe) {
63 _log.error(
64 "Unable to configure the default spacer: " + ioe.getMessage());
65 }
66
67 try {
68 InputStream is = classLoader.getResourceAsStream(
69 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
70
71 if (is == null) {
72 _log.error("Default company logo is not available");
73 }
74
75 _defaultCompanyLogo = getImage(is);
76 }
77 catch (IOException ioe) {
78 _log.error(
79 "Unable to configure the default company logo: " +
80 ioe.getMessage());
81 }
82
83 try {
84 InputStream is = classLoader.getResourceAsStream(
85 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
86
87 if (is == null) {
88 _log.error("Default user female portrait is not available");
89 }
90
91 _defaultUserFemalePortrait = getImage(is);
92 }
93 catch (IOException ioe) {
94 _log.error(
95 "Unable to configure the default user female portrait: " +
96 ioe.getMessage());
97 }
98
99 try {
100 InputStream is = classLoader.getResourceAsStream(
101 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
102
103 if (is == null) {
104 _log.error("Default user male portrait is not available");
105 }
106
107 _defaultUserMalePortrait = getImage(is);
108 }
109 catch (IOException ioe) {
110 _log.error(
111 "Unable to configure the default user male portrait: " +
112 ioe.getMessage());
113 }
114 }
115
116 public void deleteImage(long imageId) throws SystemException {
117 if (imageId <= 0) {
118 return;
119 }
120
121 if (imagePersistence.getListeners().length == 0) {
122 runSQL("delete from Image where imageId = " + imageId);
123
124 imagePersistence.clearCache();
125 }
126 else {
127 try {
128 imagePersistence.remove(imageId);
129 }
130 catch (NoSuchImageException nsie) {
131 }
132 }
133
134 }
135
136 public Image getCompanyLogo(long imageId) {
137 Image image = getImage(imageId);
138
139 if (image == null) {
140 image = getDefaultCompanyLogo();
141 }
142
143 return image;
144 }
145
146 public Image getDefaultCompanyLogo() {
147 return _defaultCompanyLogo;
148 }
149
150 public Image getDefaultSpacer() {
151 return _defaultSpacer;
152 }
153
154 public Image getDefaultUserFemalePortrait() {
155 return _defaultUserFemalePortrait;
156 }
157
158 public Image getDefaultUserMalePortrait() {
159 return _defaultUserMalePortrait;
160 }
161
162 public Image getImage(long imageId) {
163 try {
164 if (imageId > 0) {
165 return imagePersistence.findByPrimaryKey(imageId);
166 }
167 }
168 catch (Exception e) {
169 if (_log.isWarnEnabled()) {
170 _log.warn(
171 "Unable to get image " + imageId + ": " + e.getMessage());
172 }
173 }
174
175 return null;
176 }
177
178 public Image getImage(byte[] bytes) throws IOException {
179 return getImage(null, bytes);
180 }
181
182 public Image getImage(File file) throws IOException {
183 return getImage(new FileInputStream(file));
184 }
185
186 public Image getImage(InputStream is) throws IOException {
187 return getImage(is, null);
188 }
189
190 public Image getImageOrDefault(long imageId) {
191 Image image = getImage(imageId);
192
193 if (image == null) {
194 image = getDefaultSpacer();
195 }
196
197 return image;
198 }
199
200 public List<Image> getImages() throws SystemException {
201 return imagePersistence.findAll();
202 }
203
204 public List<Image> getImages(int start, int end) throws SystemException {
205 return imagePersistence.findAll(start, end);
206 }
207
208 public List<Image> getImagesBySize(int size) throws SystemException {
209 return imagePersistence.findBySize(size);
210 }
211
212 public boolean isNullOrDefaultSpacer(byte[] bytes) {
213 if ((bytes == null) || (bytes.length == 0) ||
214 (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
215
216 return true;
217 }
218 else {
219 return false;
220 }
221 }
222
223 public Image updateImage(long imageId, byte[] bytes)
224 throws SystemException {
225
226 try {
227 Image image = getImage(bytes);
228
229 return updateImage(
230 imageId, image.getTextObj(), image.getType(), image.getHeight(),
231 image.getWidth(), image.getSize());
232 }
233 catch (IOException ioe) {
234 throw new SystemException(ioe);
235 }
236 }
237
238 public Image updateImage(long imageId, File file)
239 throws SystemException {
240
241 try {
242 Image image = getImage(file);
243
244 return updateImage(
245 imageId, image.getTextObj(), image.getType(), image.getHeight(),
246 image.getWidth(), image.getSize());
247 }
248 catch (IOException ioe) {
249 throw new SystemException(ioe);
250 }
251 }
252
253 public Image updateImage(long imageId, InputStream is)
254 throws SystemException {
255
256 try {
257 Image image = getImage(is);
258
259 return updateImage(
260 imageId, image.getTextObj(), image.getType(), image.getHeight(),
261 image.getWidth(), image.getSize());
262 }
263 catch (IOException ioe) {
264 throw new SystemException(ioe);
265 }
266 }
267
268 public Image updateImage(
269 long imageId, byte[] bytes, String type, int height, int width,
270 int size)
271 throws SystemException {
272
273 Image image = imagePersistence.fetchByPrimaryKey(imageId);
274
275 if (image == null) {
276 image = imagePersistence.create(imageId);
277 }
278
279 image.setModifiedDate(new Date());
280 image.setTextObj(bytes);
281 image.setType(type);
282 image.setHeight(height);
283 image.setWidth(width);
284 image.setSize(size);
285
286 imagePersistence.update(image, false);
287
288 ImageServletTokenUtil.resetToken(imageId);
289
290 return image;
291 }
292
293 protected Image getImage(InputStream is, byte[] bytes) throws IOException {
294 try {
295 if (is != null) {
296 bytes = FileUtil.getBytes(is);
297 }
298
299 ImageBag imageBag = ImageProcessorUtil.read(bytes);
300
301 RenderedImage renderedImage = imageBag.getRenderedImage();
302 String type = imageBag.getType();
303
304 if (renderedImage == null) {
305 throw new IOException(
306 "Unable to retreive rendered image from input stream " +
307 "with type " + type);
308 }
309
310 int height = renderedImage.getHeight();
311 int width = renderedImage.getWidth();
312 int size = bytes.length;
313
314 Image image = new ImageImpl();
315
316 image.setTextObj(bytes);
317 image.setType(type);
318 image.setHeight(height);
319 image.setWidth(width);
320 image.setSize(size);
321
322 return image;
323 }
324 finally {
325 if (is != null) {
326 try {
327 is.close();
328 }
329 catch (IOException ioe) {
330 if (_log.isWarnEnabled()) {
331 _log.warn(ioe);
332 }
333 }
334 }
335 }
336 }
337
338 private static Log _log = LogFactoryUtil.getLog(
339 ImageLocalServiceImpl.class);
340
341 private Image _defaultSpacer;
342 private Image _defaultCompanyLogo;
343 private Image _defaultUserFemalePortrait;
344 private Image _defaultUserMalePortrait;
345
346 }