Search in sources :

Example 11 with KBTemplate

use of com.liferay.knowledgebase.model.KBTemplate in project liferay-ide by liferay.

the class KBTemplatePersistenceImpl method fetchByUUID_G.

/**
 * Returns the k b template where uuid = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
 *
 * @param uuid the uuid
 * @param groupId the group ID
 * @param retrieveFromCache whether to use the finder cache
 * @return the matching k b template, or <code>null</code> if a matching k b template could not be found
 * @throws SystemException if a system exception occurred
 */
@Override
public KBTemplate fetchByUUID_G(String uuid, long groupId, boolean retrieveFromCache) throws SystemException {
    Object[] finderArgs = new Object[] { uuid, groupId };
    Object result = null;
    if (retrieveFromCache) {
        result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_UUID_G, finderArgs, this);
    }
    if (result instanceof KBTemplate) {
        KBTemplate kbTemplate = (KBTemplate) result;
        if (!Validator.equals(uuid, kbTemplate.getUuid()) || (groupId != kbTemplate.getGroupId())) {
            result = null;
        }
    }
    if (result == null) {
        StringBundler query = new StringBundler(4);
        query.append(_SQL_SELECT_KBTEMPLATE_WHERE);
        boolean bindUuid = false;
        if (uuid == null) {
            query.append(_FINDER_COLUMN_UUID_G_UUID_1);
        } else if (uuid.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_UUID_G_UUID_3);
        } else {
            bindUuid = true;
            query.append(_FINDER_COLUMN_UUID_G_UUID_2);
        }
        query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);
        String sql = query.toString();
        Session session = null;
        try {
            session = openSession();
            Query q = session.createQuery(sql);
            QueryPos qPos = QueryPos.getInstance(q);
            if (bindUuid) {
                qPos.add(uuid);
            }
            qPos.add(groupId);
            List<KBTemplate> list = q.list();
            if (list.isEmpty()) {
                FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, finderArgs, list);
            } else {
                KBTemplate kbTemplate = list.get(0);
                result = kbTemplate;
                cacheResult(kbTemplate);
                if ((kbTemplate.getUuid() == null) || !kbTemplate.getUuid().equals(uuid) || (kbTemplate.getGroupId() != groupId)) {
                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G, finderArgs, kbTemplate);
                }
            }
        } catch (Exception e) {
            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G, finderArgs);
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }
    if (result instanceof List<?>) {
        return null;
    } else {
        return (KBTemplate) result;
    }
}
Also used : SQLQuery(com.liferay.portal.kernel.dao.orm.SQLQuery) Query(com.liferay.portal.kernel.dao.orm.Query) ArrayList(java.util.ArrayList) UnmodifiableList(com.liferay.portal.kernel.util.UnmodifiableList) List(java.util.List) KBTemplate(com.liferay.knowledgebase.model.KBTemplate) QueryPos(com.liferay.portal.kernel.dao.orm.QueryPos) StringBundler(com.liferay.portal.kernel.util.StringBundler) NoSuchTemplateException(com.liferay.knowledgebase.NoSuchTemplateException) SystemException(com.liferay.portal.kernel.exception.SystemException) Session(com.liferay.portal.kernel.dao.orm.Session)

Example 12 with KBTemplate

use of com.liferay.knowledgebase.model.KBTemplate in project liferay-ide by liferay.

the class KBTemplatePersistenceImpl method remove.

/**
 * Removes the k b template with the primary key from the database. Also notifies the appropriate model listeners.
 *
 * @param primaryKey the primary key of the k b template
 * @return the k b template that was removed
 * @throws com.liferay.knowledgebase.NoSuchTemplateException if a k b template with the primary key could not be found
 * @throws SystemException if a system exception occurred
 */
@Override
public KBTemplate remove(Serializable primaryKey) throws NoSuchTemplateException, SystemException {
    Session session = null;
    try {
        session = openSession();
        KBTemplate kbTemplate = (KBTemplate) session.get(KBTemplateImpl.class, primaryKey);
        if (kbTemplate == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }
            throw new NoSuchTemplateException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
        }
        return remove(kbTemplate);
    } catch (NoSuchTemplateException nsee) {
        throw nsee;
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }
}
Also used : NoSuchTemplateException(com.liferay.knowledgebase.NoSuchTemplateException) KBTemplateImpl(com.liferay.knowledgebase.model.impl.KBTemplateImpl) KBTemplate(com.liferay.knowledgebase.model.KBTemplate) NoSuchTemplateException(com.liferay.knowledgebase.NoSuchTemplateException) SystemException(com.liferay.portal.kernel.exception.SystemException) Session(com.liferay.portal.kernel.dao.orm.Session)

