How to get unpublished documents in HST Query?

354 views
Skip to first unread message

Global Edge Software Ltd

unread,
Jan 26, 2015, 5:26:20 AM1/26/15
to hippo-c...@googlegroups.com
Hi All,

Is there a way to get only unpublished documents in HST Query?

The following lines get only the published(live version) documents.

HstQuery hstQuery = getQueryManager(request).createQuery(scope,NewsDocumentBean.class,true);
Filter filter = hstQuery.createFilter();
HstQueryResult hstResult = hstQuery.execute();


Ard Schrijvers

unread,
Jan 26, 2015, 5:48:29 AM1/26/15
to hippo-c...@googlegroups.com
You can only get unpublished documents in the preview. By default, the
channel mngr in the cms shows preview documents. If you want to expose
the preview outside the cms, you need to add a hst:mount with
hst:type=preview : Then a preview jcr session is used which will use
the unpublished documents. Obviously, you should include a login for
such a preview mount

Regards Ard
> --
> Hippo Community Group: The place for all discussions and announcements about
> Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to hippo-c...@googlegroups.com
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google Groups
> "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to hippo-communi...@googlegroups.com.
> Visit this group at http://groups.google.com/group/hippo-community.
> For more options, visit https://groups.google.com/d/optout.



--
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
Boston - 1 Broadway, Cambridge, MA 02142

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com

Global Edge Software Ltd

unread,
Jan 26, 2015, 6:13:58 AM1/26/15
to hippo-c...@googlegroups.com
I use the below code to login as previewuser, but how do I use this preview session in HST query?

HippoRepository repository = HippoRepositoryFactory.getHippoRepository();
Session jcrSession = repository.login("previewuser", "previewuserpass".toCharArray());

Thank you

Ard Schrijvers

unread,
Jan 26, 2015, 6:17:54 AM1/26/15
to hippo-c...@googlegroups.com
On Mon, Jan 26, 2015 at 12:13 PM, Global Edge Software Ltd
<ges.ma...@gmail.com> wrote:
> I use the below code to login as previewuser, but how do I use this preview
> session in HST query?
>
> HippoRepository repository = HippoRepositoryFactory.getHippoRepository();
> Session jcrSession = repository.login("previewuser",
> "previewuserpass".toCharArray());

You would then have to create your own HstQueryManagerImpl instance
and use that.

Personally I strongly recommend not to login a preview user from a
live context and use a preview site instead

Regards Ard

Global Edge Software Ltd

unread,
Jan 26, 2015, 7:00:35 AM1/26/15
to hippo-c...@googlegroups.com
Hi,
Below is my complete code, but not getting the unpublished document


HippoRepository repository = HippoRepositoryFactory.getHippoRepository();
Session previewSession = repository.login("previewuser", "previewuserpass".toCharArray());
HstQueryManager queryManager = new HstQueryManagerImpl(previewSession, objectConverter, DateTools.Resolution.YEAR);
HstQuery hstQuery = queryManager.createQuery(scope, NewsDocumentBean.class, true);
HstQueryResult hstResult = hstQuery.execute();

HST Result

hstQuery = (org.hippoecm.hst.content.beans.query.HstQueryImpl) org.hippoecm.hst.content.beans.query.HstQueryImpl@9516b3 = //element(*,estro:NewsDocument)[(@hippo:paths='cca3158f-f965-4008-8b68-c2e1af8759b3') and (@hippo:availability='live') and not(@jcr:primaryType='nt:frozenNode')] order by @jcr:score descending

Could you direct me with the right code?

Ard Schrijvers

