Skip to content

Instantly share code, notes, and snippets.

@dmitrygusev
Created April 7, 2013 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitrygusev/5331606 to your computer and use it in GitHub Desktop.
Save dmitrygusev/5331606 to your computer and use it in GitHub Desktop.
import x.y.z.entities.Company;
import org.apache.tapestry5.Block;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
public class CompanyBlocks
{
@Property private Company company;
@Inject private Block addressBlock;
public Block onRenderFromCompanyAddress(Company company)
{
// Setup properties used by this block
this.company = company;
// // You may also wrap your block to a zone and pass it to the render queue.
// // In this case you don't need to return anything from this method.
// ajaxResponseRenderer.addRender(addressBlockZone);
return addressBlock;
}
}
<div
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">
<t:block id="addressBlock">
<t:company.address t:id="companyAddress" company="company" />
</t:block>
</div>
import java.util.ArrayList;
import java.util.List;
import x.y.z.entities.Company;
import x.y.z.rest.CompanyResource;
import x.y.z.services.search.SearchService;
import x.y.z.services.tapestry5.EventResponseRenderer;
import x.y.z.services.tapestry5.RenderEvent;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
public class CompanyResourceImpl implements CompanyResource
{
private static final Logger logger = LoggerFactory.getLogger(CompanyResourceImpl.class);
@Inject private SearchService searchService;
@Inject private EventResponseRenderer renderer;
@Override
public Matches search(String term, int page, int pageLimit)
{
logger.info("Searching for term='{}', page={}, pageLimit={}",
new Object[] { term, page, pageLimit });
Matches result = new Matches();
// ...
int offset = page > 1 ? (page - 1) * pageLimit : 0;
List<Company> matches = searchService.findMatches(term, offset, pageLimit + 1);
result.hasMore = matches.size() > pageLimit;
for (int i = 0; i < pageLimit && i < matches.size(); i++)
{
Company company = matches.get(i);
result.add(createMatch(company));
}
return result;
}
protected Match createMatch(final Company company)
{
String rawAddress = renderer.render(new RenderEvent(
"internal/companyblocks", "companyAddress", company));
String htmlAddress = new JSONObject(rawAddress).getString("content");
return new Match(
company.getId(),
company.getName(),
htmlAddress);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment