com.liferay.experts.questions.util.QuestionsUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.experts.questions.util.QuestionsUtil.java

Source

/**
 * 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.questions.util;

import com.liferay.experts.NoSuchQuestionException;
import com.liferay.experts.model.Question;
import com.liferay.experts.service.QuestionLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
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.util.GetterUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ryan Park
 */
public class QuestionsUtil {

    public static List<Question> getQuestions(Hits hits) throws PortalException, SystemException {

        List<Question> questions = new ArrayList<Question>();

        List<Document> documents = hits.toList();

        for (Document document : documents) {
            long questionId = GetterUtil.getLong(document.get(Field.ENTRY_CLASS_PK));

            try {
                Question question = QuestionLocalServiceUtil.getQuestion(questionId);

                questions.add(question);
            } catch (NoSuchQuestionException nsae) {
                Indexer indexer = IndexerRegistryUtil.getIndexer(Question.class);

                long companyId = GetterUtil.getLong(document.get(Field.COMPANY_ID));

                indexer.delete(companyId, String.valueOf(questionId));
            }
        }

        return questions;
    }

}