tag:blogger.com,1999:blog-3305419975676030648.comments2023-09-15T14:45:14.583+02:00CTP Java CompetenceAnonymoushttp://www.blogger.com/profile/03406453074932551739noreply@blogger.comBlogger186125tag:blogger.com,1999:blog-3305419975676030648.post-12990608298988121362014-02-04T23:27:17.544+01:002014-02-04T23:27:17.544+01:00Hi there!
Nice blog you have with some interesting...Hi there!<br />Nice blog you have with some interesting articles. Keep up it up!<br /><br />Thanks.<br /><a href="http://devmain.blogspot.com" rel="nofollow">devmain blogspot</a>Unknownhttps://www.blogger.com/profile/00601577357384212529noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-7502726200678843732014-01-28T06:33:32.288+01:002014-01-28T06:33:32.288+01:00The structural analysis is a perfect one and nice ...The structural analysis is a perfect one and nice to see in such a good presentations. Thanks for sharing it.<br /><a href="http://jlcindia.com/Java-Training-Centers-In-Bangalore-Btm.html" rel="nofollow">java training centers in bangalore btm<br /></a>Anonymoushttps://www.blogger.com/profile/06365331212138248811noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-53549258361629404872014-01-28T06:33:21.421+01:002014-01-28T06:33:21.421+01:00The structural analysis is a perfect one and nice ...The structural analysis is a perfect one and nice to see in such a good presentations. Thanks for sharing it.<br /><a href="http://jlcindia.com/Java-Training-Centers-In-Bangalore-Btm.html" rel="nofollow">java training centers in bangalore btm<br /></a> Anonymoushttps://www.blogger.com/profile/06365331212138248811noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-53906373293342427242013-10-30T18:38:19.786+01:002013-10-30T18:38:19.786+01:00Hey, can we provide node level authentication inst...Hey, can we provide node level authentication instead of repository level authentication?<br /><br />Thanks in advance.Karthikaiselvanhttps://www.blogger.com/profile/11599356874838697383noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-66551603845908623322013-02-08T12:44:30.422+01:002013-02-08T12:44:30.422+01:00Is it usable with Guice? If it isn't are you p...Is it usable with Guice? If it isn't are you planning to provide some Guice integration?Anonymoushttps://www.blogger.com/profile/06697329660067355209noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-59651087914246715062012-05-28T15:13:56.028+02:002012-05-28T15:13:56.028+02:00Tks for the information, i always want to know wha...Tks for the information, i always want to know what OSGi is, in a fast way. And you did it. nice job!Tiago Augustohttps://www.blogger.com/profile/08293003525843325746noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-66616916732580548482012-04-05T13:10:34.414+02:002012-04-05T13:10:34.414+02:00Hey abdelgadir,
Appreciate your efforts! As you&#...Hey abdelgadir,<br /><br />Appreciate your efforts! As you've noticed, the blog comments are not very code friendly. Please place feature requests at the project website (https://github.com/ctpconsulting/query) where they are much better kept, structured and tracked than here.Anonymoushttps://www.blogger.com/profile/03406453074932551739noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-76026768979238710472012-04-05T12:34:52.628+02:002012-04-05T12:34:52.628+02:00I just noticed all generic bracket have been remov...I just noticed all generic bracket have been removed from my postings e.g., MAP<String,String&gt becomes MAP :(abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-80560470024251564772012-04-05T12:32:23.653+02:002012-04-05T12:32:23.653+02:00yet another suggestion.
Make your query methods a...yet another suggestion.<br /><br />Make your query methods accept Map representing the query parameter list. <br /><br />e.g., I have <br /><br />public SearchResultList findByNamedQuery(final Class type, final String namedQueryName,<br /> final Map parameters,<br /> final @Nullable YLimit limit, final @Nullable YFetch fetch) {<br /> //namedQueries will only take YLimit (not YPage) because the ordering is already part of the named query<br /> Set> rawParameters = parameters.entrySet();<br /> TypedQuery query = getEntityManager().createNamedQuery(namedQueryName, type);<br /> //process the named parameters<br /> for (Map.Entry entry : rawParameters) {<br /> query.setParameter(entry.getKey(), entry.getValue());<br /> }<br /> return executeQuery(query, limit, fetch);<br /> }<br /><br /><br />the client can then construct the parameters much more conveniently using the following fluent API<br /><br /><br /><br />public class QueryParameter {<br /> private Map parameters = null;<br /><br /> private QueryParameter(String name, Object value) {<br /> this.parameters = new HashMap();<br /> this.parameters.put(name, value);<br /> }<br /><br /> public static QueryParameter with(String name, Object value) {<br /> return new QueryParameter(name, value);<br /> }<br /><br /> public QueryParameter and(String name, Object value) {<br /> this.parameters.put(name, value);<br /> return this;<br /> }<br /><br /> public Map parameters() {<br /> return this.parameters;<br /> }<br />}abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-4643610062059830832012-04-05T12:26:27.195+02:002012-04-05T12:26:27.195+02:00forgot to include the Yfetch class. here it is:
...forgot to include the Yfetch class. here it is:<br /><br /><br />package hospital.model.dao;<br /><br />import com.google.common.collect.*;<br /><br />import java.io.Serializable;<br />import java.util.Collection;<br />import java.util.Map;<br /><br /><br />/**<br /> * This class allows you to dynamically add individual fields to the set of fields that will be <strong>eagerly</strong><br /> * loaded from the database.<br /> * <br /> * The methods that take only string arguments use the fully-qualified field name, such as org.mag.Magazine.publisher.<br /> * Similarly, getFields returns the set of fully-qualified field names. In all methods, the named field must be<br /> * defined in the class specified in the invocation, not a superclass. So, if the field publisher is defined in base<br /> * class Publication rather than sub-class Magazine, you must invoke addField (Publication.class, "publisher") and not<br /> * addField (Magazine.class, "publisher"). This helps reduce more extensive look-ups for predominant use cases.<br /> * <br /> * Also, in order to avoid the cost of reflection, the system will not perform any validation of the<br /> * field name/class name pairs that you put into the fetch configuration. If you specify non-existent class/field pairs,<br /> * nothing adverse will happen, but you will receive no notification of the fact that the specified configuration is<br /> * not being used.<br /> */<br />public class YFetch implements Serializable {<br /> /**<br /> * The maximum depth of relations to traverse when eager fetching. Use -1 for no limit which is the default<br /> * for openJpa.<br /> */<br /> private int maxFetchDepth = -1;<br /> private Multimap fetchProps = null;<br /><br /> private YFetch() {<br /> this(null, null);<br /> }<br /><br /> /**<br /> * @param clazz the class which <strong>directly</strong> defines the given fieldName<br /> * @param fieldName the fieldName to eagerly fetch<br /> */<br /> private YFetch(Class clazz, String fieldName) {<br /> this.fetchProps = HashMultimap.create();<br /> if (clazz != null && fieldName != null) {<br /> this.fetchProps.put(clazz, fieldName);<br /> }<br /> }<br /><br /> public static YFetch with(Class clazz, String fieldName) {<br /> return new YFetch(clazz, fieldName);<br /> }<br /><br /> public static YFetch with() {<br /> return new YFetch();<br /> }<br /><br /> public YFetch and(Class clazz, String fieldName) {<br /> this.fetchProps.put(clazz, fieldName);<br /> return this;<br /> }<br /><br /> public YFetch and(Class clazz, String... fieldNames) {<br /> this.fetchProps.putAll(clazz, Lists.newArrayList(fieldNames));<br /> return this;<br /> }<br /><br /> public YFetch and(Class clazz, Collection fieldNames) {<br /> this.fetchProps.putAll(clazz, fieldNames);<br /> return this;<br /> }<br /><br /> public YFetch setMaxFetchDepth(int maxFetchDepth) {<br /> this.maxFetchDepth = maxFetchDepth;<br /> return this;<br /> }<br /><br /> public int getMaxFetchDepth() {<br /> return maxFetchDepth;<br /> }<br /><br /> public Map> getFetchProperties() {<br /> return fetchProps.asMap();<br /> }<br /><br /> public boolean has(Class clazz, String fieldName) {<br /> return fetchProps.containsEntry(clazz, fieldName);<br /> }<br /><br />}abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-67880924985413729892012-04-05T12:15:06.239+02:002012-04-05T12:15:06.239+02:00one more comment, please note SearchResultList is...one more comment, please note SearchResultList is compatible with List. So any client that expects a List as a result will not be affected.<br /><br />Thanksabdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-78308281587604375022012-04-05T11:55:00.520+02:002012-04-05T11:55:00.520+02:00and this is the fetch strategy interface
public ...and this is the fetch strategy interface<br /><br /><br />public interface IPersistenceJPAFetchStrategy {<br /> public void applyFetch(YFetch fetch, Query query);<br />}abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-37068202235105952992012-04-05T11:54:01.459+02:002012-04-05T11:54:01.459+02:00correction, google code project which inspired Ypa...correction, google code project which inspired Ypage,YLimit, YOrder is <br />http://www.altuure.com/projects/yagdao <br /><br />The YFetch concept is my addition.abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-6129681520971672372012-04-05T11:46:35.448+02:002012-04-05T11:46:35.448+02:00public boolean hasPreviousPage(){
return g...public boolean hasPreviousPage(){<br /> return getPageIndex() > 0;<br /> }<br /><br /> public boolean hasNextPage(){<br /> return ((getPageIndex()+1) * getPaging().getPageSize()) < getTotalCount();<br /> }<br /><br /> @Override<br /> public String toString() {<br /> return "SearchResultList{" +<br /> "paging=" + paging +<br /> ", totalCount=" + totalCount +<br /> '}';<br /> }<br /><br /> @Override<br /> public boolean equals(Object o) {<br /> if (this == o) return true;<br /> if (o == null || getClass() != o.getClass()) return false;<br /> if (!super.equals(o)) return false;<br /><br /> SearchResultList that = (SearchResultList) o;<br /><br /> if (totalCount != that.totalCount) return false;<br /> if (paging != null ? !paging.equals(that.paging) : that.paging != null) return false;<br /><br /> return true;<br /> }<br /><br /> @Override<br /> public int hashCode() {<br /> int result = super.hashCode();<br /> result = 31 * result + (paging != null ? paging.hashCode() : 0);<br /> result = 31 * result + (int) (totalCount ^ (totalCount >>> 32));<br /> return result;<br /> }<br />}abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-71096376153539485472012-04-05T11:46:04.919+02:002012-04-05T11:46:04.919+02:00and last but not least
package hospital.model.d...and last but not least<br /><br /><br /><br />package hospital.model.dao;<br /><br />import com.google.common.base.Preconditions;<br />import com.google.common.collect.ForwardingList;<br /><br />import java.io.Serializable;<br />import java.util.Collections;<br />import java.util.List;<br /><br />/**<br /> * List encapsulating a result set with details of total result size and paging details.<br /> */<br />public class SearchResultList extends ForwardingList implements Serializable {<br /> /**<br /> * Paging details. A search result could be paged in the sense that it represents a single page of the total<br /> * retrievable results. In such case, the paging variable describes which page is being returned.<br /> * The paging index is zero-based.<br /> */<br /> private YPage paging;<br /> /**<br /> * total number of results that would have been returned if no maxResults had been specified.<br /> * In other words, it is the total retrievable result size; not just for a single page but for all possible pages.<br /> * This total count may or may not match the number of actual results being returned by this search result instance.<br /> */<br /> private long totalCount;<br /> /**<br /> * decorated list containing the results being returned. This list doesn't necessarily represent the total<br /> * retrievable results, for example, when the results are being returned in junks/pages<br /> */<br /> private List delegate;<br /><br /> public SearchResultList() {<br /> this(Collections.emptyList());<br /> }<br /><br /> public SearchResultList(List delegate) {<br /> this(delegate, null);<br /> }<br /><br /> /**<br /> * non-paged search result list i.e., the results being returned by this instance represent all retrievable results.<br /> * In this case the totalCount==result.size()<br /> */<br /> public SearchResultList(List result, YLimit limit) {<br /> this(result, result==null? 0 : result.size(), YPage.copy(limit));<br /> }<br /><br /> /**<br /> * paged search result list i.e., the set of results being returned by this instance represent the content of only<br /> * one page. Total count is the total retrievable across all pages.<br /> * In other words, 'the results in this instance (which are stored in delegate) represent only one page<br /> * (as defined by paging) of the total set of retrievable results which is equals to totalCount'.<br /> */<br /> public SearchResultList(List delegate, long totalCount, YPage paging) {<br /> super();<br /> Preconditions.checkNotNull(delegate, "delegate must not be null");<br /> this.delegate = delegate;<br /> this.totalCount = totalCount;<br /> this.paging = paging;<br /> }<br /><br /> @Override<br /> protected List delegate() {<br /> return delegate;<br /> }<br /><br /> public YPage getPaging() {<br /> return paging;<br /> }<br /><br /> public long getTotalCount() {<br /> return totalCount;<br /> }<br /><br /> /**<br /> * get the index of this result's page.<br /> *<br /> * @return index of the page defined by this result<br /> */<br /> public int getPageIndex() {<br /> return (int) paging.getFirstResultIndex() / paging.getPageSize();<br /> }<br /><br /> /**<br /> * @return page count<br /> */<br /> public int getPageCount() {<br /> //is i the lastPageIndex ??!!<br /> int i = (int) totalCount / paging.getPageSize();<br /> if (totalCount % paging.getPageSize() != 0) {<br /> i = i + 1;<br /> }<br /> return i;<br /> }abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-35171111139425086512012-04-05T11:44:57.549+02:002012-04-05T11:44:57.549+02:00import com.google.common.base.Preconditions;
impor...import com.google.common.base.Preconditions;<br />import com.google.common.base.Strings;<br /><br />import java.io.Serializable;<br /><br />/**<br /> * Class used to specify a sorted property<br /> *<br /> * @author altuure<br /> * @author abdelgadir ibrahim<br /> */<br />public class YOrder implements Serializable {<br /> private static final long serialVersionUID = 1L;<br /><br /> /**<br /> * Available sort orders<br /> */<br /> public enum Sort {<br /> ASC, DESC<br /> }<br /><br /> /**<br /> * sort field name<br /> */<br /> private String name;<br /><br /> /**<br /> * sort order<br /> */<br /> private Sort sort;<br /><br /> /**<br /> * construct a YOrder to sort given property in asc order. The property name is a path expression starting from<br /> * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',<br /> * where Employee is the returned root class, it should be specified as 'user.firstName'.<br /> *<br /> * @param name sort property name. Assumes asc sort order<br /> */<br /> public YOrder(String name) {<br /> this(name, Sort.ASC);<br /> }<br /><br /> /**<br /> * construct a YOrder to sort given property by the given sort order. The property name is a path expression<br /> * starting from the returned root entity excluding the root itself. For example, assuming property<br /> * 'Employee.user.firstName', where Employee is the returned root class, it should be specified as 'user.firstName'.<br /> *<br /> * @param name sort property<br /> * @param sort sort order<br /> */<br /> public YOrder(String name, Sort sort) {<br /> super();<br /> Preconditions.checkNotNull(Strings.emptyToNull(name), "sort property must not be null or empty");<br /> this.name = name;<br /> this.sort = sort;<br /> }<br /><br /> /**<br /> * get the sort property name<br /> * @return sort property name<br /> */<br /> public String getName() {<br /> return name;<br /> }<br /><br /> /**<br /> * get sort order<br /> *<br /> * @return sort order<br /> */<br /> public Sort getSort() {<br /> return sort;<br /> }<br /><br /> @Override<br /> public String toString() {<br /> return "YOrder{" +<br /> "name='" + name + '\'' +<br /> ", sort=" + sort +<br /> '}';<br /> }<br /><br /> @Override<br /> public int hashCode() {<br /> final int prime = 31;<br /> int result = 1;<br /> result = prime * result + ((name == null) ? 0 : name.hashCode());<br /> result = prime * result + ((sort == null) ? 0 : sort.hashCode());<br /> return result;<br /> }<br /><br /> @Override<br /> public boolean equals(Object obj) {<br /> if (this == obj)<br /> return true;<br /> if (obj == null)<br /> return false;<br /> if (getClass() != obj.getClass())<br /> return false;<br /> YOrder other = (YOrder) obj;<br /> if (name == null) {<br /> if (other.name != null)<br /> return false;<br /> } else if (!name.equals(other.name))<br /> return false;<br /> if (sort != other.sort)<br /> return false;<br /> return true;<br /> }<br />}abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-42450202598266357932012-04-05T11:43:03.826+02:002012-04-05T11:43:03.826+02:00/**
* util method to add order.The orderBy pr.../**<br /> * util method to add order.The orderBy property name is a path expression starting from<br /> * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',<br /> * where Employee is the returned root class, it should be specified as 'user.firstName'.<br /> *<br /> * @param orderBy order property.<br /> * @param ascending sort order.<br /> * @return this<br /> */<br /> public YPage addOrder(String orderBy, boolean ascending) {<br /> orders.add(new YOrder(orderBy, (ascending) ? YOrder.Sort.ASC : YOrder.Sort.DESC));<br /> return this;<br /> }<br /><br /> /**<br /> * builder method to sort result set. The orderBy property name is a path expression starting from<br /> * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',<br /> * where Employee is the returned root class, it should be specified as 'user.firstName'.<br /> *<br /> * @param orderBy propery to sort<br /> * @param sort sort order<br /> * @return this<br /> */<br /> public YPage addOrder(String orderBy, YOrder.Sort sort) {<br /> orders.add(new YOrder(orderBy, sort));<br /> return this;<br /> }<br /><br /> /**<br /> * list of orders.<br /> *<br /> * @return orders<br /> */<br /> public List getOrders() {<br /> return orders;<br /> }<br /><br /> /**<br /> * orders to set.<br /> *<br /> * @param orders orders<br /> */<br /> public void setOrders(List orders) {<br /> this.orders = orders;<br /> }<br /><br /> @Override<br /> public int hashCode() {<br /> int result = super.hashCode();<br /> final int prime = 31;<br /> result = prime * result + ((orders == null) ? 0 : orders.hashCode());<br /><br /> return result;<br /> }<br /><br /> @Override<br /> public boolean equals(Object o) {<br /> if (this == o) return true;<br /> if (!(o instanceof YPage)) return false;<br /> if (!super.equals(o)) return false;<br /><br /> YPage yPage = (YPage) o;<br /><br /> if (orders != null ? !orders.equals(yPage.orders) : yPage.orders != null) return false;<br /><br /> return true;<br /> }<br /><br /> @Override<br /> public String toString() {<br /> StringBuilder builder = new StringBuilder();<br /> builder.append("YPage [orders=");<br /> builder.append(orders);<br /> builder.append(", YLimit=");<br /> builder.append(super.toString());<br /> builder.append("]");<br /> return builder.toString();<br /> }<br />}abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-45221840830910940742012-04-05T11:42:50.530+02:002012-04-05T11:42:50.530+02:00package hospital.model.dao;
import java.io.Serial...package hospital.model.dao;<br /><br />import java.io.Serializable;<br />import java.util.ArrayList;<br />import java.util.List;<br /><br />public class YPage extends YLimit implements Serializable {<br /> /**<br /> * serialization key.<br /> */<br /> private static final long serialVersionUID = 1L;<br /> /**<br /> * order sort list.<br /> */<br /> private List orders = new ArrayList(2);<br /><br /> /**<br /> * dao page serialization constructor.<br /> */<br /> public YPage() {<br /> super();<br /> }<br /><br /> /**<br /> * dao page serialization constructor.<br /> *<br /> * @param limit limits<br /> */<br /> public static YPage copy(YLimit limit) {<br /> if (limit == null) {<br /> return new YPage();<br /> } else {<br /> return new YPage(limit.firstResultIndex, limit.pageSize);<br /> }<br /> }<br /><br /> /**<br /> * order by given property asc. The property name is a path expression starting from<br /> * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',<br /> * where Employee is the returned root class, it should be specified as 'user.firstName'.<br /> *<br /> * @param orderBy property asc.<br /> */<br /> public YPage(String orderBy) {<br /> this(orderBy, true, NOT_ASSIGNED, NOT_ASSIGNED);<br /> }<br /><br /> /**<br /> * order by given property. The orderBy property name is a path expression starting from<br /> * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',<br /> * where Employee is the returned root class, it should be specified as 'user.firstName'.<br /> *<br /> * @param orderBy property .<br /> * @param ascending is asc<br /> */<br /> public YPage(String orderBy, boolean ascending) {<br /> this(orderBy, ascending, NOT_ASSIGNED, NOT_ASSIGNED);<br /> }<br /><br /> /**<br /> * dao page. The orderBy property name is a path expression starting from<br /> * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',<br /> * where Employee is the returned root class, it should be specified as 'user.firstName'.<br /> *<br /> * @param orderBy order property<br /> * @param ascending asc<br /> * @param firstResultIndex first result index<br /> * @param pageSize result size<br /> */<br /> public YPage(String orderBy, boolean ascending, int firstResultIndex, int pageSize) {<br /> super(firstResultIndex, pageSize);<br /> if (orderBy != null) {<br /> orders.add(new YOrder(orderBy, (ascending) ? YOrder.Sort.ASC : YOrder.Sort.DESC));<br /> }<br /> }<br /><br /> /**<br /> * limit result with given properties.<br /> *<br /> * @param firstResultIndex first result index<br /> * @param pageSize page size<br /> */<br /> public YPage(int firstResultIndex, int pageSize) {<br /> this(null, false, firstResultIndex, pageSize);<br /> }<br /><br /> /**<br /> * unlimited result size from given index.<br /> *<br /> * @param firstResultIndex first result index<br /> */<br /> public YPage(int firstResultIndex) {<br /> this(null, false, firstResultIndex, NOT_ASSIGNED);<br /> }<br /><br /> /**<br /> * is ordered.<br /> *<br /> * @return is ordered<br /> */<br /> public boolean isOrdered() {<br /> return orders != null && !orders.isEmpty();<br /> }abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-1240983937623904522012-04-05T11:41:04.592+02:002012-04-05T11:41:04.592+02:00the YLimit, YPage, and YOrder are inspired and bas...the YLimit, YPage, and YOrder are inspired and based on YDAO opensource project see google code). I have extracted and adapted some of their work (and fixed a couple bugs) for my purposes so credit to them.<br /><br />/**<br /> * Created by IntelliJ IDEA.<br /> * User: age<br /> * Date: 18/11/11<br /> * Time: 23:51<br /> * To change this template use File | Settings | File Templates.<br /> *<br /> * Copyright (C) 2010 altuure http://www.altuure.com/projects/yagdao<br /> *<br /> * Licensed under the Apache License, Version 2.0 (the "License");<br /> * you may not use this file except in compliance with the License.<br /> * You may obtain a copy of the License at<br /> *<br /> * http://www.apache.org/licenses/LICENSE-2.0<br /> *<br /> * Unless required by applicable law or agreed to in writing, software<br /> * distributed under the License is distributed on an "AS IS" BASIS,<br /> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br /> * See the License for the specific language governing permissions and<br /> * limitations under the License.<br /> */<br /><br />package hospital.model.dao;<br /><br />import java.io.Serializable;<br /><br /><br />/**<br /> * DAO meta class for paging and sorting.<br /> *<br /> * @author altuure<br /> */<br />public class YLimit implements Serializable {<br /> private static final long serialVersionUID = 1L;<br /> /**<br /> * not assigned value.<br /> */<br /> protected static final int NOT_ASSIGNED = -1;<br /> /**<br /> * index of first result.<br /> */<br /> protected int firstResultIndex = NOT_ASSIGNED;<br /> /**<br /> * result set page size. The page size specifies the maximum number of records to retrieve<br /> */<br /> protected int pageSize = NOT_ASSIGNED;<br /><br /> /**<br /> * limit result with given properties.<br /> *<br /> * @param firstResultIndex first result index<br /> * @param pageSize page size<br /> */<br /> public YLimit(int firstResultIndex, int pageSize) {<br /> this.firstResultIndex = firstResultIndex;<br /> this.pageSize = pageSize;<br /> }<br /><br /> /**<br /> * unlimited result size from given index.<br /> *<br /> * @param firstResultIndex first result index<br /> */<br /> public YLimit(int firstResultIndex) {<br /> this(firstResultIndex, NOT_ASSIGNED);<br /> }<br /><br /> /**<br /> * default cons.<br /> */<br /> public YLimit() {<br /> this(NOT_ASSIGNED, NOT_ASSIGNED);<br /> }<br /><br /> /**<br /> * first result index.<br /> *<br /> * @return first result<br /> */<br /> public int getFirstResultIndex() {<br /> return firstResultIndex;<br /> }<br /><br /> /**<br /> * @return page size.<br /> */<br /> public int getPageSize() {<br /> return pageSize;<br /> }<br /><br /> /**<br /> * get whether this limit object represents a page as opposed to the total results.<br /> *<br /> * @return whether firstResultIndex>0<br /> */<br /> public boolean isPaged() {<br /> return firstResultIndex >= 0;<br /> }<br /><br /> /**<br /> * get whether the page size is defined.<br /> *<br /> * @return pageSize>0<br /> */<br /> public boolean isPageSized() {<br /> return pageSize >= 0;<br /> }<br /><br /> @Override<br /> public boolean equals(Object o) {<br /> if (this == o) return true;<br /> if (o == null || getClass() != o.getClass()) return false;<br /><br /> YLimit yLimit = (YLimit) o;<br /><br /> if (firstResultIndex != yLimit.firstResultIndex) return false;<br /> if (pageSize != yLimit.pageSize) return false;<br /><br /> return true;<br /> }<br /><br /> @Override<br /> public int hashCode() {<br /> int result = firstResultIndex;<br /> result = 31 * result + pageSize;<br /> return result;<br /> }<br /><br /> /*<br /> * (non-Javadoc)<br /> *<br /> * @see java.lang.Object#toString()<br /> */<br /> @Override<br /> public String toString() {<br /> return "YLimit [firstResultIndex=" + firstResultIndex + ", pageSize=" + pageSize + "]";<br /> }<br />}abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-87441149618086270642012-04-05T11:36:48.590+02:002012-04-05T11:36:48.590+02:00private Path extractPath(Root from, Map joins, Str...private Path extractPath(Root from, Map joins, String property) {<br /> Path path;<br /> String[] strings = DaoUtils.splitDot(property);<br /><br /> From join = getLatestJoin(from, joins, strings);<br /> path = join.get(strings[strings.length - 1]);<br /><br /> return path;<br /> }<br /><br /> /**<br /> * create latest join from given root criteria from<br /> *<br /> * @param from root<br /> * @param joins join map cache<br /> * @param strings '.' splitted field array<br /> * @return get latest join before property<br /> */<br /> private From getLatestJoin(Root from, Map joins, String[] strings) {<br /> // /not implemented yet<br /> From join = from;<br /> for (int j = 0; j < strings.length - 1; j++) {<br /> String propertyName = strings[j];<br /> String key = keyMaker(strings, j);<br /><br /> if (joins.containsKey(key)) {<br /> join = joins.get(key);<br /> } else {<br /> if (j == 0) {<br /> join = from.join(propertyName);<br /> } else {<br /> join = join.join(propertyName);<br /> }<br /> joins.put(key, join);<br /> }<br /> }<br /> return join;<br /> }<br /><br /> /**<br /> * given a property path, construct a (sub)path ending at index j. Uses '.' as the path separator<br /> *<br /> * @param strings property path<br /> * @param j index of last property<br /> * @return constructed (sub)path<br /> */<br /> private String keyMaker(String[] strings, int j) {<br /> StringBuilder stringBuilder = new StringBuilder();<br /> for (int i = 0; i <= j; i++) {<br /> String string = strings[i];<br /> if (i > 0) {<br /> stringBuilder.append(".");<br /> }<br /> stringBuilder.append(string);<br /> }<br /> return stringBuilder.toString();<br /> }<br /><br /> /**<br /> * set limits for query<br /> *<br /> * @param query query<br /> * @param limit page limits<br /> */<br /> private void setLimits(Query query, YLimit limit) {<br /> if (limit == null) {<br /> return;<br /> }<br /> if (limit.isPaged()) {<br /> query.setFirstResult(limit.getFirstResultIndex());<br /> }<br /> if (limit.isPageSized()) {<br /> query.setMaxResults(limit.getPageSize());<br /> }<br /> }<br /><br /> private void applyFetch(YFetch fetch, Query query){<br /> persistenceJPAFetchStrategy.applyFetch(fetch, query);<br /> }<br /><br /><br />//we would have one for each provider e.g., hibernate etc<br />@Alternative<br />public class OpenJpaFetchStrategy implements IPersistenceJPAFetchStrategy {<br /><br /> public void applyFetch(YFetch fetch, Query query) {<br /> if (fetch != null) {<br /> OpenJPAQuery openJPAQuery = OpenJPAPersistence.cast(query);<br /> FetchPlan fetchPlan = openJPAQuery.getFetchPlan();<br /> fetchPlan.setMaxFetchDepth(fetch.getMaxFetchDepth());<br /> Map> fetchProps = fetch.getFetchProperties();<br /> for (Class clazz : fetchProps.keySet()) {<br /> Collection fieldNames = fetchProps.get(clazz);<br /> fetchPlan.addFields(clazz, fieldNames);<br /> }<br /> }<br /> }<br />}abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-17547864744857554752012-04-05T11:36:29.211+02:002012-04-05T11:36:29.211+02:00Continue,
The second more important set of sugges...Continue,<br /><br />The second more important set of suggestions is related to support for paging and for fetching certain fields as part of query (cleaner alternative to all the open-session-in-view stuff which is a solution to the dreaded lazyinitializationexception). currently, I have something like:<br /><br />public SearchResultList findAll(final Class type, final @Nullable YPage paging,<br /> final @Nullable YFetch fetch) {<br /> CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();<br /> CriteriaQuery cq = builder.createQuery(type);<br /> Root root = cq.from(type);<br /> cq.select(root);<br /> return executeQuery(cq, paging, fetch, builder, root);<br /> }<br /><br />/**<br /> * execute query with given properties<br /> *<br /> * @param select root query<br /> * @param paging paging<br /> * @param criteriaBuilder root builder<br /> * @param from parent from<br /> * @return executed list<br /> */<br /> private SearchResultList executeQuery(CriteriaQuery select, YPage paging, YFetch fetch,<br /> CriteriaBuilder criteriaBuilder, Root from) {<br /> if (paging == null) {<br /> TypedQuery typedQuery = getEntityManager().createQuery(select);<br /> applyFetch(fetch, typedQuery);<br /> return new SearchResultList(typedQuery.getResultList());<br /> }<br /><br /> //do the total count before setting the limits so as to work out the total retrievable.<br /> //note that setting the limit may affect the restrictions e.g., by appending the equivalent of<br /> //'where from.id >= paging.getFirstResultIndex'<br /> long totalCount = count(from, select.getRestriction(), criteriaBuilder);<br /><br /> setOrders(select, paging, criteriaBuilder, from, new HashMap());<br /> TypedQuery typedQuery = getEntityManager().createQuery(select);<br /> setLimits(typedQuery, paging);<br /> applyFetch(fetch, typedQuery);<br /> return new SearchResultList(typedQuery.getResultList(), totalCount, paging);<br /> }<br /><br /> /**<br /> * set Orders of given criteria.<br /> *<br /> * @param select query select<br /> * @param paging query page definition<br /> * @param criteriaBuilder criteria builder<br /> * @param from root from<br /> * @param joins joins map<br /> */<br /> private void setOrders(CriteriaQuery select, YPage paging, CriteriaBuilder criteriaBuilder, Root from,<br /> Map joins) {<br /> if (paging == null) {<br /> return;<br /> }<br /> if (paging.isOrdered()) {<br /> ArrayList jpaOrders = new ArrayList();<br /> List orders = paging.getOrders();<br /> //to set the jpa orders, we first need to join on the objects whose properties we are using to order by<br /> //e.g., if we have 'order by employers.name', we will first need to join to employers if not already joined<br /> for (YOrder daoOrder : orders) {<br /> Path path = extractPath(from, joins, daoOrder.getName());<br /> Order jpaOrder = (daoOrder.getSort().equals(YOrder.Sort.ASC)) ? criteriaBuilder.asc(path)<br /> : criteriaBuilder.desc(path);<br /> jpaOrders.add(jpaOrder);<br /> }<br /> select.orderBy(jpaOrders.toArray(new Order[0]));<br /> }<br /> }abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-1062622613888603842012-04-05T11:23:49.154+02:002012-04-05T11:23:49.154+02:00great work - many thanks.
I have a few suggestion...great work - many thanks. <br />I have a few suggestion for improvement based on my current requirements.<br />The first set of suggestions is to add a few more methods to the EntityDao interface. below is how they are currently implemented in my project:<br /><br />public T createAndFlushAndRefresh(final T entity) {<br /> if (entity != null) {<br /> getEntityManager().persist(entity);<br /> getEntityManager().flush();<br /> getEntityManager().refresh(entity);<br /> }<br /> return entity;<br /> }<br /><br /><br />public void deleteByPrimaryKey(final Class type, final Serializable pk) {<br /> if (pk != null) {<br /> Object entity = getEntityManager().find(type, pk);<br /> if (entity != null) {<br /> delete(entity);<br /> }<br /> }<br /> }<br /><br /><br /><br /> public void delete(final Object entity) {<br /> if (entity != null) {<br /> EntityManager em = getEntityManager();<br /> em.remove(em.contains(entity) ? entity : em.merge(entity));<br /> }<br /> }<br /><br />to rename the findBy(PK) method to findByPK(PK)<br /><br /> public boolean exists(final Class type, final Serializable primaryKey) {<br /> //return false for a null primary key<br /> return primaryKey == null ? false : (findByPrimaryKey(type, primaryKey) != null);<br /> }<br /><br /><br />/**<br /> * This is a special version of count() that finds how many instances of 'root' will be returned given the<br /> * specified restriction.<br /> */<br /> private Long count(final Root root, final Predicate restriction, final CriteriaBuilder builder) {<br /> //TODO exclude inactive records from the count<br /> CriteriaQuery countQuery = builder.createQuery(Number.class);<br /> countQuery.select(builder.count(root));<br /> countQuery.where(restriction);<br /> return getEntityManager().createQuery(countQuery).getSingleResult().longValue();<br /> }abdelgadirhttps://www.blogger.com/profile/13414045551847514436noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-20445858884814139792012-03-30T22:18:28.039+02:002012-03-30T22:18:28.039+02:00Hi Derrick,
Looks like there is a problem with Dr...Hi Derrick,<br /><br />Looks like there is a problem with Drools. If you don't need the rule based security, I'd remove it. AFAIR the reference to Drools can be removed by just deleting the appropriate tag in components.xml.<br /><br />BTW to your question in the first comment, the archetype uses RichFaces by default if you skip the icefaces parameter.Anonymoushttps://www.blogger.com/profile/03406453074932551739noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-12709104366060847632012-03-30T21:25:11.867+02:002012-03-30T21:25:11.867+02:00Could you oupdate your example to newer Weld versi...Could you oupdate your example to newer Weld version? The API has significanlty changed.Anonymoushttps://www.blogger.com/profile/15090996564146119358noreply@blogger.comtag:blogger.com,1999:blog-3305419975676030648.post-9675932735523594532012-03-30T21:12:25.054+02:002012-03-30T21:12:25.054+02:00Hey Thomas,
Thanks for the quick reply. The reaso...Hey Thomas,<br />Thanks for the quick reply. The reason I had those problems was because the library was deprecated and needed to be switched. However, I am running into 1 other issue. When the project is deploying an error is returned stating the following: <br /><br />Running TestSuite<br />java.lang.RuntimeException: Could not create Component: org.jboss.seam.security.ruleBasedPermissionResolver<br />at org.jboss.seam.init.Initialization.addComponent(Initialization.java:1202)<br />at org.jboss.seam.init.Initialization.installComponents(Initialization.java:1118)<br />at org.jboss.seam.init.Initialization.init(Initialization.java:733)<br />at org.jboss.seam.mock.AbstractSeamTest.startSeam(AbstractSeamTest.java:919)<br />at org.jboss.seam.mock.SeamTest.startSeam(SeamTest.java:58)<br />at mdh.AuthenticatorTest.startSeam(AuthenticatorTest.java:17)<br />Caused by: java.lang.IllegalArgumentException: No converter for type: org.drools.RuleBase<br />at org.jboss.seam.util.Conversions.getConverter(Conversions.java:63)<br />at org.jboss.seam.Component$ConstantInitialValue.<.init.>(Component.java:2481)<br />at org.jboss.seam.Component.getInitialValue(Component.java:538)<br />at org.jboss.seam.Component.getInitialValueHonoringExceptions(Component.java:512)<br />at org.jboss.seam.Component.initInitializers(Component.java:489)<br />at org.jboss.seam.Component.<.init.>(Component.java:254)<br />at org.jboss.seam.Component.<.init.>(Component.java:205)<br />at org.jboss.seam.init.Initialization.addComponent(Initialization.java:1186)<br />... 27 more<br />Tests run: 7, Failures: 1, Errors: 0, Skipped: 6, Time elapsed: 9.969 sec <<< FAILURE!<br /><br />Results :<br /><br />Failed tests:<br />startSeam(mdh.AuthenticatorTest)<br /><br />I saw a similar comment about this issue in the original post; however, they didn't provide instructions on a fix. Thoughts?<br /><br />Also, please understand the reason I am still using this is because my project is strictly tied to Seam 2.2.1, Maven 3 and Jboss-as 5.1. So far, this has been the closest I have got to getting all three working together. Please email me a response at dsutherland1492@gmail.com if you could. Thanks for your help.Derrickhttps://www.blogger.com/profile/09314845278961931731noreply@blogger.com