Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.azrul.langkuik; import com.vaadin.navigator.Navigator; import com.vaadin.navigator.View; import com.vaadin.navigator.ViewChangeListener; import com.vaadin.server.VaadinService; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Layout; import com.vaadin.ui.MenuBar; import com.vaadin.ui.Notification; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; import java.util.List; import java.util.UUID; import javax.persistence.EntityManagerFactory; import javax.persistence.metamodel.ManagedType; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.azrul.langkuik.annotations.WebEntity; import org.azrul.langkuik.configs.Configuration; import org.azrul.langkuik.dao.DataAccessObject; import org.azrul.langkuik.dao.HibernateGenericDAO; import org.azrul.langkuik.framework.PageParameter; import org.azrul.langkuik.framework.customtype.attachment.AttachmentCustomType; import org.azrul.langkuik.framework.relationship.RelationManagerFactory; import org.azrul.langkuik.framework.webgui.BeanView; import org.azrul.langkuik.framework.webgui.PlainTableView; import org.azrul.langkuik.framework.webgui.breadcrumb.BreadCrumbBuilder; import org.azrul.langkuik.framework.webgui.breadcrumb.History; import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; import org.vaadin.dialogs.ConfirmDialog; public class Langkuik { public void initLangkuik(final EntityManagerFactory emf, final UI ui, final RelationManagerFactory relationManagerFactory) { List<Class<?>> customTypeInterfaces = new ArrayList<>(); initLangkuik(emf, ui, relationManagerFactory, customTypeInterfaces); } public void massIndex(final EntityManagerFactory emf) { HibernateGenericDAO.massIndexDatabaseForSearch(emf); } public void initLangkuik(final EntityManagerFactory emf, final UI ui, final RelationManagerFactory relationManagerFactory, List<Class<?>> customTypeInterfaces) { List<Class<?>> rootClasses = new ArrayList<>(); for (ManagedType<?> entity : emf.getMetamodel().getManagedTypes()) { Class<?> clazz = entity.getJavaType(); if (clazz.getAnnotation(WebEntity.class).isRoot() == true) { rootClasses.add(clazz); } } //Manage custom type if (customTypeInterfaces == null) { customTypeInterfaces = new ArrayList<>(); } //add system level custom type customTypeInterfaces.add(AttachmentCustomType.class); //create DAOs for custom types final List<DataAccessObject<?>> customTypeDaos = new ArrayList<>(); for (Class<?> clazz : customTypeInterfaces) { customTypeDaos.add(new HibernateGenericDAO(emf, clazz)); } //Setup page VerticalLayout main = new VerticalLayout(); VerticalLayout content = new VerticalLayout(); final Navigator navigator = new Navigator(ui, content); final HorizontalLayout breadcrumb = new HorizontalLayout(); MenuBar menubar = new MenuBar(); menubar.setId("MENUBAR"); main.addComponent(menubar); main.addComponent(breadcrumb); main.addComponent(content); final Deque<History> history = new ArrayDeque<>(); final Configuration config = Configuration.getInstance(); history.push(new History("START", "Start")); StartView startView = new StartView(history, breadcrumb); navigator.addView("START", startView); MenuBar.MenuItem create = menubar.addItem("Create", null); MenuBar.MenuItem view = menubar.addItem("View", null); final PageParameter pageParameter = new PageParameter(customTypeDaos, emf, relationManagerFactory, history, config, breadcrumb); for (final Class rootClass : rootClasses) { final WebEntity myObject = (WebEntity) rootClass.getAnnotation(WebEntity.class); final DataAccessObject<?> dao = new HibernateGenericDAO<>(emf, rootClass); create.addItem("New " + myObject.name(), new MenuBar.Command() { @Override public void menuSelected(MenuBar.MenuItem selectedItem) { Object object = dao.createNew(true); BeanView<Object, ?> createNewView = new BeanView<>(object, null, null, pageParameter); String targetView = "CREATE_NEW_APPLICATION_" + UUID.randomUUID().toString(); navigator.addView(targetView, (View) createNewView); history.clear(); history.push(new History("START", "Start")); History his = new History(targetView, "Create new " + myObject.name()); history.push(his); navigator.navigateTo(targetView); } }); view.addItem("View " + myObject.name(), new MenuBar.Command() { @Override public void menuSelected(MenuBar.MenuItem selectedItem) { PlainTableView<?> seeApplicationView = new PlainTableView<>(rootClass, pageParameter); String targetView = "VIEW_APPLICATION_" + UUID.randomUUID().toString(); navigator.addView(targetView, (View) seeApplicationView); history.clear(); history.push(new History("START", "Start")); History his = new History(targetView, "View " + myObject.name()); history.push(his); navigator.navigateTo(targetView); } }); } menubar.addItem("Logout", null).addItem("Logout", new MenuBar.Command() { @Override public void menuSelected(MenuBar.MenuItem selectedItem) { ConfirmDialog.show(ui, "Please Confirm:", "Are you really sure you want to log out?", "I am", "Not quite", new ConfirmDialog.Listener() { @Override public void onClose(ConfirmDialog dialog) { if (dialog.isConfirmed()) { HttpServletRequest req = (HttpServletRequest) VaadinService.getCurrentRequest(); HttpServletResponse resp = (HttpServletResponse) VaadinService .getCurrentResponse(); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); SecurityContextLogoutHandler ctxLogOut = new SecurityContextLogoutHandler(); ctxLogOut.logout(req, resp, auth); } } }); } }); navigator.navigateTo("START"); ui.setContent(main); } class StartView extends VerticalLayout implements View { private final Deque<History> history; private final Layout breadcrumb; public StartView(Deque<History> history, Layout breadcrumb) { setSizeFull(); this.history = history; this.breadcrumb = breadcrumb; } @Override public void enter(ViewChangeListener.ViewChangeEvent vcevent) { //creat bread crumb BreadCrumbBuilder.buildBreadCrumb(vcevent.getNavigator(), breadcrumb, history); Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (!(auth instanceof AnonymousAuthenticationToken)) { UserDetails userDetails = (UserDetails) auth.getPrincipal(); Notification.show("Welcome " + userDetails.getUsername()); for (GrantedAuthority a : userDetails.getAuthorities()) { System.out.println(a.getAuthority()); } } } } }