001    /**
002     * Copyright (c) 2000-2013 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            @Override
048            public String getAutoDeployDirectory() throws SystemException {
049                    return PrefsPropsUtil.getString(
050                            PropsKeys.AUTO_DEPLOY_DEPLOY_DIR,
051                            PropsValues.AUTO_DEPLOY_DEPLOY_DIR);
052            }
053    
054            @JSONWebService
055            @Override
056            public int getBuildNumber() {
057                    return ReleaseInfo.getBuildNumber();
058            }
059    
060            @Override
061            public void testAddClassName_Rollback(String classNameValue)
062                    throws SystemException {
063    
064                    addClassName(classNameValue);
065    
066                    throw new SystemException();
067            }
068    
069            @Override
070            public void testAddClassName_Success(String classNameValue)
071                    throws SystemException {
072    
073                    addClassName(classNameValue);
074            }
075    
076            @Override
077            public void testAddClassNameAndTestTransactionPortletBar_PortalRollback(
078                            String transactionPortletBarText)
079                    throws SystemException {
080    
081                    addClassName(PortalService.class.getName());
082    
083                    addTransactionPortletBar(transactionPortletBarText, false);
084    
085                    throw new SystemException();
086            }
087    
088            @Override
089            public void testAddClassNameAndTestTransactionPortletBar_PortletRollback(
090                            String transactionPortletBarText)
091                    throws SystemException {
092    
093                    addClassName(PortalService.class.getName());
094    
095                    addTransactionPortletBar(transactionPortletBarText, true);
096            }
097    
098            @Override
099            public void testAddClassNameAndTestTransactionPortletBar_Success(
100                            String transactionPortletBarText)
101                    throws SystemException {
102    
103                    addClassName(PortalService.class.getName());
104    
105                    addTransactionPortletBar(transactionPortletBarText, false);
106            }
107    
108            @Override
109            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
110            public void testAutoSyncHibernateSessionStateOnTxCreation()
111                    throws SystemException {
112    
113                    // Add in new transaction
114    
115                    ClassName className = classNameLocalService.addClassName(
116                            "testAutoSyncHibernateSessionStateOnTxCreation1");
117    
118                    try {
119    
120                            // Fetch in current transaction
121    
122                            // Clear entity cache to force Hibernate to populate its first level
123                            // cache
124    
125                            EntityCacheUtil.clearCache();
126    
127                            className = classNamePersistence.fetchByPrimaryKey(
128                                    className.getClassNameId());
129    
130                            Session currentSession = classNamePersistence.getCurrentSession();
131    
132                            if (!currentSession.contains(className)) {
133                                    throw new IllegalStateException(
134                                            "Entities are not available in Hibernate's first level " +
135                                                    "cache");
136                            }
137    
138                            ClassName newClassName = new ClassNameImpl();
139    
140                            newClassName.setPrimaryKey(className.getClassNameId());
141    
142                            String newValue = "testAutoSyncHibernateSessionStateOnTxCreation2";
143    
144                            newClassName.setValue(newValue);
145    
146                            // Update in new transaction
147    
148                            classNameLocalService.updateClassName(newClassName);
149    
150                            if (currentSession.contains(className)) {
151                                    throw new IllegalStateException(
152                                            "Entities are still available in Hibernate's first level " +
153                                                    "cache");
154                            }
155    
156                            // Refetch in current transaction
157    
158                            // Clear entity cache to force Hibernate to populate its first level
159                            // cache
160    
161                            EntityCacheUtil.clearCache();
162    
163                            className = classNamePersistence.fetchByPrimaryKey(
164                                    className.getClassNameId());
165    
166                            if (!newValue.equals(className.getValue())) {
167                                    throw new IllegalStateException(
168                                            "Expected " + newValue + " but found " +
169                                                    className.getClassName());
170                            }
171                    }
172                    finally {
173    
174                            // Clean up
175    
176                            classNameLocalService.deleteClassName(className);
177                    }
178            }
179    
180            @Override
181            public void testDeleteClassName() throws PortalException, SystemException {
182                    classNamePersistence.removeByValue(PortalService.class.getName());
183            }
184    
185            @Override
186            public int testGetBuildNumber() {
187                    return portalService.getBuildNumber();
188            }
189    
190            @Override
191            public void testGetUserId() {
192                    long userId = 0;
193    
194                    try {
195                            userId = getUserId();
196                    }
197                    catch (Exception e) {
198                            _log.error(e, e);
199                    }
200    
201                    if (_log.isInfoEnabled()) {
202                            _log.info("User id " + userId);
203                    }
204            }
205    
206            @Override
207            public boolean testHasClassName() throws SystemException {
208                    int count = classNamePersistence.countByValue(
209                            PortalService.class.getName());
210    
211                    if (count > 0) {
212                            return true;
213                    }
214                    else {
215                            return false;
216                    }
217            }
218    
219            protected void addClassName(String classNameValue) throws SystemException {
220                    long classNameId = counterLocalService.increment();
221    
222                    ClassName className = classNamePersistence.create(classNameId);
223    
224                    className.setValue(classNameValue);
225    
226                    classNamePersistence.update(className);
227            }
228    
229            protected void addTransactionPortletBar(
230                            String transactionPortletBarText, boolean rollback)
231                    throws SystemException {
232    
233                    try {
234                            Message message = new Message();
235    
236                            message.put("rollback", rollback);
237                            message.put("text", transactionPortletBarText);
238    
239                            SynchronousMessageSender synchronousMessageSender =
240                                    (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
241                                            DirectSynchronousMessageSender.class.getName());
242    
243                            synchronousMessageSender.send(
244                                    DestinationNames.TEST_TRANSACTION, message);
245                    }
246                    catch (Exception e) {
247                            throw new SystemException(e);
248                    }
249            }
250    
251            private static Log _log = LogFactoryUtil.getLog(PortalServiceImpl.class);
252    
253    }