Example #1
0
  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();
    }
  }
Example #2
0
  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();
    }
  }