Created
July 12, 2012 14:26
-
-
Save wbotelhos/3098439 to your computer and use it in GitHub Desktop.
VRaptor and jQuery Gridy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class EntityWrapper<T> { | |
| private Collection<T> list; | |
| private Integer total; | |
| public Collection<T> getList() { | |
| return list; | |
| } | |
| public void setList(Collection<T> list) { | |
| this.list = list; | |
| } | |
| public Integer getTotal() { | |
| return total; | |
| } | |
| public void setTotal(Integer total) { | |
| this.total = total; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class GenericBusiness<T> implements GenericRepository<T> { | |
| protected final EntityManager manager; | |
| private final Class<T> clazz; | |
| protected GenericBusiness(EntityManager manager) { | |
| this.manager = manager; | |
| @SuppressWarnings("unchecked") | |
| Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; | |
| this.clazz = clazz; | |
| } | |
| public Collection<T> all() { | |
| Query query = manager.createQuery("from " + clazz.getName()); | |
| @SuppressWarnings("unchecked") | |
| Collection<T> list = query.getResultList(); | |
| return list; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="grid"></div> | |
| <script type="text/javascript"> | |
| $(function() { | |
| $('#grid').gridy({ | |
| find : 'name', | |
| finds : [{ name: 'Nome', value: 'name' }, { name: 'Endereço', value: 'address' }], | |
| columns : [{ width: 400 }], // for equals headers and columns, use just columns property. | |
| headers : [ | |
| { name: 'Nome', value: 'name', width: 200 }, | |
| { name: 'Endereço', value: 'address', width: 200 } | |
| ], | |
| sortName : 'name', | |
| style : 'free', | |
| url : '${userSession.logged}/model/gridy', | |
| width : 400 | |
| }); | |
| }); | |
| </script> | |
| <script id="template" type="text/html"> // text/html for google index | |
| <div> // line repeat | |
| <div>\${id}</div> // column 1 | |
| <div>{{html metodoJS(name)}}</div> // column 2 | |
| </div> | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component | |
| public class ModelBusiness extends GenericBusiness<Model> implements ModelRepository { | |
| public ModelBusiness(EntityManager manager) { | |
| super(manager); | |
| } | |
| public int gridy(String search, String find) { | |
| String sql = "select count(id) from " + Model.class.getName(); | |
| if (has(find)) { | |
| sql += " where " + find + " like :search"; | |
| } | |
| Query query = manager.createQuery(sql) | |
| if (has(find)) { | |
| query.setParameter("search", "%" + search + "%"); | |
| } | |
| return ((Long) query.getSingleResult()).intValue(); | |
| } | |
| public Collection<Model> gridy(String search, int page, String sortName, String sortOrder, String find, int rows) { | |
| String sql = "from " + Model.class.getName(); | |
| if (has(find)) { | |
| sql += " where " + find + " like :search"; | |
| } | |
| String order = ""; | |
| if (has(sortName)) { | |
| order = " order by " + sortName; | |
| } | |
| if (has(sortName) && sortOrder != null) { | |
| order += (sortOrder.isEmpty() || sortOrder.equals("asc")) ? " asc" : " desc"; | |
| } | |
| int inicio = ((page > 0) ? (page - 1) : 0) * rows; | |
| Query query = manager.createQuery(sql + order); | |
| query.setFirstResult(inicio); | |
| query.setMaxResults(rows); | |
| if (has(find)) { | |
| query.setParameter("search", "%" + search + "%"); | |
| } | |
| @SuppressWarnings("unchecked") | |
| Collection<Model> list = query.getResultList(); | |
| return list; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Resource | |
| public class ModelController { | |
| private final Result result; | |
| private final ModelRepository repository; | |
| public ModelController(Result result, ModelRepository repository) { | |
| this.result = result; | |
| this.repository = repository; | |
| } | |
| @Get("/model") | |
| public void list() { | |
| } | |
| @Get("/model/gridy") | |
| public void gridy(String search, int page, String sortName, String sortOrder, String find, int rows) { | |
| search = Utils.decoderText(search); | |
| search = Utils.scapeInjection(search); | |
| EntityWrapper<Model> wrapper = new EntityWrapper<Model>(); | |
| wrapper.setList(repository.gridy(search, page, sortName, sortOrder, find, rows)); | |
| wrapper.setTotal(repository.gridy(search, find)); | |
| result.use(json()).withoutRoot().from(wrapper).serialize(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Utils { | |
| public static boolean has(String text) { | |
| return text != null && !text.isEmpty(); | |
| } | |
| public static String scapeInjection(String text) { | |
| if (text == null) { | |
| return text; | |
| } | |
| return text | |
| .replaceAll("insert", "") | |
| .replaceAll("select", "") | |
| .replaceAll("update", "") | |
| .replaceAll("drop", "") | |
| .replaceAll("delete", "") | |
| .replaceAll("--", "") | |
| .replaceAll("\n", "") | |
| .replaceAll("\\\\", "") | |
| .replaceAll("\\*", "") | |
| .replaceAll("/", "") | |
| .replaceAll("@", "") | |
| .replaceAll("#", ""); | |
| } | |
| public static String decoderText(String text) { | |
| if (text == null) { | |
| return text; | |
| } | |
| try { | |
| byte[] bytes = text.getBytes("ISO-8859-1"); | |
| text = new String(bytes, "UTF-8"); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| return text; | |
| } | |
| return text; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment