001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
019    import com.liferay.portal.kernel.dao.orm.Session;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.jsonwebservice.JSONWebService;
023    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceMode;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.messaging.DestinationNames;
027    import com.liferay.portal.kernel.messaging.Message;
028    import com.liferay.portal.kernel.messaging.sender.DirectSynchronousMessageSender;
029    import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
030    import com.liferay.portal.kernel.transaction.Propagation;
031    import com.liferay.portal.kernel.transaction.Transactional;
032    import com.liferay.portal.kernel.util.PropsKeys;
033    import com.liferay.portal.kernel.util.ReleaseInfo;
034    import com.liferay.portal.model.ClassName;
035    import com.liferay.portal.model.impl.ClassNameImpl;
036    import com.liferay.portal.service.PortalService;
037    import com.liferay.portal.service.base.PortalServiceBaseImpl;
038    import com.liferay.portal.util.PrefsPropsUtil;
039    import com.liferay.portal.util.PropsValues;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    @JSONWebService(mode = JSONWebServiceMode.MANUAL)
045    public class PortalServiceImpl extends PortalServiceBaseImpl {
046    
047            public String getAutoDeployDirectory() throws SystemException {
048                    return PrefsPropsUtil.getString(
049                            PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
050                            PropsValues.AUTO_DEPLOY_DEPLOY_DIR);
051            }
052    
053            @JSONWebService
054            public int getBuildNumber() {
055                    return ReleaseInfo.getBuildNumber();
056            }
057    
058            public void testAddClassName_Rollback(String classNameValue)
059                    throws SystemException {
060    
061                    addClassName(classNameValue);
062    
063                    throw new SystemException();
064            }
065    
066            public void testAddClassName_Success(String classNameValue)
067                    throws SystemException {
068    
069                    addClassName(classNameValue);
070            }
071    
072            public void testAddClassNameAndTestTransactionPortletBar_PortalRollback(
073                            String transactionPortletBarText)
074                    throws SystemException {
075    
076                    addClassName(PortalService.class.getName());
077    
078                    addTransactionPortletBar(transactionPortletBarText, false);
079    
080                    throw new SystemException();
081            }
082    
083            public void testAddClassNameAndTestTransactionPortletBar_PortletRollback(
084                            String transactionPortletBarText)
085                    throws SystemException {
086    
087                    addClassName(PortalService.class.getName());
088    
089                    addTransactionPortletBar(transactionPortletBarText, true);
090            }
091    
092            public void testAddClassNameAndTestTransactionPortletBar_Success(
093                            String transactionPortletBarText)
094                    throws SystemException {
095    
096                    addClassName(PortalService.class.getName());
097    
098                    addTransactionPortletBar(transactionPortletBarText, false);
099            }
100    
101            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
102            public void testAutoSyncHibernateSessionStateOnTxCreation()
103                    throws SystemException {
104    
105                    // Add in new transaction
106    
107                    ClassName className = classNameLocalService.addClassName(
108                            "testAutoSyncHibernateSessionStateOnTxCreation1");
109    
110                    try {
111    
112                            // Fetch in current transaction
113    
114                            // Clear entity cache to force Hibernate to populate its first level
115                            // cache
116    
117                            EntityCacheUtil.clearCache();
118    
119                            className = classNamePersistence.fetchByPrimaryKey(
120                                    className.getClassNameId());
121    
122                            Session currentSession = classNamePersistence.getCurrentSession();
123    
124                            if (!currentSession.contains(className)) {
125                                    throw new IllegalStateException(
126                                            "Entities are not available in Hibernate's first level " +
127                                                    "cache");
128                            }
129    
130                            ClassName newClassName = new ClassNameImpl();
131    
132                            newClassName.setPrimaryKey(className.getClassNameId());
133    
134                            String newValue = "testAutoSyncHibernateSessionStateOnTxCreation2";
135    
136                            newClassName.setValue(newValue);
137    
138                            // Update in new transaction
139    
140                            classNameLocalService.updateClassName(newClassName);
141    
142                            if (currentSession.contains(className)) {
143                                    throw new IllegalStateException(
144                                            "Entities are still available in Hibernate's first level " +
145                                                    "cache");
146                            }
147    
148                            // Refetch in current transaction
149    
150                            // Clear entity cache to force Hibernate to populate its first level
151                            // cache
152    
153                            EntityCacheUtil.clearCache();
154    
155                            className = classNamePersistence.fetchByPrimaryKey(
156                                    className.getClassNameId());
157    
158                            if (!newValue.equals(className.getValue())) {
159                                    throw new IllegalStateException(
160                                            "Expected " + newValue + " but found " +
161                                                    className.getClassName());
162                            }
163                    }
164                    finally {
165    
166                            // Clean up
167    
168                            classNameLocalService.deleteClassName(className);
169                    }
170            }
171    
172            public void testCounterIncrement_Rollback() throws SystemException {
173                    int counterIncrement = PropsValues.COUNTER_INCREMENT;
174    
175                    for (int i = 0; i < counterIncrement * 2; i++) {
176                            counterLocalService.increment();
177                    }
178    
179                    throw new SystemException();
180            }
181    
182            public void testDeleteClassName() throws PortalException, SystemException {
183                    classNamePersistence.removeByValue(PortalService.class.getName());
184            }
185    
186            public int testGetBuildNumber() {
187                    return portalService.getBuildNumber();
188            }
189    
190            public void testGetUserId() {
191                    long userId = 0;
192    
193                    try {
194                            userId = getUserId();
195                    }
196                    catch (Exception e) {
197                            _log.error(e, e);
198                    }
199    
200                    if (_log.isInfoEnabled()) {
201                            _log.info("User id " + userId);
202                    }
203            }
204    
205            public boolean testHasClassName() throws SystemException {
206                    int count = classNamePersistence.countByValue(
207                            PortalService.class.getName());
208    
209                    if (count > 0) {
210                            return true;
211                    }
212                    else {
213                            return false;
214                    }
215            }
216    
217            protected void addClassName(String classNameValue) throws SystemException {
218                    long classNameId = counterLocalService.increment();
219    
220                    ClassName className = classNamePersistence.create(classNameId);
221    
222                    className.setValue(classNameValue);
223    
224                    classNamePersistence.update(className);
225            }
226    
227            protected void addTransactionPortletBar(
228                            String transactionPortletBarText, boolean rollback)
229                    throws SystemException {
230    
231                    try {
232                            Message message = new Message();
233    
234                            message.put("rollback", rollback);
235                            message.put("text", transactionPortletBarText);
236    
237                            SynchronousMessageSender synchronousMessageSender =
238                                    (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
239                                            DirectSynchronousMessageSender.class.getName());
240    
241                            synchronousMessageSender.send(
242                                    DestinationNames.TEST_TRANSACTION, message);
243                    }
244                    catch (Exception e) {
245                            throw new SystemException(e);
246                    }
247            }
248    
249            private static Log _log = LogFactoryUtil.getLog(PortalServiceImpl.class);
250    
251    }