Example usage for org.hibernate Session refresh

List of usage examples for org.hibernate Session refresh

Introduction

In this page you can find the example usage for org.hibernate Session refresh.

Prototype

void refresh(Object object);

Source Link

Document

Re-read the state of the given instance from the underlying database.

Usage

From source file:pt.webdetails.cda.cache.CacheActivator.java

License:Open Source License

public void processQueries(Session s, PriorityQueue<CachedQuery> queue) {

    CacheScheduleManager.logger.debug("Refreshing cached query...");
    CachedQuery q = queue.poll();// ww  w  .  j  a v a  2 s  .c o  m
    try {
        IPentahoSession session = PentahoSessionHolder.getSession();
        s.refresh(q);
        setSession(q);
        q.execute();
        PentahoSessionHolder.setSession(session);
    } catch (Exception ex) {
        CacheScheduleManager.logger.error("Failed to execute " + q.toString());
    }

    q.updateNext();
    queue.add(q);
    s.update(q);
}

From source file:pt.webdetails.cda.cache.scheduler.CacheActivator.java

License:Open Source License

public void processQueries(Session s, Queue<CachedQuery> queue) {

    logger.debug("Refreshing cached query");
    CachedQuery q = queue.poll();/*ww  w . ja v  a  2s.  c o  m*/
    try {
        IPentahoSession session = PentahoSessionHolder.getSession();
        s.refresh(q);
        setSession(q);
        q.execute();
        PentahoSessionHolder.setSession(session);
    } catch (Exception ex) {
        logger.error("Failed to execute " + q.toString(), ex);
    }

    q.updateNext();
    queue.add(q);
    s.update(q);
}

From source file:quizfun.model.dao.HibernateGameDao.java

License:Open Source License

@Override
public Game findGameById(Long id) {
    if (logger.isDebugEnabled()) {
        logger.debug("Going to find Game");
    }//from   ww  w . ja v a  2  s .  c  o  m
    Session session = getSession(false);

    Game game = new Game();
    try {
        game = (Game) session.get(Game.class, id);
        session.refresh(game);
        Hibernate.initialize(game.getQuestions());
    } catch (HibernateException e) {
        throw convertHibernateAccessException(e);
    }

    if (game != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Returning " + game.getId() + " Question");
        }
        return game;
    }
    return null;
}

From source file:smops.PageAnalyzer.java

private static void analyseForms(Document doc, Set<String> current_form_bodies, Business business, File input,
        String url, Session session) throws HibernateException {
    Elements form_elements = doc.select("form");
    System.out.println("#forms in page: " + form_elements.size());
    //            System.out.println(biz_website_a.toString());
    if (form_elements != null) {
        for (Element f : form_elements) {
            int numberOfValidInputs = getNumberOfValidInputs(f);
            if (numberOfValidInputs == 0) {
                System.out.println("number of valid inputs is zero");
                continue;
            }/*  ww  w  .  ja v  a 2s .  co  m*/
            //                    session.beginTransaction();
            Form form_obj = new Form();
            String action = f.attr("action");
            String form_html = f.html();
            String form_text = f.text();
            boolean isAccepted = isAcceptedForm(form_obj, action, form_html, form_text, current_form_bodies);
            if (!isAccepted) {
                System.out.println("form is not accepted!");
                continue;
            }
            String title = getBestTitle(f);
            String purpose = getBestPurpose(f);
            form_obj.setAction(action);
            form_obj.setBusiness(business);
            form_obj.setFileName(input.getName());
            form_obj.setPageUrl(url);
            form_obj.setTitle(Utils.truncate(title, 2048));
            form_obj.setHtml(Utils.truncate(form_html, 65535));
            form_obj.setPurpose(purpose);
            session.beginTransaction();
            session.save(form_obj);
            System.out.println("form saved for " + url);
            session.getTransaction().commit();
            session.flush();
            System.out.println("form_obj=" + form_obj.getId());
            session.refresh(form_obj);
            System.out.println("form=" + form_obj);
            //                    session.getTransaction().commit();

            final Elements inputs = f.select("input");
            int input_count = 0;
            //                    session.beginTransaction();
            for (Element i : inputs) {
                String type = i.attr("type");
                //                    if (!(type.equals("text") || type.equals("password") || type.equals("radio") || type.equals("checkbox"))) {
                //                        continue;
                //                    }
                if (!isAcceptedInputType(type)) {
                    continue;
                }
                input_count++;
                String name = i.attr("name");
                String value = i.attr("value");
                String html = i.html();
                String label = getBestLabel(i);
                String infoType = getBestInfoType(i);
                Field field = new Field();
                field.setType(type);
                field.setName(name);
                field.setValue(Utils.truncate(value, 512));
                field.setLabel(Utils.truncate(label, 1024));
                field.setHtml(html);
                field.setInfoType(infoType);
                field.setForm(form_obj);
                form_obj.getFields().add(field);
                //                        System.out.println("field=" + field);
                session.beginTransaction();
                session.saveOrUpdate(form_obj);
                session.save(field);
                System.out.println("field " + name + " saved for " + form_obj.getId());
                session.getTransaction().commit();

            }
            session.save(form_obj);
            //                    session.getTransaction().commit();
            System.out.println("Input#:" + input_count);
        }
    }
}