unread,
Jan 26, 2015, 7:20:25 AM1/26/15
to hippo-c...@googlegroups.com
On Mon, Jan 26, 2015 at 1:00 PM, Global Edge Software Ltd
<ges.ma...@gmail.com> wrote:
> Hi,
> Below is my complete code, but not getting the unpublished document
>
>
> HippoRepository repository = HippoRepositoryFactory.getHippoRepository();
> Session previewSession = repository.login("previewuser",
> "previewuserpass".toCharArray());
> HstQueryManager queryManager = new HstQueryManagerImpl(previewSession,
> objectConverter, DateTools.Resolution.YEAR);
> HstQuery hstQuery = queryManager.createQuery(scope, NewsDocumentBean.class,
> true);
> HstQueryResult hstResult = hstQuery.execute();
>
> HST Result
>
> hstQuery = (org.hippoecm.hst.content.beans.query.HstQueryImpl)
> org.hippoecm.hst.content.beans.query.HstQueryImpl@9516b3 =
> //element(*,estro:NewsDocument)[(@hippo:paths='cca3158f-f965-4008-8b68-c2e1af8759b3')
> and (@hippo:availability='live') and not(@jcr:primaryType='nt:frozenNode')]
> order by @jcr:score descending
>
> Could you direct me with the right code?

Ah apologies, I see that the query is executed with the session tied
to 'scope', which contains a jcr node that is created with the live
session. So you'd first also have to fetch the scope bean with the
preview session.

Either way, as said, I personally discourage your usage. Not sure what
the requirement is. Perhaps it validates your preview user session in
a live context

Regards Ard

Global Edge Software Ltd

unread,
Jan 26, 2015, 7:46:07 AM1/26/15
to hippo-c...@googlegroups.com
How do I get the scope bean with preview session?.

The following code returns the scope with live session

HippoBean scope = getSiteContentBaseBean(request);

Thank you.

Ard Schrijvers

unread,
Jan 26, 2015, 7:49:26 AM1/26/15
to hippo-c...@googlegroups.com
On Mon, Jan 26, 2015 at 1:46 PM, Global Edge Software Ltd
<ges.ma...@gmail.com> wrote:
> How do I get the scope bean with preview session?.
>
> The following code returns the scope with live session
>
> HippoBean scope = getSiteContentBaseBean(request);

You can use

hstRequest.getHstRequestContext().getObjectConverter().getObject(previewSession,
scope.getPath());

Regards Ard

Global Edge Software Ltd

unread,
Jan 26, 2015, 8:16:48 AM1/26/15
to hippo-c...@googlegroups.com
There is no getHstRequestContext() method available in HstRequest class, but getRequestContext().
The HstRequestContext class does not have a method called getObjectConverter, but getObjectBeanManager(Session sn).

My requirement is, members (with login) will create some document from site using form and then they should be able to view/edit the unpublished document. I am able to create documents from site using sitewriter session and not able to list the unpublished document.

How can I do this?

Thank you.

Ard Schrijvers

unread,
Jan 26, 2015, 8:23:33 AM1/26/15
to hippo-c...@googlegroups.com
On Mon, Jan 26, 2015 at 2:16 PM, Global Edge Software Ltd
<ges.ma...@gmail.com> wrote:
> There is no getHstRequestContext() method available in HstRequest class, but
> getRequestContext().
> The HstRequestContext class does not have a method called
> getObjectConverter, but getObjectBeanManager(Session sn).

Ah apologies, yes, you need those. I was writing it from the top of my head

>
> My requirement is, members (with login) will create some document from site
> using form and then they should be able to view/edit the unpublished
> document. I am able to create documents from site using sitewriter session
> and not able to list the unpublished document.
>
> How can I do this?

Well, you can do it the way you are trying by using a preview session
in a live context. A complete alternative would be to add a rest
endpoint with a separate mount that has hst:type = preview that you
use to fetch unpublished documents with via a ajax call....or an ssi
or esi (server side / edge side include).

Global Edge Software Ltd

unread,
Jan 27, 2015, 2:16:43 AM1/27/15
to hippo-c...@googlegroups.com
Hi,

I still get the published documents only.

