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