1
22
23 package com.liferay.portlet.social.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.StringMaker;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
30 import com.liferay.portal.spring.hibernate.HibernateUtil;
31 import com.liferay.portlet.social.NoSuchRelationException;
32 import com.liferay.portlet.social.model.SocialRelation;
33 import com.liferay.portlet.social.model.SocialRelationConstants;
34 import com.liferay.portlet.social.model.impl.SocialRelationImpl;
35 import com.liferay.util.dao.hibernate.QueryPos;
36 import com.liferay.util.dao.hibernate.QueryUtil;
37
38 import java.util.Iterator;
39 import java.util.List;
40
41 import org.hibernate.Hibernate;
42 import org.hibernate.SQLQuery;
43 import org.hibernate.Session;
44
45
51 public class SocialRelationFinderImpl implements SocialRelationFinder {
52
53 public static String COUNT_BY_U_T =
54 SocialRelationFinder.class.getName() + ".countByU_T";
55
56 public static String FIND_BY_U_T =
57 SocialRelationFinder.class.getName() + ".findByU_T";
58
59 public static String FIND_BY_U_U_T_BI =
60 SocialRelationFinder.class.getName() + ".findByU_U_T_BI";
61
62 public static String FIND_BY_U_U_T_UNI =
63 SocialRelationFinder.class.getName() + ".findByU_U_T_UNI";
64
65 public int countByU_T(long userId, int type) throws SystemException {
66 Session session = null;
67
68 try {
69 session = HibernateUtil.openSession();
70
71 String sql = CustomSQLUtil.get(COUNT_BY_U_T);
72
73 if (SocialRelationConstants.isTypeUni(type)) {
74 sql = StringUtil.replace(
75 sql, "(SocialRelation.userId2 = ?) OR", StringPool.BLANK);
76 }
77
78 SQLQuery q = session.createSQLQuery(sql);
79
80 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
81
82 QueryPos qPos = QueryPos.getInstance(q);
83
84 qPos.add(userId);
85
86 if (SocialRelationConstants.isTypeBi(type)) {
87 qPos.add(userId);
88 }
89
90 qPos.add(type);
91
92 Iterator<Long> itr = q.list().iterator();
93
94 if (itr.hasNext()) {
95 Long count = itr.next();
96
97 if (count != null) {
98 return count.intValue();
99 }
100 }
101
102 return 0;
103 }
104 catch (Exception e) {
105 throw new SystemException(e);
106 }
107 finally {
108 HibernateUtil.closeSession(session);
109 }
110 }
111
112 public List<SocialRelation> findByU_T(
113 long userId, int type, int begin, int end)
114 throws SystemException {
115
116 Session session = null;
117
118 try {
119 session = HibernateUtil.openSession();
120
121 String sql = CustomSQLUtil.get(FIND_BY_U_T);
122
123 if (SocialRelationConstants.isTypeUni(type)) {
124 sql = StringUtil.replace(
125 sql, "(SocialRelation.userId2 = ?) OR", StringPool.BLANK);
126 }
127
128 SQLQuery q = session.createSQLQuery(sql);
129
130 q.addEntity("SocialRelation", SocialRelationImpl.class);
131
132 QueryPos qPos = QueryPos.getInstance(q);
133
134 qPos.add(userId);
135
136 if (SocialRelationConstants.isTypeBi(type)) {
137 qPos.add(userId);
138 }
139
140 qPos.add(type);
141
142 return (List<SocialRelation>)QueryUtil.list(
143 q, HibernateUtil.getDialect(), begin, end);
144 }
145 catch (Exception e) {
146 throw new SystemException(e);
147 }
148 finally {
149 HibernateUtil.closeSession(session);
150 }
151 }
152
153 public SocialRelation findByU_U_T(long userId1, long userId2, int type)
154 throws NoSuchRelationException, SystemException {
155
156 Session session = null;
157
158 try {
159 session = HibernateUtil.openSession();
160
161 String sql = CustomSQLUtil.get(FIND_BY_U_U_T_BI);
162
163 if (SocialRelationConstants.isTypeUni(type)) {
164 sql = CustomSQLUtil.get(FIND_BY_U_U_T_UNI);
165 }
166
167 SQLQuery q = session.createSQLQuery(sql);
168
169 q.addEntity("SocialRelation", SocialRelationImpl.class);
170
171 QueryPos qPos = QueryPos.getInstance(q);
172
173 qPos.add(userId1);
174 qPos.add(userId2);
175 qPos.add(type);
176
177 if (SocialRelationConstants.isTypeBi(type)) {
178 qPos.add(userId2);
179 qPos.add(userId1);
180 qPos.add(type);
181 }
182
183 Iterator<SocialRelation> itr = q.list().iterator();
184
185 if (itr.hasNext()) {
186 SocialRelation relation = itr.next();
187
188 return relation;
189 }
190 }
191 catch (Exception e) {
192 throw new SystemException(e);
193 }
194 finally {
195 HibernateUtil.closeSession(session);
196 }
197
198 StringMaker sm = new StringMaker();
199
200 sm.append("No SocialRelation exists with the key {userId1=");
201 sm.append(userId1);
202 sm.append(", userId2=");
203 sm.append(userId2);
204 sm.append(", type=");
205 sm.append(type);
206 sm.append("}");
207
208 throw new NoSuchRelationException(sm.toString());
209 }
210
211 }