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.journal.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.service.impl.ImageLocalUtil;
28  import com.liferay.portlet.journal.DuplicateArticleImageIdException;
29  import com.liferay.portlet.journal.NoSuchArticleImageException;
30  import com.liferay.portlet.journal.model.JournalArticleImage;
31  import com.liferay.portlet.journal.service.base.JournalArticleImageLocalServiceBaseImpl;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="JournalArticleImageLocalServiceImpl.java.html"><b><i>View Source</i>
38   * </b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class JournalArticleImageLocalServiceImpl
44      extends JournalArticleImageLocalServiceBaseImpl {
45  
46      public void addArticleImageId(
47              long articleImageId, long groupId, String articleId, double version,
48              String elName, String languageId)
49          throws PortalException, SystemException {
50  
51          if (articleImageId <= 0) {
52              return;
53          }
54  
55          JournalArticleImage articleImage =
56              journalArticleImagePersistence.fetchByG_A_V_E_L(
57                  groupId, articleId, version, elName, languageId);
58  
59          if (articleImage == null) {
60              articleImage = journalArticleImagePersistence.create(
61                  articleImageId);
62  
63              articleImage.setGroupId(groupId);
64              articleImage.setArticleId(articleId);
65              articleImage.setVersion(version);
66              articleImage.setElName(elName);
67              articleImage.setLanguageId(languageId);
68              articleImage.setTempImage(false);
69  
70              journalArticleImagePersistence.update(articleImage);
71          }
72          else if (articleImage.getArticleImageId() == articleImageId) {
73          }
74          else {
75              throw new DuplicateArticleImageIdException();
76          }
77      }
78  
79      public void deleteArticleImage(long articleImageId) throws SystemException {
80          try {
81              journalArticleImagePersistence.remove(articleImageId);
82          }
83          catch (NoSuchArticleImageException nsaie) {
84          }
85      }
86  
87      public void deleteArticleImage(
88              long groupId, String articleId, double version, String elName,
89              String languageId)
90          throws SystemException {
91  
92          try {
93              journalArticleImagePersistence.removeByG_A_V_E_L(
94                  groupId, articleId, version, elName, languageId);
95          }
96          catch (NoSuchArticleImageException nsaie) {
97          }
98      }
99  
100     public void deleteImages(long groupId, String articleId, double version)
101         throws SystemException {
102 
103         Iterator itr = journalArticleImagePersistence.findByG_A_V(
104             groupId, articleId, version).iterator();
105 
106         while (itr.hasNext()) {
107             JournalArticleImage articleImage = (JournalArticleImage)itr.next();
108 
109             ImageLocalUtil.deleteImage(articleImage.getArticleImageId());
110 
111             journalArticleImagePersistence.remove(articleImage);
112         }
113     }
114 
115     public JournalArticleImage getArticleImage(long articleImageId)
116         throws PortalException, SystemException {
117 
118         return journalArticleImagePersistence.findByPrimaryKey(articleImageId);
119     }
120 
121     public long getArticleImageId(
122             long groupId, String articleId, double version, String elName,
123             String languageId)
124         throws SystemException {
125 
126         return getArticleImageId(
127             groupId, articleId, version, elName, languageId, false);
128     }
129 
130     public long getArticleImageId(
131             long groupId, String articleId, double version, String elName,
132             String languageId, boolean tempImage)
133         throws SystemException {
134 
135         JournalArticleImage articleImage =
136             journalArticleImagePersistence.fetchByG_A_V_E_L(
137                 groupId, articleId, version, elName, languageId);
138 
139         if (articleImage == null) {
140             long articleImageId = counterLocalService.increment();
141 
142             articleImage = journalArticleImagePersistence.create(
143                 articleImageId);
144 
145             articleImage.setGroupId(groupId);
146             articleImage.setArticleId(articleId);
147             articleImage.setVersion(version);
148             articleImage.setElName(elName);
149             articleImage.setLanguageId(languageId);
150             articleImage.setTempImage(tempImage);
151 
152             journalArticleImagePersistence.update(articleImage);
153         }
154 
155         return articleImage.getArticleImageId();
156     }
157 
158     public List getArticleImages(long groupId) throws SystemException {
159         return journalArticleImagePersistence.findByGroupId(groupId);
160     }
161 
162 }