1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Image;
30  import com.liferay.portal.service.ImageLocalServiceUtil;
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  /**
36   * <a href="IGImageImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class IGImageImpl extends IGImageModelImpl implements IGImage {
41  
42      public static String getNameWithExtension(String name, String type) {
43          if (Validator.isNotNull(type)) {
44              name += StringPool.PERIOD + type;
45          }
46  
47          return name;
48      }
49  
50      public IGImageImpl() {
51      }
52  
53      public IGFolder getFolder() {
54          IGFolder folder = null;
55  
56          try {
57              folder = IGFolderLocalServiceUtil.getFolder(getFolderId());
58          }
59          catch (Exception e) {
60              folder = new IGFolderImpl();
61  
62              _log.error(e);
63          }
64  
65          return folder;
66      }
67  
68      public String getNameWithExtension() {
69          String nameWithExtension = getName();
70  
71          if (Validator.isNull(nameWithExtension)) {
72              nameWithExtension = String.valueOf(getImageId());
73          }
74  
75          String type = getImageType();
76  
77          return getNameWithExtension(nameWithExtension, type);
78      }
79  
80      public String getImageType() {
81          if (_imageType == null) {
82              try {
83                  Image largeImage = ImageLocalServiceUtil.getImage(
84                      getLargeImageId());
85  
86                  _imageType = largeImage.getType();
87              }
88              catch (Exception e) {
89                  _imageType = StringPool.BLANK;
90  
91                  _log.error(e);
92              }
93          }
94  
95          return _imageType;
96      }
97  
98      public void setImageType(String imageType) {
99          _imageType = imageType;
100     }
101 
102     public int getImageSize() {
103         if (_imageSize == null) {
104             try {
105                 Image largeImage = ImageLocalServiceUtil.getImage(
106                     getLargeImageId());
107 
108                 _imageSize = new Integer(largeImage.getSize());
109             }
110             catch (Exception e) {
111                 _imageSize = new Integer(0);
112 
113                 _log.error(e);
114             }
115         }
116 
117         return _imageSize.intValue();
118     }
119 
120     private static Log _log = LogFactoryUtil.getLog(IGImageImpl.class);
121 
122     private String _imageType;
123     private Integer _imageSize;
124 
125 }