Example usage for javax.persistence EntityManager getTransaction

List of usage examples for javax.persistence EntityManager getTransaction

Introduction

In this page you can find the example usage for javax.persistence EntityManager getTransaction.

Prototype

public EntityTransaction getTransaction();

Source Link

Document

Return the resource-level EntityTransaction object.

Usage

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void saveUser(BakerUser bu) {
    logger.info("Will save BakerUser = " + bu.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//ww  w .  java 2  s . c  o  m

    entityManager.persist(bu);

    entityManager.flush();
    entityTransaction.commit();

}

From source file:org.traccar.web.server.model.DataServiceImpl.java

private void createUser(EntityManager entityManager, User user) {
    synchronized (entityManager) {
        entityManager.getTransaction().begin();
        try {//from   w  w  w  .j  av a2s  .  c  o  m
            entityManager.persist(user);
            entityManager.getTransaction().commit();
        } catch (RuntimeException e) {
            entityManager.getTransaction().rollback();
            throw e;
        }
    }
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void deleteUser(int userid) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    BakerUser u = entityManager.find(BakerUser.class, userid);

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//  w w  w. j a  v a2s . c  om

    entityManager.remove(u);

    entityTransaction.commit();
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void deleteProperty(int propid) {

    EntityManager entityManager = entityManagerFactory.createEntityManager();
    BakerProperty c = entityManager.find(BakerProperty.class, propid);

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*  www  .ja  va2 s.  c  o  m*/
    entityManager.remove(c);
    entityTransaction.commit();

}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void deleteSubscribedResource(int smId) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    SubscribedResource sm = entityManager.find(SubscribedResource.class, smId);

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//  w  ww .  java2  s. com
    entityManager.remove(sm);
    entityTransaction.commit();
}

From source file:org.traccar.web.server.model.DataServiceImpl.java

@Override
public Device updateDevice(Device device) {
    EntityManager entityManager = getSessionEntityManager();
    synchronized (entityManager) {
        entityManager.getTransaction().begin();
        try {//from   w  w  w  .  j a  v a2  s.c  o m
            device = entityManager.merge(device);
            entityManager.getTransaction().commit();
            return device;
        } catch (RuntimeException e) {
            entityManager.getTransaction().rollback();
            throw e;
        }
    }
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void deleteDeployment(int id) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    DeploymentDescriptor c = entityManager.find(DeploymentDescriptor.class, id);

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//from   w ww. ja  v a2s . co m
    entityManager.remove(c);
    entityTransaction.commit();

}

From source file:com.doculibre.constellio.wicket.panels.results.tagging.SearchResultEditTaggingPanel.java

public SearchResultEditTaggingPanel(String id, final SolrDocument doc, final IDataProvider dataProvider,
        Thesaurus source) {//from  w  ww.  ja v a  2 s .c  o  m
    super(id);
    if (source == null) {
        freeTextTags = true;
        source = new Thesaurus(getLocalizer().getString("freeTextTags", SearchResultEditTaggingPanel.this));
    }
    RecordServices recordServices = ConstellioSpringUtils.getRecordServices();
    Record record = recordServices.get(doc);
    recordModel = new RecordModel(record);
    taggingSourceModel = new EntityModel<Thesaurus>(source);

    filterAddForm = new Form("filterAddForm");
    filterAddForm.setOutputMarkupId(true);

    IModel taggingSourceChoicesModel = new LoadableDetachableModel() {
        @Override
        protected Object load() {
            List<Object> taggingSources = new ArrayList<Object>();

            taggingSources.add(
                    new Thesaurus(getLocalizer().getString("freeTextTags", SearchResultEditTaggingPanel.this)));

            SkosServices skosServices = ConstellioSpringUtils.getSkosServices();
            Record record = recordModel.getObject();
            ConnectorInstance connectorInstance = record.getConnectorInstance();
            RecordCollection collection = connectorInstance.getRecordCollection();
            Map<String, Object> criteria = new HashMap<String, Object>();
            criteria.put("recordCollection", collection);
            taggingSources.addAll(skosServices.list(criteria));

            return taggingSources;
        }
    };
    IChoiceRenderer taggingSourceRenderer = new ChoiceRenderer("dcTitle");
    taggingSourceField = new DropDownChoice("taggingSource", taggingSourceModel, taggingSourceChoicesModel,
            taggingSourceRenderer);
    taggingSourceField.setVisible(isSourceSelectionVisible());
    taggingSourceField.add(new AjaxFormComponentUpdatingBehavior("onchange") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            target.addComponent(availableTagsContainer);
            target.addComponent(appliedTagsContainer);
            target.addComponent(filterAddField);
            target.addComponent(filterAddButton);
        }
    });

    filterAddField = new TextField("filterAddField", new Model());
    filterAddField.setOutputMarkupId(true);
    filterAddField.add(new AjaxFormComponentUpdatingBehavior("onkeyup") {
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            String input = filterAddField.getModelObjectAsString();
            availableTagsModel.setFilter(input);
            appliedTagsModel.setFilter(input);
            if (isFreeTextTagSource()) {
                target.addComponent(availableTagsContainer);
                target.addComponent(appliedTagsContainer);
            }
        }
    });

    filterAddButton = new AjaxButton("filterAddButton") {
        @Override
        protected void onSubmit(AjaxRequestTarget target, Form form) {
            String tagName = (String) filterAddField.getModelObject();
            if (StringUtils.isNotBlank(tagName)) {
                if (isFreeTextTagSource()) {
                    FreeTextTagServices freeTextTagServices = ConstellioSpringUtils.getFreeTextTagServices();
                    FreeTextTag existingTag = freeTextTagServices.get(tagName);
                    if (existingTag == null) {
                        FreeTextTag newTag = new FreeTextTag();
                        newTag.setFreeText(tagName);

                        EntityManager entityManager = ConstellioPersistenceContext.getCurrentEntityManager();
                        if (!entityManager.getTransaction().isActive()) {
                            entityManager.getTransaction().begin();
                        }
                        freeTextTagServices.makePersistent(newTag);
                        entityManager.getTransaction().commit();

                        availableTagsModel.setFilter(null);
                        appliedTagsModel.setFilter(null);
                        filterAddField.clearInput();
                        filterAddField.setModelObject(null);
                        target.addComponent(filterAddField);
                        target.addComponent(availableTagsContainer);
                        target.addComponent(appliedTagsContainer);
                    }
                } else {
                    target.addComponent(availableTagsContainer);
                }
            }
        }
    };
    filterAddButton.add(new AttributeModifier("value", true, new LoadableDetachableModel() {
        @Override
        protected Object load() {
            String key = isFreeTextTagSource() ? "add" : "filter";
            return getLocalizer().getString(key, SearchResultEditTaggingPanel.this);
        }
    }));

    availableTagsContainer = new WebMarkupContainer("availableTagsContainer");
    availableTagsContainer.setOutputMarkupId(true);
    availableTagsModel = new FilteredObjectsModel() {
        @Override
        protected List<Object> filter(String filter) {
            List<Object> matches = new ArrayList<Object>();
            Thesaurus taggingSource = taggingSourceModel.getObject();

            if (taggingSource == null || taggingSource.getId() == null) {
                if (StringUtils.isNotBlank(filter)) {
                    filter = filter + "*";
                }
                FreeTextTagServices taggingServices = ConstellioSpringUtils.getFreeTextTagServices();
                if (StringUtils.isBlank(filter)) {
                    List<FreeTextTag> first100 = taggingServices.list(100);
                    matches.addAll(first100);
                } else {
                    Set<FreeTextTag> searchResults = taggingServices.search(filter);
                    matches.addAll(searchResults);
                }
            } else {
                SkosServices skosServices = ConstellioSpringUtils.getSkosServices();
                Set<SkosConcept> searchResults = skosServices.searchAllLabels(filter, taggingSource, null);
                matches.addAll(searchResults);
            }
            return matches;
        }
    };
    availableTagsListView = new ListView("availableTags", availableTagsModel) {
        @Override
        protected void populateItem(ListItem item) {
            ConstellioEntity tagSource = (ConstellioEntity) item.getModelObject();
            final ReloadableEntityModel<ConstellioEntity> tagSourceModel = new ReloadableEntityModel<ConstellioEntity>(
                    tagSource);
            final ModalWindow detailsModal = new ModalWindow("detailsModal");
            item.add(detailsModal);

            final AjaxLink detailsLink = new AjaxLink("detailsLink") {
                @Override
                public void onClick(AjaxRequestTarget target) {
                    ConstellioEntity tagSource = (ConstellioEntity) tagSourceModel.getObject();
                    if (tagSource instanceof SkosConcept) {
                        SkosConcept skosConcept = (SkosConcept) tagSource;
                        detailsModal.setContent(
                                new SkosConceptModalPanel(detailsModal.getContentId(), skosConcept));
                        detailsModal.show(target);
                    }
                }
            };
            item.add(detailsLink);
            detailsLink.setEnabled(tagSource instanceof SkosConcept);

            detailsLink.add(new Label("name", new LoadableDetachableModel() {
                @Override
                protected Object load() {
                    String name;
                    ConstellioEntity tagSource = (ConstellioEntity) tagSourceModel.getObject();
                    if (tagSource != null) {
                        if (tagSource instanceof FreeTextTag) {
                            FreeTextTag freeTextTag = (FreeTextTag) tagSource;
                            name = freeTextTag.getFreeText();
                        } else {
                            SkosConcept skosConcept = (SkosConcept) tagSource;
                            name = skosConcept.getPrefLabel(getLocale());
                        }
                    } else {
                        name = "null";
                    }
                    return name;
                }
            }));

            item.add(new AjaxLink("addLink") {
                @Override
                public void onClick(AjaxRequestTarget target) {
                    Record record = recordModel.getObject();
                    ConstellioEntity tagSource = (ConstellioEntity) tagSourceModel.getObject();
                    if (tagSource instanceof FreeTextTag) {
                        FreeTextTag freeTextTag = (FreeTextTag) tagSource;
                        record.addFreeTextTag(freeTextTag, true);
                    } else {
                        SkosConcept skosConcept = (SkosConcept) tagSource;
                        record.addSkosConcept(skosConcept, true);
                    }
                    record.setUpdateIndex(true);

                    RecordServices recordServices = ConstellioSpringUtils.getRecordServices();

                    SolrServer solrServer = SolrCoreContext
                            .getSolrServer(record.getConnectorInstance().getRecordCollection());
                    try {
                        ConstellioPersistenceUtils.beginTransaction();
                        recordServices.makePersistent(record);
                        try {
                            solrServer.commit();
                        } catch (Throwable t) {
                            try {
                                solrServer.rollback();
                            } catch (Exception e) {
                                throw new RuntimeException(t);
                            }
                        }
                    } finally {
                        ConstellioPersistenceUtils.finishTransaction(false);
                    }

                    target.addComponent(availableTagsContainer);
                    target.addComponent(appliedTagsContainer);
                }

                @Override
                public boolean isVisible() {
                    boolean visible = super.isVisible();
                    if (visible) {
                        Record record = recordModel.getObject();
                        if (tagSourceModel.getObject() instanceof FreeTextTag) {
                            FreeTextTag freeTextTag = (FreeTextTag) tagSourceModel.getObject();
                            visible = !record.hasFreeTextTag(freeTextTag);
                        } else {
                            SkosConcept skosConcept = (SkosConcept) tagSourceModel.getObject();
                            visible = !record.hasSkosConcept(skosConcept);
                        }
                    }
                    return visible;
                }

                @Override
                public void detachModels() {
                    tagSourceModel.detach();
                    super.detachModels();
                }
            });
        }
    };

    appliedTagsContainer = new WebMarkupContainer("appliedTagsContainer");
    appliedTagsContainer.setOutputMarkupId(true);
    appliedTagsModel = new FilteredObjectsModel() {
        @Override
        protected List<RecordTag> filter(String filter) {
            List<RecordTag> matches = new ArrayList<RecordTag>();
            Record record = recordModel.getObject();
            Thesaurus source = taggingSourceModel.getObject();
            for (RecordTag recordTag : record.getIncludedRecordTags(freeTextTags ? null : source, true)) {
                //                    if (StringUtils.isEmpty(filter)
                //                        || recordTag.getName(getLocale()).toLowerCase().indexOf(filter.toLowerCase()) != -1) {
                matches.add(recordTag);
                //                    }
            }
            return matches;
        }
    };
    appliedTagsListView = new ListView("appliedTags", appliedTagsModel) {
        @Override
        protected void populateItem(ListItem item) {
            RecordTag recordTag = (RecordTag) item.getModelObject();
            final RecordTagModel recordTagModel = new RecordTagModel(recordTag);

            ConstellioEntity tagSource;
            if (recordTag.getFreeTextTag() != null) {
                tagSource = recordTag.getFreeTextTag();
            } else {
                tagSource = recordTag.getSkosConcept();
            }
            final ReloadableEntityModel<ConstellioEntity> tagSourceModel = new ReloadableEntityModel<ConstellioEntity>(
                    tagSource);
            final ModalWindow detailsModal = new ModalWindow("detailsModal");
            item.add(detailsModal);

            final AjaxLink detailsLink = new AjaxLink("detailsLink") {
                @Override
                public void onClick(AjaxRequestTarget target) {
                    ConstellioEntity tagSource = (ConstellioEntity) tagSourceModel.getObject();
                    if (tagSource instanceof SkosConcept) {
                        SkosConcept skosConcept = (SkosConcept) tagSource;
                        detailsModal.setContent(
                                new SkosConceptModalPanel(detailsModal.getContentId(), skosConcept));
                        detailsModal.show(target);
                    }
                }
            };
            item.add(detailsLink);
            detailsLink.setEnabled(tagSource instanceof SkosConcept);

            detailsLink.add(new Label("name", new LoadableDetachableModel() {
                @Override
                protected Object load() {
                    String name;
                    ConstellioEntity tagSource = (ConstellioEntity) tagSourceModel.getObject();
                    if (tagSource != null) {
                        if (tagSource instanceof FreeTextTag) {
                            FreeTextTag freeTextTag = (FreeTextTag) tagSource;
                            name = freeTextTag.getFreeText();
                        } else {
                            SkosConcept skosConcept = (SkosConcept) tagSource;
                            name = skosConcept.getPrefLabel(getLocale());
                        }
                    } else {
                        name = "null";
                    }
                    return name;
                }
            }));
            detailsLink.add(new AttributeModifier("style", true, new LoadableDetachableModel() {
                @Override
                protected Object load() {
                    RecordTag recordTag = recordTagModel.getObject();
                    return recordTag.isExcluded() ? "text-decoration: line-through;" : "text-decoration:none";
                }
            }));
            item.add(new AjaxLink("removeLink") {
                @Override
                public void onClick(AjaxRequestTarget target) {
                    Record record = recordModel.getObject();
                    RecordTag recordTag = recordTagModel.getObject();
                    boolean excluded = recordTag.isExcluded();
                    if (excluded) {
                        record.getRecordTags().remove(recordTag);
                    } else {
                        recordTag.setManual(true);
                        recordTag.setExcluded(true);
                    }
                    record.setUpdateIndex(true);

                    RecordServices recordServices = ConstellioSpringUtils.getRecordServices();

                    SolrServer solrServer = SolrCoreContext
                            .getSolrServer(record.getConnectorInstance().getRecordCollection());
                    try {
                        ConstellioPersistenceUtils.beginTransaction();
                        recordServices.makePersistent(record);
                        try {
                            solrServer.commit();
                        } catch (Throwable t) {
                            try {
                                solrServer.rollback();
                            } catch (Exception e) {
                                throw new RuntimeException(t);
                            }
                        }
                    } finally {
                        ConstellioPersistenceUtils.finishTransaction(false);
                    }

                    target.addComponent(availableTagsContainer);
                    target.addComponent(appliedTagsContainer);
                }

                @Override
                public void detachModels() {
                    recordTagModel.detach();
                    tagSourceModel.detach();
                    super.detachModels();
                }
            });
        }
    };

    closeLink = new AjaxLink("closeLink") {
        @Override
        public void onClick(AjaxRequestTarget target) {
            SearchResultTaggingPanel parent = (SearchResultTaggingPanel) findParent(
                    SearchResultTaggingPanel.class);
            target.addComponent(parent);
            ModalWindow.closeCurrent(target);
        }
    };

    add(taggingSourceField);
    add(new Label("sourceName", source.getDcTitle()).setVisible(!isSourceSelectionVisible()));
    add(filterAddForm);
    filterAddForm.add(filterAddField);
    filterAddForm.add(filterAddButton);

    add(availableTagsContainer);
    availableTagsContainer.add(availableTagsListView);

    add(appliedTagsContainer);
    appliedTagsContainer.add(appliedTagsListView);

    add(closeLink);
}