From source file:storybook.action.LangToolAction.java

License:Open Source License

@Override
public void actionPerformed(ActionEvent e) {
    try {/*from  ww w .jav  a2 s. com*/
        final LangToolMain langTool = new LangToolMain(mainFrame);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    String text = "";
                    if (scene != null) {
                        BookModel model = mainFrame.getBookModel();
                        Session session = model.beginTransaction();
                        session.refresh(scene);
                        text = HtmlUtil.htmlToText(scene.getText(), true);
                        model.commit();
                    }
                    langTool.createGUI();
                    langTool.showGUI();
                    if (!text.isEmpty()) {
                        langTool.setTextToCheck(text);
                    }
                } catch (Exception | NoClassDefFoundError e2) {
                    System.err.println("LangToolAction.actionPerformed(?) Exception :" + e2.getMessage());
                }
            }
        });
    } catch (Exception e1) {
        System.err
                .println("LangToolAction.actionPerformed(" + e.toString() + ") Exception :" + e1.getMessage());
    }
}

From source file:storybook.model.EntityUtil.java

License:Open Source License

public static void copyEntity(MainFrame mainFrame, AbstractEntity entity) {
    AbstractEntityHandler handler = getEntityHandler(mainFrame, entity);
    AbstractEntity newEntity = handler.createNewEntity();
    BookModel model = mainFrame.getBookModel();
    Session session = model.beginTransaction();
    session.refresh(entity);
    copyEntityProperties(mainFrame, entity, newEntity);
    //      AbstractEntity newEntity = cloneEntity(mainFrame, entity);
    markCopiedEntity(mainFrame, newEntity);

    List<Person> persons = new ArrayList<>();
    List<Location> locations = new ArrayList<>();
    List<Item> items = new ArrayList<>();
    List<Strand> strands = new ArrayList<>();
    if (entity instanceof Scene) {
        // correct date / relative scene
        Scene scene = (Scene) entity;/*from www . j a v a  2 s . c  om*/
        Scene newScene = (Scene) newEntity;
        if (!scene.hasRelativeScene()) {
            newScene.removeRelativeScene();
        }
        persons = scene.getPersons();
        locations = scene.getLocations();
        strands = scene.getStrands();
    }
    List<Attribute> attributes = new ArrayList<>();
    if (entity instanceof Person) {
        Person person = (Person) entity;
        attributes = person.getAttributes();
    }
    model.commit();
    BookController ctrl = mainFrame.getBookController();
    ctrl.newEntity(newEntity);

    // re-set "stolen" bag links
    if (entity instanceof Scene) {
        Scene scene = (Scene) entity;

        List<Person> copyPersons = new ArrayList<>();
        for (Person person : persons) {
            copyPersons.add(person);
        }
        scene.setPersons(copyPersons);

        List<Location> copyLocations = new ArrayList<>();
        for (Location location : locations) {
            copyLocations.add(location);
        }
        scene.setLocations(copyLocations);

        List<Strand> copyStrands = new ArrayList<>();
        for (Strand strand : strands) {
            copyStrands.add(strand);
        }
        scene.setStrands(copyStrands);
    }
    if (entity instanceof Person) {
        Person person = (Person) entity;
        List<Attribute> copyAttributes = new ArrayList<>();
        for (Attribute attribute : attributes) {
            copyAttributes.add(attribute);
        }
        person.setAttributes(copyAttributes);
    }
    ctrl.updateEntity(entity);

}

From source file:storybook.model.EntityUtil.java

License:Open Source License

