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 int getImageSize() {
69          if (_imageSize == null) {
70              try {
71                  Image largeImage = ImageLocalServiceUtil.getImage(
72                      getLargeImageId());
73  
74                  _imageSize = new Integer(largeImage.getSize());
75              }
76              catch (Exception e) {
77                  _imageSize = new Integer(0);
78  
79                  _log.error(e);
80              }
81          }
82  
83          return _imageSize.intValue();
84      }
85  
86      public String getImageType() {
87          if (_imageType == null) {
88              try {
89                  Image largeImage = ImageLocalServiceUtil.getImage(
90                      getLargeImageId());
91  
92                  _imageType = largeImage.getType();
93              }
94              catch (Exception e) {
95                  _imageType = StringPool.BLANK;
96  
97                  _log.error(e);
98              }
99          }
100 
101         return _imageType;
102     }
103 
104     public String getNameWithExtension() {
105         String nameWithExtension = getName();
106 
107         if (Validator.isNull(nameWithExtension)) {
108             nameWithExtension = String.valueOf(getImageId());
109         }
110 
111         String type = getImageType();
112 
113         return getNameWithExtension(nameWithExtension, type);
114     }
115 
116     public void setImageType(String imageType) {
117         _imageType = imageType;
118     }
119 
120     private static Log _log = LogFactoryUtil.getLog(IGImageImpl.class);
121 
122     private Integer _imageSize;
123     private String _imageType;
124 
125 }