Skip to content

Commit

Permalink
LA-22: Scope should display total number of items to be found [#22]
Browse files Browse the repository at this point in the history
  • Loading branch information
max-dev committed Dec 24, 2012
1 parent bfaac97 commit bf5fe94
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public static <T> ScopeMetadata specification( Specification<T> specification )
return new SpecificationScopeMetadata<T>( specification ).name( specification.getClass().getSimpleName() );
}

public static boolean isSpecificationScope( final ScopeMetadata scope ) {
return scope instanceof ScopeMetadataUtils.SpecificationScopeMetadata;
}

public static boolean isPredicateScope( final ScopeMetadata scope ) {
return scope instanceof ScopeMetadataUtils.PredicateScopeMetadata;
}

public static class SpecificationScopeMetadata<T> extends AbstractScope {

private transient final Specification<T> specification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.newLinkedList;
import static com.google.common.collect.Maps.newHashMap;
import static org.lightadmin.core.config.domain.scope.ScopeMetadataUtils.isPredicateScope;
import static org.lightadmin.core.config.domain.scope.ScopeMetadataUtils.isSpecificationScope;

@SuppressWarnings( "unchecked" )
@RequestMapping( "/rest" )
Expand Down Expand Up @@ -171,14 +173,6 @@ private Page<?> selectPage( List<Object> items, PagingAndSorting pageSort ) {
return new PageImpl<Object>( itemsOnPage, pageSort, items.size() );
}

private boolean isSpecificationScope( final ScopeMetadata scope ) {
return scope instanceof ScopeMetadataUtils.SpecificationScopeMetadata;
}

private boolean isPredicateScope( final ScopeMetadata scope ) {
return scope instanceof ScopeMetadataUtils.PredicateScopeMetadata;
}

private Specification and( Specification specification, Specification otherSpecification ) {
return Specifications.where( specification ).and( otherSpecification );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package org.lightadmin.core.view.preparer;

import com.google.common.collect.Collections2;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.lightadmin.core.config.domain.DomainTypeAdministrationConfiguration;
import org.lightadmin.core.config.domain.scope.ScopeMetadata;
import org.lightadmin.core.config.domain.scope.ScopeMetadataUtils;
import org.lightadmin.core.persistence.repository.DynamicJpaRepository;
import org.lightadmin.core.util.Pair;

import java.util.List;

import static com.google.common.collect.Lists.newLinkedList;
import static org.lightadmin.core.config.domain.scope.ScopeMetadataUtils.*;

public class ListViewPreparer extends ConfigurationAwareViewPreparer {

Expand All @@ -12,8 +22,39 @@ protected void execute( final TilesRequestContext tilesContext, final AttributeC

addAttribute( attributeContext, "fields", configuration.getListViewFragment().getFields() );

addAttribute( attributeContext, "scopes", configuration.getScopes() );
addAttribute( attributeContext, "scopes", scopes( configuration ) );

addAttribute( attributeContext, "filters", configuration.getFilters() );
}

private List<Pair<? extends ScopeMetadata, Long>> scopes( DomainTypeAdministrationConfiguration configuration ) {
final List<Pair<? extends ScopeMetadata, Long>> result = newLinkedList();
final Iterable<ScopeMetadata> scopes = configuration.getScopes();

final DynamicJpaRepository<?, ?> repository = configuration.getRepository();

for ( ScopeMetadata scope : scopes ) {
if ( isPredicateScope( scope ) ) {
result.add( scopeWithRecordsCount( ( PredicateScopeMetadata ) scope, repository ) );
} else if ( isSpecificationScope( scope ) ) {
result.add( scopeWithRecordsCount( ( ScopeMetadataUtils.SpecificationScopeMetadata ) scope, repository ) );
} else {
result.add( Pair.create( scope, repository.count() ) );
}
}

return result;
}

@SuppressWarnings( "unchecked" )
private Pair<? extends ScopeMetadata, Long> scopeWithRecordsCount( final ScopeMetadataUtils.SpecificationScopeMetadata scope, final DynamicJpaRepository<?, ?> repository ) {
return Pair.create( scope, repository.count( scope.specification() ) );
}

@SuppressWarnings( "unchecked" )
private Pair<? extends ScopeMetadata, Long> scopeWithRecordsCount( final PredicateScopeMetadata scope, final DynamicJpaRepository<?, ?> repository ) {
long recordsCount = Collections2.filter( repository.findAll(), scope.predicate() ).size();

return Pair.create( scope, recordsCount );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<%@ attribute name="domainTypeName" required="true" type="java.lang.String"%>
<%@ attribute name="fields" required="true" type="java.util.Set"%>
<%@ attribute name="scopes" required="true" type="org.lightadmin.core.config.domain.scope.ScopesConfigurationUnit"%>
<%@ attribute name="scopes" required="true" type="java.util.List"%>
<%@ attribute name="domainTypeEntityMetadata" required="true" rtexprvalue="true" type="org.lightadmin.core.persistence.metamodel.DomainTypeEntityMetadata"%>

<c:set var="primaryKeyField" value="${light:primaryKeyPersistentField(fields)}"/>
Expand Down
18 changes: 9 additions & 9 deletions lightadmin-demo/src/main/webapp/WEB-INF/tags/scopes.tag
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<%@ tag import="com.google.common.collect.Iterables" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="light" uri="http://www.lightadmin.org/tags" %>

<%@ attribute name="scopes" required="true" rtexprvalue="true" type="org.lightadmin.core.config.domain.scope.ScopesConfigurationUnit"%>
<%@ attribute name="scopes" required="true" rtexprvalue="true" type="java.util.List"%>

<tiles:useAttribute name="domainTypeAdministrationConfiguration"/>

<c:set var="domainTypeName" value="${domainTypeAdministrationConfiguration.domainTypeName}"/>

<spring:url var="domainRestScopeBaseUrl" value="${light:domainRestScopeBaseUrl(domainTypeName)}" scope="page"/>

<c:set var="tag_scopes_scopeList" value="<%= Iterables.toArray( scopes, org.lightadmin.core.config.domain.scope.ScopeMetadata.class ) %>"/>

<c:if test="${not empty tag_scopes_scopeList}">
<c:if test="${not empty scopes}">
<div class="scopes" id="scopes">
<ul>
<c:forEach var="scope" items="${tag_scopes_scopeList}">
<c:forEach var="scope" items="${scopes}">
<li>
<a scope-name="${scope.name}" class="scope ${scope.defaultScope ? 'active green' : 'blue' }" href="#${scope.name}"><c:out value="${scope.name}"/></a>
<a scope-name="${scope.first.name}" class="scope ${scope.first.defaultScope ? 'active green' : 'blue' }" href="#${scope.first.name}"><c:out value="${scope.first.name}"/>&nbsp;(<c:out value="${scope.second}"/>)</a>
</li>
</c:forEach>
</ul>
Expand All @@ -34,8 +30,12 @@
return '${domainRestScopeBaseUrl}' + '/' + scopeName + '/search';
}
function activeScope() {
return $("a.scope.active" );
}
function activeScopeName() {
return $("a.scope.active" ).attr('scope-name');
return activeScope().attr('scope-name');
}
function activateScope( scope ) {
Expand Down
2 changes: 2 additions & 0 deletions lightadmin-demo/src/main/webapp/scripts/lightadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function dataTableRESTAdapter( sSource, aoData, fnCallback ) {
data.iTotalRecords = data.page.totalElements;
data.iTotalDisplayRecords = data.page.totalElements;

activeScope().html(activeScopeName() + ' (' + data.iTotalRecords + ')');

fnCallback( data );
}
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void filter(String filterField, String filterValue) {
}

private WebElement getScope( String scopeLabel ) {
return webDriver().findElement( By.linkText( scopeLabel ) );
return webDriver().findElement( By.partialLinkText( scopeLabel ) );
}

public QuickViewComponent showQuickViewForItem( int itemId ) {
Expand Down

0 comments on commit bf5fe94

Please sign in to comment.