001: package ru.emdev.EmForge.web.bean;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005:
006: import javax.faces.model.SelectItem;
007:
008: import org.apache.commons.lang.ObjectUtils;
009: import org.emforge.projectmanager.ProjectService;
010: import org.emforge.projectmanager.base.ProjectDO;
011: import org.emforge.report.ReportView;
012: import org.emforge.xfer.EmForgeObject;
013: import org.emforge.xfer.PriorityTO;
014:
015: import ru.emdev.EmForge.security.EmForgeUserDetails;
016: import ru.emdev.EmForge.security.UserFactory;
017: import ru.emdev.EmForge.security.dao.Role;
018: import ru.emdev.EmForge.security.web.SiteRole;
019:
020: /**
021: *
022: */
023: public class GlobalSelectItems {
024:
025: private UserFactory m_userFactory;
026: private ProjectService m_projectService;
027:
028: /**
029: * Sets a reference to user factory bean to manipulate users. Called by Spring Framework.
030: *
031: * @param i_userFactory is a user factory bean to set
032: */
033: public void setUserFactory(UserFactory i_userFactory) {
034:
035: m_userFactory = i_userFactory;
036: }
037:
038: /**
039: * Sets a reference to project bean to manipulate projects. Called by Spring Framework.
040: *
041: * @param i_projectService is a project bean to set
042: */
043: public void setProjectService(ProjectService i_projectService) {
044:
045: m_projectService = i_projectService;
046: }
047:
048: /**
049: * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
050: */
051: public void afterPropertiesSet() throws Exception {
052:
053: if (m_userFactory == null) {
054: throw new IllegalArgumentException(
055: "userFactory should be specified for "
056: + this .getClass());
057: }
058: if (m_projectService == null) {
059: throw new IllegalArgumentException(
060: "projectService should be specified for "
061: + this .getClass());
062: }
063: }
064:
065: /** Priorities */
066: protected Collection<SelectItem> m_priorities;
067:
068: /**
069: * Gets List of Priorities
070: */
071: public Collection<SelectItem> getPriorities() {
072:
073: if (m_priorities == null) {
074: m_priorities = new ArrayList<SelectItem>();
075:
076: for (PriorityTO priority : PriorityTO.values()) {
077: m_priorities.add(new SelectItem(priority, priority
078: .getName()));
079: }
080: }
081:
082: return m_priorities;
083: }
084:
085: /**
086: * Get list of projects. Since list of project may be changed we calculates it every time
087: *
088: * @todo move empty select item out
089: */
090: public Collection<SelectItem> getProjects() {
091:
092: Collection<SelectItem> result = new ArrayList<SelectItem>();
093:
094: Collection<ProjectDO> projects = m_projectService
095: .getAllProjects();
096: for (ProjectDO project : projects) {
097: String displayName = ObjectUtils.toString(project
098: .getDisplayName());
099: result.add(new SelectItem(project, displayName));
100: }
101:
102: return result;
103: }
104:
105: /**
106: * Retrieves milestones of specified project
107: *
108: * @param i_projectWikiName is a wiki page name of required project
109: * @return Collection of milestones of specified project or empty collection if required objects are absent
110: */
111: public Collection<SelectItem> getMilestones(String i_projectWikiName) {
112:
113: ProjectDO project = m_projectService
114: .getProject(i_projectWikiName);
115: return getSelectItems(m_projectService.getMilestones(project));
116: }
117:
118: /**
119: * Get list of users
120: *
121: * @todo Move out from here new SelectItem("", "")
122: * @depricated use getUsers and converter instead
123: * @return
124: * @throws Exception
125: */
126: public Collection<SelectItem> getUserNames() throws Exception {
127:
128: Collection<SelectItem> result = new ArrayList<SelectItem>();
129:
130: Collection<EmForgeUserDetails> users = m_userFactory.getUsers();
131: for (EmForgeUserDetails user : users) {
132: result.add(new SelectItem(user.getUsername(), user
133: .getDisplayName()));
134: }
135:
136: return result;
137: }
138:
139: /**
140: * @return
141: * @throws Exception
142: */
143: public Collection<SelectItem> getUsers() {
144:
145: Collection<SelectItem> result = new ArrayList<SelectItem>();
146:
147: Collection<EmForgeUserDetails> users = m_userFactory.getUsers();
148:
149: for (EmForgeUserDetails user : users) {
150: result.add(new SelectItem(user, user.getDisplayName()));
151: }
152:
153: return result;
154: }
155:
156: /**
157: * Retrieves users of specified project
158: *
159: * @param i_projectWikiName is a wiki page name of required project
160: * @return Collection of users of specified project or empty collection if required objects are absent
161: */
162: public Collection<SelectItem> getProjectUsers(
163: String i_projectWikiName) {
164:
165: ProjectDO project = m_projectService
166: .getProject(i_projectWikiName);
167: return getSelectItemsForUsers(m_projectService
168: .getAllProjectUsers(project));
169: }
170:
171: /**
172: * Retrieves users of specified project and role
173: *
174: * @param i_projectWikiName is a wiki page name of required project
175: * @param i_roleName is a project role
176: * @return Collection of users of specified project and role or empty collection if required objects are absent
177: */
178: public Collection<SelectItem> getProjectUsers(
179: String i_projectWikiName, String i_roleName) {
180:
181: ProjectDO project = m_projectService
182: .getProject(i_projectWikiName);
183: return getSelectItemsForUsers(m_projectService.getUsers(
184: project, i_roleName));
185: }
186:
187: /**
188: * Requires users with specified site role
189: *
190: * @param i_siteRoleName is a site role name
191: * @return Collection of users with specified site role
192: */
193: public Collection<SelectItem> getSiteRoleUsers(String i_siteRoleName) {
194:
195: Collection<EmForgeUserDetails> users = null;
196: if (SiteRole.isSiteRole(i_siteRoleName)) {
197: users = m_userFactory.getUsers(i_siteRoleName);
198: }
199: return getSelectItemsForUsers(users);
200: }
201:
202: /**
203: * Requires all existing roles
204: *
205: * @return Collection of roles
206: */
207: public Collection<SelectItem> getRoles() {
208:
209: return getSelectItems(m_userFactory.getRoles());
210: }
211:
212: /**
213: * Requires all existing site roles
214: *
215: * @return Collection of site roles
216: */
217: public Collection<SelectItem> getSiteRoles() {
218:
219: Collection<Role> result = new ArrayList<Role>();
220:
221: for (Role role : m_userFactory.getRoles()) {
222: if (SiteRole.isSiteRole(role.getName())) {
223: result.add(role);
224: }
225: }
226: return getSelectItems(result);
227: }
228:
229: /**
230: * @return
231: * @throws Exception
232: */
233: public Collection<SelectItem> getReportFormats() throws Exception {
234:
235: return ReportView.getSelectItems();
236: }
237:
238: /**
239: * @param <E> is any instance of EmForgeObject
240: * @param i_objects is a collection of EmForge objects
241: * @return Collection of SelectItems based on specified EmForge objects
242: */
243: protected <E extends EmForgeObject> Collection<SelectItem> getSelectItems(
244: Collection<E> i_objects) {
245:
246: Collection<SelectItem> result = new ArrayList<SelectItem>();
247:
248: if (i_objects != null) {
249: for (E obj : i_objects) {
250: result.add(new SelectItem(obj, obj.getName()));
251: }
252: }
253:
254: return result;
255: }
256:
257: /**
258: * @param <E> is an instance of EmForgeUserDetails
259: * @param i_objects is a collection of EmForge users
260: * @return Collection of SelectItems based on specified EmForge users
261: * @todo it's a workaround to get collection of SelectItems for EmForge users. EmForgeUserDetails should expand
262: * EmForgeObject!
263: */
264: protected <E extends EmForgeUserDetails> Collection<SelectItem> getSelectItemsForUsers(
265: Collection<E> i_objects) {
266:
267: Collection<SelectItem> result = new ArrayList<SelectItem>();
268:
269: if (i_objects != null) {
270: for (E obj : i_objects) {
271: result.add(new SelectItem(obj, obj.getDisplayName()));
272: }
273: }
274:
275: return result;
276: }
277: }
|