View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.tasksJsf;
18  
19  import javax.enterprise.context.RequestScoped;
20  import javax.enterprise.inject.Instance;
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  /**
25   * <p>
26   * Basic operations for tasks owned by current user - additions, deletions/
27   * </p>
28   *
29   * @author Lukas Fryc
30   *
31   */
32  @RequestScoped
33  @Named
34  public class TaskController {
35  
36      @Inject
37      private TaskDao taskDao;
38  
39      @Inject
40      private TaskList taskList;
41  
42      /**
43       * Injects current user, which is provided by {@link AuthController}.
44       */
45      @Inject
46      @CurrentUser
47      private Instance<User> currentUser;
48  
49      /**
50       * Injects current user stored in the conversation scope
51       */
52      @Inject
53      private CurrentTaskStore currentTaskStore;
54  
55      /**
56       * Set the current task to the context
57       *
58       * @param task current task to be set to context
59       */
60      public void setCurrentTask(Task task) {
61          currentTaskStore.set(task);
62      }
63  
64      /**
65       * Creates new task and, if no task is selected as current, selects it.
66       *
67       * @param taskTitle
68       */
69      public void createTask(String taskTitle) {
70          taskList.invalidate();
71          Task task = new Task(taskTitle);
72          taskDao.createTask(currentUser.get(), task);
73          if (currentTaskStore.get() == null) {
74              currentTaskStore.set(task);
75          }
76      }
77  
78      /**
79       * Deletes given task
80       *
81       * @param task to delete
82       */
83      public void deleteTask(Task task) {
84          taskList.invalidate();
85          if (task.equals(currentTaskStore.get())) {
86              currentTaskStore.unset();
87          }
88          taskDao.deleteTask(task);
89      }
90  
91      /**
92       * Deletes given task
93       *
94       * @param task to delete
95       */
96      public void deleteCurrentTask() {
97          deleteTask(currentTaskStore.get());
98      }
99  }