Example 13 with KBTemplate

use of com.liferay.knowledgebase.model.KBTemplate in project liferay-ide by liferay.

the class KBTemplatePersistenceImpl method getByUuid_PrevAndNext.

protected KBTemplate getByUuid_PrevAndNext(Session session, KBTemplate kbTemplate, String uuid, OrderByComparator orderByComparator, boolean previous) {
    StringBundler query = null;
    if (orderByComparator != null) {
        query = new StringBundler(6 + (orderByComparator.getOrderByFields().length * 6));
    } else {
        query = new StringBundler(3);
    }
    query.append(_SQL_SELECT_KBTEMPLATE_WHERE);
    boolean bindUuid = false;
    if (uuid == null) {
        query.append(_FINDER_COLUMN_UUID_UUID_1);
    } else if (uuid.equals(StringPool.BLANK)) {
        query.append(_FINDER_COLUMN_UUID_UUID_3);
    } else {
        bindUuid = true;
        query.append(_FINDER_COLUMN_UUID_UUID_2);
    }
    if (orderByComparator != null) {
        String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
        if (orderByConditionFields.length > 0) {
            query.append(WHERE_AND);
        }
        for (int i = 0; i < orderByConditionFields.length; i++) {
            query.append(_ORDER_BY_ENTITY_ALIAS);
            query.append(orderByConditionFields[i]);
            if ((i + 1) < orderByConditionFields.length) {
                if (orderByComparator.isAscending() ^ previous) {
                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
                } else {
                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
                }
            } else {
                if (orderByComparator.isAscending() ^ previous) {
                    query.append(WHERE_GREATER_THAN);
                } else {
                    query.append(WHERE_LESSER_THAN);
                }
            }
        }
        query.append(ORDER_BY_CLAUSE);
        String[] orderByFields = orderByComparator.getOrderByFields();
        for (int i = 0; i < orderByFields.length; i++) {
            query.append(_ORDER_BY_ENTITY_ALIAS);
            query.append(orderByFields[i]);
            if ((i + 1) < orderByFields.length) {
                if (orderByComparator.isAscending() ^ previous) {
                    query.append(ORDER_BY_ASC_HAS_NEXT);
                } else {
                    query.append(ORDER_BY_DESC_HAS_NEXT);
                }
            } else {
                if (orderByComparator.isAscending() ^ previous) {
                    query.append(ORDER_BY_ASC);
                } else {
                    query.append(ORDER_BY_DESC);
                }
            }
        }
    } else {
        query.append(KBTemplateModelImpl.ORDER_BY_JPQL);
    }
    String sql = query.toString();
    Query q = session.createQuery(sql);
    q.setFirstResult(0);
    q.setMaxResults(2);
    QueryPos qPos = QueryPos.getInstance(q);
    if (bindUuid) {
        qPos.add(uuid);
    }
    if (orderByComparator != null) {
        Object[] values = orderByComparator.getOrderByConditionValues(kbTemplate);
        for (Object value : values) {
            qPos.add(value);
        }
    }
    List<KBTemplate> list = q.list();
    if (list.size() == 2) {
        return list.get(1);
    } else {
        return null;
    }
}
Also used : SQLQuery(com.liferay.portal.kernel.dao.orm.SQLQuery) Query(com.liferay.portal.kernel.dao.orm.Query) QueryPos(com.liferay.portal.kernel.dao.orm.QueryPos) KBTemplate(com.liferay.knowledgebase.model.KBTemplate) StringBundler(com.liferay.portal.kernel.util.StringBundler)

Example 14 with KBTemplate

use of com.liferay.knowledgebase.model.KBTemplate in project liferay-ide by liferay.

the class KBTemplatePersistenceImpl method findByUuid_Last.

/**
 * Returns the last k b template in the ordered set where uuid = &#63;.
 *
 * @param uuid the uuid
 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
 * @return the last matching k b template
 * @throws com.liferay.knowledgebase.NoSuchTemplateException if a matching k b template could not be found
 * @throws SystemException if a system exception occurred
 */
