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