Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Turn AutoCompletes into beans with Transactional
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Feb 22, 2016
1 parent 5599e74 commit bddaaec
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.jpa.api.transaction.Transactional;
import org.zanata.security.annotations.CheckLoggedIn;
import org.zanata.security.annotations.CheckPermission;
import org.zanata.security.annotations.CheckRole;
Expand Down Expand Up @@ -110,6 +112,7 @@ public void updateLanguage(String language) {
}

@CheckRole("admin")
@Transactional
public String save() {
if (!isLanguageNameValid()) {
return null; // not success
Expand Down
23 changes: 17 additions & 6 deletions zanata-war/src/main/java/org/zanata/action/ProjectHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import java.util.Set;

import javax.annotation.Nullable;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Any;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ValueChangeEvent;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
Expand Down Expand Up @@ -96,7 +96,7 @@
@Slf4j
//@GroupedConversationScoped
//@ConversationGroup(ProjectSlug.class)
@RequestScoped
@ViewScoped
public class ProjectHome extends SlugHome<HProject> implements
HasLanguageSettings {

Expand Down Expand Up @@ -263,8 +263,8 @@ public boolean apply(HLocale input) {
private String selectedProjectType;

@Getter
private ProjectMaintainersAutocomplete maintainerAutocomplete =
new ProjectMaintainersAutocomplete();
@Inject
private ProjectMaintainersAutocomplete maintainerAutocomplete;

@Getter
private AbstractListFilter<HPerson> maintainerFilter =
Expand Down Expand Up @@ -650,6 +650,7 @@ private void refreshDisabledLocales() {
disabledLocales = null;
}

@Transactional
public void setRestrictedByRole(String key, boolean checked) {
identity.checkPermission(instance, "update");
getInstance().setRestrictedByRoles(checked);
Expand Down Expand Up @@ -1142,7 +1143,16 @@ private boolean checkViewObsolete() {
&& identity.hasPermission("HProject", "view-obsolete");
}

public class ProjectMaintainersAutocomplete extends MaintainerAutocomplete {
@ViewScoped
public static class ProjectMaintainersAutocomplete extends MaintainerAutocomplete {

@Inject
private ProjectHome projectHome;

private HProject getInstance() {
return projectHome.getInstance();
}


@Override
protected List<HPerson> getMaintainers() {
Expand All @@ -1155,6 +1165,7 @@ protected List<HPerson> getMaintainers() {
* Action when an item is selected
*/
@Override
@Transactional
public void onSelectItemAction() {
if (StringUtils.isEmpty(getSelectedItem())) {
return;
Expand All @@ -1169,7 +1180,7 @@ public void onSelectItemAction() {
.getInstance(ProjectHome.class);
projectHome.update();
reset();
maintainerFilter.reset();
projectHome.getMaintainerFilter().reset();

getFacesMessages().addGlobal(FacesMessage.SEVERITY_INFO,
msgs.format("jsf.project.MaintainerAdded",
Expand Down
86 changes: 54 additions & 32 deletions zanata-war/src/main/java/org/zanata/action/VersionGroupHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Any;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ValueChangeEvent;

import org.apache.commons.lang.StringUtils;
Expand All @@ -51,15 +51,13 @@
import org.zanata.security.annotations.Authenticated;
import org.zanata.service.SlugEntityService;
import org.zanata.service.VersionGroupService;
import org.zanata.service.impl.VersionGroupServiceImpl;
import org.zanata.ui.AbstractAutocomplete;
import org.zanata.ui.AbstractListFilter;
import org.zanata.ui.InMemoryListFilter;
import org.zanata.ui.autocomplete.LocaleAutocomplete;
import org.zanata.ui.autocomplete.MaintainerAutocomplete;
import org.zanata.ui.faces.FacesMessages;
import org.zanata.util.ComparatorUtil;
import org.zanata.util.ServiceLocator;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
Expand All @@ -72,7 +70,7 @@
@Named("versionGroupHome")
//@GroupedConversationScoped
//@ConversationGroup(VersionGroupSlug.class)
@RequestScoped
@ViewScoped
public class VersionGroupHome extends SlugHome<HIterationGroup>
implements Serializable {
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -102,15 +100,16 @@ public class VersionGroupHome extends SlugHome<HIterationGroup>
private ZanataIdentity identity;

@Getter
private GroupMaintainerAutocomplete maintainerAutocomplete =
new GroupMaintainerAutocomplete();
@Inject
private GroupMaintainerAutocomplete maintainerAutocomplete;

@Getter
private VersionAutocomplete versionAutocomplete = new VersionAutocomplete();
@Inject
private VersionAutocomplete versionAutocomplete;

@Getter
private GroupLocaleAutocomplete localeAutocomplete =
new GroupLocaleAutocomplete();
@Inject
private GroupLocaleAutocomplete localeAutocomplete;

@Getter
private AbstractListFilter<HPerson> maintainerFilter =
Expand Down Expand Up @@ -301,43 +300,63 @@ public void validateSuppliedId() {
clearSlugs();
}

public class GroupMaintainerAutocomplete extends MaintainerAutocomplete {
@ViewScoped
public static class GroupMaintainerAutocomplete extends MaintainerAutocomplete {

@Inject
private VersionGroupHome versionGroupHome;

@Inject
private ZanataIdentity identity;

@Override
protected List<HPerson> getMaintainers() {
return getInstanceMaintainers();
return versionGroupHome.getInstanceMaintainers();
}

private HIterationGroup getInstance() {
return versionGroupHome.getInstance();
}

/**
* Action when an item is selected
*/
@Override
@Transactional
public void onSelectItemAction() {
if (StringUtils.isEmpty(getSelectedItem())) {
return;
}

identity.checkPermission(instance, "update");
identity.checkPermission(getInstance(), "update");
HPerson maintainer = personDAO.findByUsername(getSelectedItem());
getInstance().getMaintainers().add(maintainer);
getVersionGroupHome().update(conversationScopeMessages);
versionGroupHome.update(conversationScopeMessages);
reset();
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.format("jsf.MaintainerAddedToGroup",
maintainer.getName()));
}
}

public class VersionAutocomplete extends
@ViewScoped
public static class VersionAutocomplete extends
AbstractAutocomplete<HProjectIteration> {
private ProjectIterationDAO projectIterationDAO = ServiceLocator
.instance().getInstance(ProjectIterationDAO.class);
@Inject
private ProjectIterationDAO projectIterationDAO;

private VersionGroupService versionGroupServiceImpl = ServiceLocator
.instance().getInstance(VersionGroupServiceImpl.class);
@Inject
private VersionGroupService versionGroupServiceImpl;

private ZanataIdentity identity = ServiceLocator.instance()
.getInstance(ZanataIdentity.class);
@Inject
private VersionGroupHome versionGroupHome;

@Inject
private ZanataIdentity identity;

private HIterationGroup getInstance() {
return versionGroupHome.getInstance();
}

@Override
public List<HProjectIteration> suggest() {
Expand Down Expand Up @@ -366,12 +385,12 @@ public void onSelectItemAction() {
return;
}

identity.checkPermission(instance, "update");
identity.checkPermission(getInstance(), "update");
HProjectIteration version =
projectIterationDAO.findById(new Long(getSelectedItem()));
getInstance().getProjectIterations().add(version);

getVersionGroupHome().update(conversationScopeMessages);
versionGroupHome.update(conversationScopeMessages);
reset();

conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
Expand All @@ -380,14 +399,17 @@ public void onSelectItemAction() {
}
}

private static VersionGroupHome getVersionGroupHome() {
return ServiceLocator.instance()
.getInstance(VersionGroupHome.class);
}
@ViewScoped
public static class GroupLocaleAutocomplete extends LocaleAutocomplete {
@Inject
private VersionGroupHome versionGroupHome;

public class GroupLocaleAutocomplete extends LocaleAutocomplete {
private ZanataIdentity identity = ServiceLocator.instance()
.getInstance(ZanataIdentity.class);
@Inject
private ZanataIdentity identity;

private HIterationGroup getInstance() {
return versionGroupHome.getInstance();
}

@Override
protected Set<HLocale> getLocales() {
Expand Down Expand Up @@ -424,15 +446,15 @@ public void onSelectItemAction() {
if (StringUtils.isEmpty(getSelectedItem())) {
return;
}
identity.checkPermission(instance, "update");
identity.checkPermission(getInstance(), "update");

HLocale locale = localeServiceImpl.getByLocaleId(getSelectedItem());

getInstance().getActiveLocales().add(locale);

getVersionGroupHome().update(conversationScopeMessages);
versionGroupHome.update(conversationScopeMessages);
reset();
maintainerFilter.reset();
versionGroupHome.getMaintainerFilter().reset();
conversationScopeMessages.setMessage(FacesMessage.SEVERITY_INFO,
msgs.format("jsf.LanguageAddedToGroup",
locale.retrieveDisplayName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.jpa.api.transaction.Transactional;
import org.zanata.security.annotations.Authenticated;
import org.zanata.security.annotations.CheckLoggedIn;
import org.zanata.common.EntityStatus;
Expand Down Expand Up @@ -155,6 +156,7 @@ public List<HPerson> getGroupMaintainers() {
}

@CheckLoggedIn
@Transactional
public void send() {
if (hasSelectedVersion()) {
String fromName = authenticatedAccount.getPerson().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map;
import java.util.Set;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.validation.ConstraintViolationException;

import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -106,7 +107,7 @@
import lombok.extern.slf4j.Slf4j;

@Named("versionHomeAction")
@javax.faces.bean.ViewScoped
@ViewScoped
@Slf4j
public class VersionHomeAction extends AbstractSortAction implements
Serializable {
Expand Down Expand Up @@ -1092,6 +1093,7 @@ public void clearTransFileUpload() {
translationFileUpload = new TranslationFileUploadHelper();
}

// TODO turn this into a CDI bean?
private class DocumentFilter extends InMemoryListFilter<HDocument> {
private DocumentDAO documentDAO =
ServiceLocator.instance().getInstance(DocumentDAO.class);
Expand All @@ -1108,6 +1110,7 @@ protected boolean include(HDocument elem, String filter) {
}
};

// TODO turn this into a CDI bean?
private class SourceDocumentFilter extends InMemoryListFilter<HDocument> {
private DocumentDAO documentDAO = ServiceLocator.instance()
.getInstance(DocumentDAO.class);
Expand Down

0 comments on commit bddaaec

Please sign in to comment.