@Override
public KBTemplate findByUuid_Last(String uuid, OrderByComparator orderByComparator) throws NoSuchTemplateException, SystemException {
    KBTemplate kbTemplate = fetchByUuid_Last(uuid, orderByComparator);
    if (kbTemplate != null) {
        return kbTemplate;
    }
    StringBundler msg = new StringBundler(4);
    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
    msg.append("uuid=");
    msg.append(uuid);
    msg.append(StringPool.CLOSE_CURLY_BRACE);
    throw new NoSuchTemplateException(msg.toString());
}
Also used : NoSuchTemplateException(com.liferay.knowledgebase.NoSuchTemplateException) KBTemplate(com.liferay.knowledgebase.model.KBTemplate) StringBundler(com.liferay.portal.kernel.util.StringBundler)

Example 15 with KBTemplate

use of com.liferay.knowledgebase.model.KBTemplate in project liferay-ide by liferay.

the class KBTemplateLocalServiceImpl method addKBTemplate.

@Override
public KBTemplate addKBTemplate(long userId, String title, String content, ServiceContext serviceContext) throws PortalException, SystemException {
    // KB template
    User user = userPersistence.findByPrimaryKey(userId);
    long groupId = serviceContext.getScopeGroupId();
    Date now = new Date();
    validate(title, content);
    long kbTemplateId = counterLocalService.increment();
    KBTemplate kbTemplate = kbTemplatePersistence.create(kbTemplateId);
    kbTemplate.setUuid(serviceContext.getUuid());
    kbTemplate.setGroupId(groupId);
    kbTemplate.setCompanyId(user.getCompanyId());
    kbTemplate.setUserId(user.getUserId());
    kbTemplate.setUserName(user.getFullName());
    kbTemplate.setCreateDate(serviceContext.getCreateDate(now));
    kbTemplate.setModifiedDate(serviceContext.getModifiedDate(now));
    kbTemplate.setTitle(title);
    kbTemplate.setContent(content);
    kbTemplatePersistence.update(kbTemplate);
    // Resources
    resourceLocalService.addModelResources(kbTemplate, serviceContext);
    // Social
    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
    extraDataJSONObject.put("title", kbTemplate.getTitle());
    socialActivityLocalService.addActivity(userId, groupId, KBTemplate.class.getName(), kbTemplateId, AdminActivityKeys.ADD_KB_TEMPLATE, extraDataJSONObject.toString(), 0);
    return kbTemplate;
}
Also used : User(com.liferay.portal.model.User) JSONObject(com.liferay.portal.kernel.json.JSONObject) KBTemplate(com.liferay.knowledgebase.model.KBTemplate) Date(java.util.Date)

Aggregations

KBTemplate (com.liferay.knowledgebase.model.KBTemplate)40 NoSuchTemplateException (com.liferay.knowledgebase.NoSuchTemplateException)21 StringBundler (com.liferay.portal.kernel.util.StringBundler)17 SystemException (com.liferay.portal.kernel.exception.SystemException)14 Session (com.liferay.portal.kernel.dao.orm.Session)12 SQLQuery (com.liferay.portal.kernel.dao.orm.SQLQuery)10 QueryPos (com.liferay.portal.kernel.dao.orm.QueryPos)9 Query (com.liferay.portal.kernel.dao.orm.Query)8 KBTemplateImpl (com.liferay.knowledgebase.model.impl.KBTemplateImpl)7 ArrayList (java.util.ArrayList)7 UnmodifiableList (com.liferay.portal.kernel.util.UnmodifiableList)6 List (java.util.List)6 FinderPath (com.liferay.portal.kernel.dao.orm.FinderPath)4 KBArticle (com.liferay.knowledgebase.model.KBArticle)3 PortalException (com.liferay.portal.kernel.exception.PortalException)2 JSONObject (com.liferay.portal.kernel.json.JSONObject)2 KBArticleImportException (com.liferay.knowledgebase.KBArticleImportException)1 KBCommentContentException (com.liferay.knowledgebase.KBCommentContentException)1 KBTemplateContentException (com.liferay.knowledgebase.KBTemplateContentException)1 KBTemplateTitleException (com.liferay.knowledgebase.KBTemplateTitleException)1