HippoRepository repository = HippoRepositoryFactory.getHippoRepository();
Session previewSession = repository.login("previewuser", "previewuserpass".toCharArray());
Node scope = previewSession.getRootNode().getNode("content/documents/demo");
HstRequestContext requestContext = RequestContextProvider.get();
HstQuery hstQuery = requestContext.getContentBeansTool().createQueryManager(previewSession).createQuery(scope,NewsDocumentBean.class,true);
Filter mloFilter = hstQuery.createFilter();
hstQuery.setFilter(mloFilter);
HstQueryResult hstResult = hstQuery.execute();

How can I modify the above codes to get the unpublished document instead of using separate mount point?.

Ard Schrijvers

unread,
Jan 27, 2015, 4:45:35 AM1/27/15
to hippo-c...@googlegroups.com
Hello,

I've tried (and succeeded) to reproduce your problem. Before getting
to the problem, first a small thing: Instead of using

HippoRepository repository = HippoRepositoryFactory.getHippoRepository();
Session previewSession = repository.login("previewuser",
"previewuserpass".toCharArray());

you can better use

final Repository repository =
HstServices.getComponentManager().getComponent(Repository.class.getName());
final SimpleCredentials previewCreds =
HstServices.getComponentManager().getComponent("javax.jcr.Credentials.preview");
final Session previewSession = repository.login(previewCreds);

This way, you get a preview session from a pool managed by the HST.

So I tried the following code:

try {

final Repository repository =
HstServices.getComponentManager().getComponent(Repository.class.getName());
final SimpleCredentials previewCreds =
HstServices.getComponentManager().getComponent("javax.jcr.Credentials.preview");
final Session previewSession = repository.login(previewCreds);
final Node previewScope =
previewSession.getRootNode().getNode("content/documents/gettingstarted");
HstRequestContext requestContext = RequestContextProvider.get();
HstQuery hstQuery =
requestContext.getContentBeansTool().createQueryManager(previewSession).createQuery(previewScope,NewsDocument.class,true);
Filter mloFilter = hstQuery.createFilter();
hstQuery.setFilter(mloFilter);
HstQueryResult hstResult = hstQuery.execute();

final HippoBeanIterator hippoBeans = hstResult.getHippoBeans();
while(hippoBeans.hasNext()) {
final HippoBean hippoBean = hippoBeans.nextHippoBean();
System.out.println(hippoBean.getPath());

System.out.println(hippoBean.getNode().getSession().getUserID());

System.out.println(Arrays.toString(hippoBean.getValueProvider().getStrings("hippo:availability")));
}

} catch (RepositoryException e) {
e.printStackTrace();
} catch (QueryException e) {
e.printStackTrace();
}


What the result is, is that you do get search results from the preview
session (hippoBean.getNode().getSession().getUserID() returns
previewuser), however, the HstQueryImpl contains the following

if ( RequestContextProvider.get() != null ) {
if (RequestContextProvider.get().isPreview() ) {
query.append(" and
(@").append(HippoNodeType.HIPPO_AVAILABILITY).append("='preview'").append(")");
} else {
query.append(" and
(@").append(HippoNodeType.HIPPO_AVAILABILITY).append("='live'").append(")");
}
}


Since the request context is for live, the query gets included that
hippo availability must be live.

We could make an enhancement to be able to search for hippo
availability 'preview' by for example making it possible to set it
explicitly. From a session itself, we cannot know whether it is meant
for a preview or live session (I do not want to deduce that from the
user name containing 'preview' or something)

You can file an improvement issue for this if you like and contact
helpdesk to get in planned in. Apologies for the inconvenience

Regards Ard

On Tue, Jan 27, 2015 at 8:16 AM, Global Edge Software Ltd

Global Edge Software Ltd

unread,
Jan 27, 2015, 7:18:46 AM1/27/15
to hippo-c...@googlegroups.com
Thank you.
Reply all
Reply to author
Forward
0 new messages