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.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() {
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                    addClassName(classNameValue);
063    
064                    throw new SystemException();
065            }
066    
067            @Override
068            public void testAddClassName_Success(String classNameValue) {
069                    addClassName(classNameValue);
070            }
071    
072            @Override
073            public void testAddClassNameAndTestTransactionPortletBar_PortalRollback(
074                    String transactionPortletBarText) {
075    
076                    addClassName(PortalService.class.getName());
077    
078                    addTransactionPortletBar(transactionPortletBarText, false);
079    
080                    throw new SystemException();
081            }
082    
083            @Override
084            public void testAddClassNameAndTestTransactionPortletBar_PortletRollback(
085                    String transactionPortletBarText) {
086    
087                    addClassName(PortalService.class.getName());
088    
089                    addTransactionPortletBar(transactionPortletBarText, true);
090            }
091    
092            @Override
093            public void testAddClassNameAndTestTransactionPortletBar_Success(
094                    String transactionPortletBarText) {
095    
096                    addClassName(PortalService.class.getName());
097    
098                    addTransactionPortletBar(transactionPortletBarText, false);
099            }
100    
101            @Override
102            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
103            public void testAutoSyncHibernateSessionStateOnTxCreation() {
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            @Override
173            public void testDeleteClassName() throws PortalException {
174                    classNamePersistence.removeByValue(PortalService.class.getName());
175            }
176    
177            @Override
178            public int testGetBuildNumber() {
179                    return portalService.getBuildNumber();
180            }
181    
182            @Override
183            public void testGetUserId() {
184                    long userId = 0;
185    
186                    try {
187                            userId = getUserId();
188                    }
189                    catch (Exception e) {
190                            _log.error(e, e);
191                    }
192    
193                    if (_log.isInfoEnabled()) {
194                            _log.info("User id " + userId);
195                    }
196            }
197    
198            @Override
199            public boolean testHasClassName() {
200                    int count = classNamePersistence.countByValue(
201                            PortalService.class.getName());
202    
203                    if (count > 0) {
204                            return true;
205                    }
206                    else {
207                            return false;
208                    }
209            }
210    
211            protected void addClassName(String classNameValue) {
212                    long classNameId = counterLocalService.increment();
213    
214                    ClassName className = classNamePersistence.create(classNameId);
215    
216                    className.setValue(classNameValue);
217    
218                    classNamePersistence.update(className);
219            }
220    
221            protected void addTransactionPortletBar(
222                    String transactionPortletBarText, boolean rollback) {
223    
224                    try {
225                            Message message = new Message();
226    
227                            message.put("rollback", rollback);
228                            message.put("text", transactionPortletBarText);
229    
230                            SynchronousMessageSender synchronousMessageSender =
231                                    (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
232                                            DirectSynchronousMessageSender.class.getName());
233    
234                            synchronousMessageSender.send(
235                                    DestinationNames.TEST_TRANSACTION, message);
236                    }
237                    catch (Exception e) {
238                            throw new SystemException(e);
239                    }
240            }
241    
242            private static final Log _log = LogFactoryUtil.getLog(
243                    PortalServiceImpl.class);
244    
245    }