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