public static List<Attribute> getEntityAttributes(MainFrame mainFrame, AbstractEntity entity) {
    if (entity.isTransient()) {
        return new ArrayList<>();
    }//from  ww  w .  j  a  v a  2s .  co m
    if (entity instanceof Person) {
        BookModel model = mainFrame.getBookModel();
        Session session = model.beginTransaction();
        Person person = (Person) entity;
        session.refresh(person);
        List<Attribute> ret = person.getAttributes();
        model.commit();
        return ret;
    }
    return new ArrayList<>();
}

From source file:storybook.model.EntityUtil.java

License:Open Source License

public static void abandonEntityChanges(MainFrame mainFrame, AbstractEntity entity) {
    try {/*w  ww .j  a  va 2  s.co m*/
        if (entity.isTransient()) // nothing to do for a new entity
        {
            return;
        }
        BookModel model = mainFrame.getBookModel();
        Session session = model.getSession();
        if (session != null && session.isOpen()) {
            Transaction transaction = session.beginTransaction();
            session.refresh(entity);
            transaction.commit();
        }
    } catch (HibernateException e) {
        SbApp.error("EntityUtil.copyEntityProperties()", e);
    }
}

From source file:storybook.model.EntityUtil.java

License:Open Source License

private static void addInfo(MainFrame mainFrame, AbstractEntity entity, StringBuffer buf, boolean truncate) {
    AbstractEntityHandler entityHandler = getEntityHandler(mainFrame, entity);
    Class<? extends AbstractEntity> clazz = entity.getClass();
    for (SbColumn col : entityHandler.getColumns()) {
        String methodName = "get" + col.getMethodName();
        if (methodName.equals("getId") || methodName.equals("getIcon")) {
            continue;
        }/*from   ww w  .jav  a2  s . c  o  m*/
        if (col.isHideOnInfo()) {
            continue;
        }
        buf.append("<p>");
        buf.append(HtmlUtil.getBold(col.toString()));
        buf.append(": ");
        try {
            BookModel model = mainFrame.getBookModel();
            Session session = model.beginTransaction();
            session.refresh(entity);
            Method method = clazz.getMethod(methodName);
            Object ret = method.invoke(entity);
            if (ret != null) {
                String str = "";
                if (ret instanceof Boolean) {
                    str = ((Boolean) ret ? I18N.getMsg("msg.common.yes") : I18N.getMsg("msg.common.no"));
                } else if (ret instanceof List<?>) {
                    List<?> list = (List<?>) ret;
                    if (!list.isEmpty()) {
                        StringBuilder buf2 = new StringBuilder();
                        buf2.append("<ul>");
                        for (Object o : list) {
                            session.refresh(o);
                            buf2.append("<li>");
                            buf2.append(o.toString());
                            buf2.append("</li>");
                        }
                        buf2.append("</ul>");
                        str = buf2.toString();
                    }
                } else if (ret instanceof Color) {
                    Color clr = (Color) ret;
                    str = HtmlUtil.getColorSpan(clr);
                } else if (ret instanceof Date) {
                    Date date = (Date) ret;
                    DateFormat formatter;
                    if (DateUtil.isZeroTimeDate(date)) {
                        formatter = I18N.getLongDateFormatter();
                        date = DateUtil.getZeroTimeDate(date);
                    } else if (entity instanceof TimeEvent) {
                        formatter = new SimpleDateFormat(((TimeEvent) entity).getStepFormat());
                    } else {
                        formatter = I18N.getDateTimeFormatter();
                    }
                    str = formatter.format(date);
                } else {
                    str = ret.toString();
                    if (methodName.endsWith("Notes") || methodName.endsWith("Description")
                            || methodName.endsWith("Summary")) {
                        str = HtmlUtil.getCleanHtml(str);
                    }
                }
                if (truncate) {
                    str = TextUtil.truncateString(HtmlUtil.htmlToText(str), 200);
                }
                buf.append(str);
            }
            model.commit();
        } catch (HibernateException | NoSuchMethodException | SecurityException | IllegalAccessException
                | IllegalArgumentException | InvocationTargetException e) {
            System.err.println("EntityUtil.addInfo(mainFrame, entity) Exception : " + e.getMessage());
        }
        buf.append("</p>\n");
    }
}

From source file:storybook.model.EntityUtil.java

License:Open Source License

public static void refresh(MainFrame mainFrame, AbstractEntity entity) {
    BookModel model = mainFrame.getBookModel();
    Session session = model.beginTransaction();
    session.refresh(entity);
    model.commit();/* ww w  . j  a  v  a 2 s  . c o m*/
}