1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.imagegallery.model.impl;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Image;
29  import com.liferay.portal.service.ImageLocalServiceUtil;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.imagegallery.model.IGFolder;
32  import com.liferay.portlet.imagegallery.model.IGImage;
33  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /**
39   * <a href="IGImageImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class IGImageImpl extends IGImageModelImpl implements IGImage {
45  
46      public static String getNameWithExtension(String name, String type) {
47          if (Validator.isNotNull(type)) {
48              name += StringPool.PERIOD + type;
49          }
50  
51          return name;
52      }
53  
54      public IGImageImpl() {
55      }
56  
57      public String getUserUuid() throws SystemException {
58          return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
59      }
60  
61      public void setUserUuid(String userUuid) {
62          _userUuid = userUuid;
63      }
64  
65      public IGFolder getFolder() {
66          IGFolder folder = null;
67  
68          try {
69              folder = IGFolderLocalServiceUtil.getFolder(getFolderId());
70          }
71          catch (Exception e) {
72              folder = new IGFolderImpl();
73  
74              _log.error(e);
75          }
76  
77          return folder;
78      }
79  
80      public String getNameWithExtension() {
81          String nameWithExtension = getName();
82  
83          if (Validator.isNull(nameWithExtension)) {
84              nameWithExtension = String.valueOf(getImageId());
85          }
86  
87          String type = getImageType();
88  
89          return getNameWithExtension(nameWithExtension, type);
90      }
91  
92      public String getImageType() {
93          if (_imageType == null) {
94              try {
95                  Image largeImage = ImageLocalServiceUtil.getImage(
96                      getLargeImageId());
97  
98                  _imageType = largeImage.getType();
99              }
100             catch (Exception e) {
101                 _imageType = StringPool.BLANK;
102 
103                 _log.error(e);
104             }
105         }
106 
107         return _imageType;
108     }
109 
110     public void setImageType(String imageType) {
111         _imageType = imageType;
112     }
113 
114     public int getImageSize() {
115         if (_imageSize == null) {
116             try {
117                 Image largeImage = ImageLocalServiceUtil.getImage(
118                     getLargeImageId());
119 
120                 _imageSize = new Integer(largeImage.getSize());
121             }
122             catch (Exception e) {
123                 _imageSize = new Integer(0);
124 
125                 _log.error(e);
126             }
127         }
128 
129         return _imageSize.intValue();
130     }
131 
132     private static Log _log = LogFactory.getLog(IGImageImpl.class);
133 
134     private String _userUuid;
135     private String _imageType;
136     private Integer _imageSize;
137 
138 }