开发者

How to determine the locale within alfresco

I have a java class represents the list of value and i need to enforce the locale within it.

How can i determine the locale ?

The following class displays Constraint value/label for a content model.

package com.ds.ui.constraints;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;  
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.apache.log4j.Logger;
import com.ds.ui.bean.dialog.util.BeanUtil;

public class PositionPermissionsConstraints extends ListOfValuesConstraint
    implements Serializable {

public void initialize(){
    LOG.debug("PositionPermissionsConstraints Initializer");
    super.setCaseSensitive(false);
    preparePermissionList();
}

public void preparePermissionList(){
    ResourceBundle bundle = null;   
    if(FacesContext.getCurrentInstance() != null){
        if(FacesContext.getCurrentInstance().getExternalContext().getRequestLocale() != null)
        bundle = ResourceBundle.getBundle(BeanUtil.RESOURECE_BUNDLE,
                FacesContext.getCurrentInstance().getExternalContext().getRequestLocale());
    }
    else
        bundle = ResourceBundle.getBundle(BeanUtil.RESOURECE_BUNDLE);

    List<String> av = new ArrayList<String>();
    av
    .add(PositionPermissionsConstraints.
            CREATE_ORGANIZATION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(CREATE_ORGANIZATION_PERMISSION));
    av
        .add(PositionPermissionsConstraints.
                UPDATE_ORGANIZATION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(UPDATE_ORGANIZATION_PERMISSION));         
    av
        .add(PositionPermissionsConstraints.
                DELETE_ORGANIZATION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(DELETE_ORGANIZATION_PERMISSION));         
    av
        .add(PositionPermissionsConstraints.
                CREATE_ROOT_POSITION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(CREATE_ROOT_POSITION_PERMISSION));            
    av
        .add(PositionPermissionsConstraints.
                MANAGE_ROOT_POSITION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(MANAGE_ROOT_POSITION_PERMISSION));
    av
    .add(PositionPermissionsConstraints.
            CREATE_SUBORDINATE_POSITION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(CREATE_SUBORDINATE_POSITION_PERMISSION));         
开发者_运维知识库    av
        .add(PositionPermissionsConstraints.
                UPDATE_SUBORDINATE_POSITION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(UPDATE_SUBORDINATE_POSITION_PERMISSION));         
    av
        .add(PositionPermissionsConstraints.
                DELETE_SUBORDINATE_POSITION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(DELETE_SUBORDINATE_POSITION_PERMISSION));         
    av
        .add(PositionPermissionsConstraints.
                MANAGE_POSITION_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(MANAGE_POSITION_PERMISSION));         
    av
        .add(PositionPermissionsConstraints.
                ASSIGN_POSITION_TO_USER_PERMISSION);
    this.allowedLabels
    .add(bundle.getString(ASSIGN_POSITION_TO_USER_PERMISSION));
    this.setAllowedValues(av);
    this.setAllowedLabels(allowedLabels);   
}

public List<SelectItem> prepareSelectItemsList(){
    ResourceBundle bundle = ResourceBundle.getBundle(BeanUtil.RESOURECE_BUNDLE);
    List<SelectItem> selectItems = new ArrayList<SelectItem>();
    for(String value : getAllowedValues()){
        SelectItem selectItem = new SelectItem(value,bundle.getString(value));
        selectItems.add(selectItem);
    }
    return selectItems;
}

private static final String CREATE_ORGANIZATION_PERMISSION = "CREATE_ORGANIZATION";
private static final String UPDATE_ORGANIZATION_PERMISSION = "UPDATE_ORGANIZATION";
private static final String DELETE_ORGANIZATION_PERMISSION = "DELETE_ORGANIZATION";
private static final String CREATE_ROOT_POSITION_PERMISSION = "CREATE_ROOT_POSITION";
private static final String MANAGE_ROOT_POSITION_PERMISSION = "MANAGE_ROOT_POSITION";
private static final String CREATE_SUBORDINATE_POSITION_PERMISSION = "CREATE_SUBORDINATE_POSITION";
private static final String UPDATE_SUBORDINATE_POSITION_PERMISSION = "UPDATE_SUBORDINATE_POSITION";
private static final String DELETE_SUBORDINATE_POSITION_PERMISSION = "DELETE_SUBORDINATE_POSITION";
private static final String MANAGE_POSITION_PERMISSION = "MANAGE_POSITION";
private static final String ASSIGN_POSITION_TO_USER_PERMISSION = "ASSIGN_POSITION_TO_USER";

private List<String> allowedLabels = new ArrayList<String>();
private static final long serialVersionUID = 1L;
private static Logger LOG = Logger.getLogger(PositionPermissionsConstraints.class);

public List<String> getAllowedLabels() {
    return allowedLabels;
}
public void setAllowedLabels(List<String> allowedLabels) {
    this.allowedLabels = allowedLabels;
}

}

Thanks


If you're running in a WebScript, then I18NUtil is what you want. The method public static Locale getLocale() on it will return the locale for the currently running webscript

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