Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.experts.service.impl; import com.liferay.experts.model.Question; import com.liferay.experts.service.base.QuestionLocalServiceBaseImpl; import com.liferay.experts.util.StatusConstants; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.search.Hits; import com.liferay.portal.kernel.search.Indexer; import com.liferay.portal.kernel.search.IndexerRegistryUtil; import com.liferay.portal.kernel.search.QueryConfig; import com.liferay.portal.kernel.search.SearchContext; import com.liferay.portal.kernel.search.Sort; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.model.User; import com.liferay.portal.service.ServiceContext; import com.liferay.portal.util.PortalUtil; import com.liferay.portlet.ratings.model.RatingsStats; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; /** * @author Ryan Park */ public class QuestionLocalServiceImpl extends QuestionLocalServiceBaseImpl { public Question addQuestion(long userId, String title, String content, ServiceContext serviceContext) throws PortalException, SystemException { User user = userPersistence.findByPrimaryKey(userId); long groupId = serviceContext.getScopeGroupId(); Date now = new Date(); long questionId = counterLocalService.increment(); Question question = questionPersistence.create(questionId); question.setCompanyId(user.getCompanyId()); question.setGroupId(groupId); question.setUserId(user.getUserId()); question.setUserName(user.getFullName()); question.setCreateDate(now); question.setModifiedDate(now); question.setTitle(title); question.setContent(content); question.setRating(0); question.setStatus(StatusConstants.APPROVED); question.setStatusByUserId(user.getUserId()); question.setStatusByUserName(user.getFullName()); questionPersistence.update(question, false); // Asset entry assetEntryLocalService.updateEntry(user.getUserId(), groupId, Question.class.getName(), question.getQuestionId(), serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames()); // Ratings ratingsStatsLocalService.addStats(PortalUtil.getClassNameId(Question.class), question.getQuestionId()); rateQuestion(userId, question.getQuestionId(), true); // Indexer Indexer indexer = IndexerRegistryUtil.getIndexer(Question.class); indexer.reindex(question); return question; } @Override public Question deleteQuestion(long questionId) throws PortalException, SystemException { Question question = questionPersistence.findByPrimaryKey(questionId); return deleteQuestion(question); } @Override public Question deleteQuestion(Question question) throws PortalException, SystemException { questionPersistence.remove(question); // Answer answerLocalService.deleteAnswers(question.getQuestionId()); // Asset entry assetEntryLocalService.deleteEntry(Question.class.getName(), question.getQuestionId()); // Indexer Indexer indexer = IndexerRegistryUtil.getIndexer(Question.class); indexer.delete(question); // Ratings ratingsStatsLocalService.deleteStats(Question.class.getName(), question.getQuestionId()); return question; } public List<Question> getQuestions(long groupId, long assetTagId, int start, int end) throws SystemException { if (assetTagId > 0) { return questionFinder.findByGI_ATI(groupId, assetTagId, start, end); } else { return questionPersistence.findByGroupId(groupId, start, end); } } public List<Question> getQuestionsByUserId(long groupId, long userId, int start, int end) throws SystemException { return questionPersistence.findByGI_UI_S(groupId, userId, StatusConstants.APPROVED, start, end); } public int getQuestionsByUserIdCount(long groupId, long userId) throws SystemException { return questionPersistence.countByGI_UI_S(groupId, userId, StatusConstants.APPROVED); } public int getQuestionsCount(long groupId, long assetTagId) throws SystemException { if (assetTagId > 0) { return questionFinder.countByGI_ATI(groupId, assetTagId); } else { return questionPersistence.countByGroupId(groupId); } } public Question rateQuestion(long userId, long questionId, boolean promote) throws PortalException, SystemException { Date now = new Date(); Question question = questionPersistence.findByPrimaryKey(questionId); if (promote) { ServiceContext serviceContext = new ServiceContext(); serviceContext.setModifiedDate(now); ratingsEntryLocalService.updateEntry(userId, Question.class.getName(), question.getQuestionId(), 1.0, serviceContext); } else { ratingsEntryLocalService.deleteEntry(userId, Question.class.getName(), question.getQuestionId()); } RatingsStats ratingsStats = ratingsStatsLocalService.getStats(Question.class.getName(), question.getQuestionId()); question.setRating((int) ratingsStats.getTotalScore()); questionPersistence.update(question, false); return question; } public Hits search(long companyId, long groupId, String keywords, LinkedHashMap<String, Object> params, int start, int end, Sort sort) throws SystemException { try { SearchContext searchContext = new SearchContext(); searchContext.setCompanyId(companyId); searchContext.setEnd(end); searchContext.setGroupIds(new long[] { groupId }); if (params != null) { if (Validator.isNull(keywords)) { keywords = (String) params.get("keywords"); } params.remove("keywords"); } if (Validator.isNotNull(keywords)) { searchContext.setKeywords(keywords); } QueryConfig queryConfig = new QueryConfig(); queryConfig.setHighlightEnabled(false); searchContext.setQueryConfig(queryConfig); if (sort != null) { searchContext.setSorts(new Sort[] { sort }); } searchContext.setStart(start); Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(Question.class); return indexer.search(searchContext); } catch (Exception e) { throw new SystemException(e); } } public Question updateQuestion(long questionId, String title, String content, ServiceContext serviceContext) throws PortalException, SystemException { Question question = questionPersistence.findByPrimaryKey(questionId); question.setModifiedDate(new Date()); question.setTitle(title); question.setContent(content); questionPersistence.update(question, false); // Asset entry assetEntryLocalService.updateEntry(question.getUserId(), question.getGroupId(), Question.class.getName(), question.getQuestionId(), serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames()); // Indexer Indexer indexer = IndexerRegistryUtil.getIndexer(Question.class); indexer.reindex(question); return question; } }