public static ClientProfile getClientProfileByDisplay(String display) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = null; Transaction tx = null; try { session = sf.openSession(); tx = session.beginTransaction(); ClientProfile profiles = (ClientProfile) session .createQuery( "SELECT distinct client " + "FROM ClientProfile client " + "left join fetch client.layout layout " + "left join fetch client.profile profile " + "left join fetch profile.layoutProfiles laypro " + "left join fetch layout.panels panel " + "left join fetch panel.texts " + "left join fetch panel.videos " + "left join fetch panel.images " + "left join fetch panel.webs " + "left join fetch panel.slideShows " + "left join fetch panel.feeds " + "left join fetch laypro.layout lay " + "left join fetch lay.panels pan " + "left join fetch pan.texts " + "left join fetch pan.videos " + "left join fetch pan.images " + "left join fetch pan.webs " + "left join fetch pan.slideShows " + "left join fetch pan.feeds " + "WHERE client.clientName = :pname") .setParameter("pname", display) .uniqueResult(); tx.commit(); if (profiles != null && profiles.getProfile() != null) { Map<Long, LayoutProfile> map = new LinkedHashMap<Long, LayoutProfile>(); for (LayoutProfile layoutProfile : profiles.getProfile().getLayoutProfiles()) { map.put(layoutProfile.getId(), layoutProfile); } profiles.getProfile().getLayoutProfiles().clear(); profiles.getProfile().getLayoutProfiles().addAll(map.values()); } return profiles; } catch (Exception e) { e.printStackTrace(); if (tx != null) { tx.rollback(); } return null; } finally { session.clear(); session.close(); } }
public static void saveProfile(Profile profile, List<LayoutProfile> layoutProfiles) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = null; Transaction tx = null; try { session = sf.openSession(); tx = session.beginTransaction(); if (profile.getId() != null) { if (profile.getLayoutProfiles().size() != 0) { for (LayoutProfile layoutProfile : profile.getLayoutProfiles()) { session.delete(layoutProfile); } } } session.saveOrUpdate(profile); for (LayoutProfile layoutProfile : layoutProfiles) { layoutProfile.setId(null); layoutProfile.setProfile(profile); session.saveOrUpdate(layoutProfile); } tx.commit(); } catch (Exception e) { e.printStackTrace(); if (tx != null) { tx.rollback(); } } finally { session.clear(); session.close(); } }
public static void duplicateDisplayData(ClientProfile profile) { Session session = null; SessionFactory sf = null; Transaction tx = null; try { Configuration configuration = new AnnotationConfiguration().configure("dynamic_hibernate.cfg.xml"); configuration.setProperty( "hibernate.connection.url", "jdbc:mysql://" + profile.getConnectionName() + ":3306/digital_signage"); sf = configuration.buildSessionFactory(); session = sf.openSession(); tx = session.beginTransaction(); session.createSQLQuery("delete from clientprofile").executeUpdate(); session.createSQLQuery("delete from layoutprofile").executeUpdate(); session.createSQLQuery("delete from feed").executeUpdate(); session.createSQLQuery("delete from image").executeUpdate(); session.createSQLQuery("delete from layout").executeUpdate(); session.createSQLQuery("delete from panel").executeUpdate(); session.createSQLQuery("delete from profile").executeUpdate(); session.createSQLQuery("delete from slideshow").executeUpdate(); session.createSQLQuery("delete from text").executeUpdate(); session.createSQLQuery("delete from video").executeUpdate(); session.createSQLQuery("delete from web").executeUpdate(); if (profile.getLayout() != null) { session.save(profile.getLayout()); if (profile.getLayout().getBgImage() != null) { duplicateFile(profile.getConnectionName(), "image", profile.getLayout().getBgImage()); } profile.setLayout(profile.getLayout()); for (Panel panel : profile.getLayout().getPanels()) { panel.setLayout(profile.getLayout()); session.save(panel); if (panel.getFeeds().size() != 0) { panel.getFeeds().iterator().next().setPanel(panel); session.save(panel.getFeeds().iterator().next()); } if (panel.getImages().size() != 0) { panel.getImages().iterator().next().setPanel(panel); session.save(panel.getImages().iterator().next()); if (panel.getImages().iterator().next().getUrl().substring(0, 7).equals("/image/")) { duplicateFile( profile.getConnectionName(), "image", panel .getImages() .iterator() .next() .getUrl() .substring(7, panel.getImages().iterator().next().getUrl().length())); } } if (panel.getSlideShows().size() != 0) { panel.getSlideShows().iterator().next().setPanel(panel); session.save(panel.getSlideShows().iterator().next()); for (String s : panel.getSlideShows().iterator().next().getImages().split(";")) { duplicateFile(profile.getConnectionName(), "image", s); } } if (panel.getTexts().size() != 0) { panel.getTexts().iterator().next().setPanel(panel); session.save(panel.getTexts().iterator().next()); } if (panel.getVideos().size() != 0) { panel.getVideos().iterator().next().setPanel(panel); session.save(panel.getVideos().iterator().next()); if (panel.getVideos().iterator().next().isLocal()) { duplicateFile( profile.getConnectionName(), "video", panel.getVideos().iterator().next().getValue()); } } if (panel.getWebs().size() != 0) { panel.getWebs().iterator().next().setPanel(panel); session.save(panel.getWebs().iterator().next()); } } } if (profile.getProfile() != null) { session.save(profile.getProfile()); profile.setProfile(profile.getProfile()); for (LayoutProfile layoutProfile : profile.getProfile().getLayoutProfiles()) { layoutProfile.setProfile(profile.getProfile()); session.save(layoutProfile.getLayout()); layoutProfile.setLayout(layoutProfile.getLayout()); session.save(layoutProfile); if (layoutProfile.getLayout().getBgImage() != null) { duplicateFile( profile.getConnectionName(), "image", layoutProfile.getLayout().getBgImage()); } for (Panel panel : layoutProfile.getLayout().getPanels()) { panel.setLayout(layoutProfile.getLayout()); session.save(panel); if (panel.getFeeds().size() != 0) { panel.getFeeds().iterator().next().setPanel(panel); session.save(panel.getFeeds().iterator().next()); } if (panel.getImages().size() != 0) { panel.getImages().iterator().next().setPanel(panel); session.save(panel.getImages().iterator().next()); if (panel.getImages().iterator().next().getUrl().substring(0, 7).equals("/image/")) { duplicateFile( profile.getConnectionName(), "image", panel .getImages() .iterator() .next() .getUrl() .substring(7, panel.getImages().iterator().next().getUrl().length())); } } if (panel.getSlideShows().size() != 0) { panel.getSlideShows().iterator().next().setPanel(panel); session.save(panel.getSlideShows().iterator().next()); for (String s : panel.getSlideShows().iterator().next().getImages().split(";")) { duplicateFile(profile.getConnectionName(), "image", s); } } if (panel.getTexts().size() != 0) { panel.getTexts().iterator().next().setPanel(panel); session.save(panel.getTexts().iterator().next()); } if (panel.getVideos().size() != 0) { panel.getVideos().iterator().next().setPanel(panel); session.save(panel.getVideos().iterator().next()); if (panel.getVideos().iterator().next().isLocal()) { duplicateFile( profile.getConnectionName(), "video", panel.getVideos().iterator().next().getValue()); } } if (panel.getWebs().size() != 0) { panel.getWebs().iterator().next().setPanel(panel); session.save(panel.getWebs().iterator().next()); } } } } session.save(profile); tx.commit(); } catch (Exception e) { e.printStackTrace(); if (tx != null) { tx.rollback(); } } finally { session.clear(); session.close(); } }