From source file:nl.b3p.viewer.stripes.ApplicationActionBean.java

public Resolution saveCache() throws JSONException, IOException {
    Resolution view = view();//from  w w  w  . j  a  v  a 2 s  .c  o m

    EntityManager em = Stripersist.getEntityManager();
    Resolution r = checkRestriction(context, application, em);
    if (r != null) {
        return r;
    }
    SelectedContentCache cache = new SelectedContentCache();
    JSONObject sc = cache.createSelectedContent(application, false, false, false, em);
    application.getDetails().put("selected_content_cache", new ClobElement(sc.toString()));
    em.getTransaction().commit();
    return view;
}

From source file:org.traccar.web.server.model.DataServiceImpl.java

@Override
public User removeUser(User user) {
    User currentUser = getSessionUser();
    if (currentUser.getAdmin()) {
        EntityManager entityManager = getSessionEntityManager();
        synchronized (entityManager) {
            entityManager.getTransaction().begin();
            try {
                user = entityManager.merge(user);
                user.getDevices().clear();
                entityManager.remove(user);
                entityManager.getTransaction().commit();
                return user;
            } catch (RuntimeException e) {
                entityManager.getTransaction().rollback();
                throw e;
            }/*from  w w w  .jav a2 s .  c  om*/
        }
    } else {
        throw new SecurityException();
    }
}