Subversion Repositories bacoAlunos

Rev

Rev 457 | Rev 1306 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
90 jmachado 1
package pt.estgp.estgweb.services.announcements;
2
 
3
import pt.utl.ist.berserk.logic.serviceManager.IService;
163 jmachado 4
import pt.estgp.estgweb.domain.*;
90 jmachado 5
import pt.estgp.estgweb.domain.dao.DaoFactory;
6
import pt.estgp.estgweb.services.common.*;
7
import pt.estgp.estgweb.services.common.impl.DefaultSearchResults;
8
import pt.estgp.estgweb.services.common.impl.DefaultResult;
156 jmachado 9
import pt.estgp.estgweb.services.common.impl.DefaultToDo;
90 jmachado 10
import pt.estgp.estgweb.Globals;
11
import org.apache.log4j.Logger;
12
 
13
import java.util.List;
14
import java.util.ArrayList;
15
 
16
import jomm.dao.impl.AbstractDao;
17
 
18
/**
19
 * @author Jorge Machado
135 jmachado 20
 * @date 25/Apr/2008
21
 * @time 18:01:54
22
 * @see pt.estgp.estgweb.web
90 jmachado 23
 */
457 jmachado 24
public class AnnouncementsCommonServices extends SingleSearchResults implements IService, IModuleCommonServices
135 jmachado 25
{
26
 
90 jmachado 27
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(AnnouncementsCommonServices.class);
28
 
135 jmachado 29
    public int countSearch(1.5.0/docs/api/java/lang/String.html">String search, SearchTypeEnum searchType, UserSession userSession)
30
    {
31
        return 0;  // todo To change body of implemented methods use File | Settings | File Templates.
90 jmachado 32
    }
33
 
135 jmachado 34
    public ISearchResults search(1.5.0/docs/api/java/lang/String.html">String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession)
35
    {
165 jmachado 36
        return search(null,search, searchType,page,maxResults,userSession);
37
    }
38
 
39
    public ISearchResults search(1.5.0/docs/api/java/lang/String.html">String from, 1.5.0/docs/api/java/lang/String.html">String search, SearchTypeEnum searchType, int page, int maxResults, UserSession userSession)
40
    {
90 jmachado 41
        DefaultSearchResults searchResults = new DefaultSearchResults();
42
        searchResults.setModule(ModuleEnum.Announcements);
43
        List<String> roles;
135 jmachado 44
        if (userSession == null || userSession.getUser() == null || ((UserImpl) userSession.getUser()).getRolesList() == null || ((UserImpl) userSession.getUser()).getRolesList().size() == 0)
45
        {
90 jmachado 46
            roles = new ArrayList<String>();
47
            roles.add(Globals.ALL_ROLE);
48
        }
49
        else
92 jmachado 50
            roles = ((UserImpl) userSession.getUser()).getRolesList();
90 jmachado 51
 
156 jmachado 52
        boolean internal = true;
140 fvelez 53
        if (userSession.getUser() == null)
54
            internal = false;
55
        searchResults.setTotalResults(DaoFactory.getAnnouncementDaoImpl().countAnnouncements(search, searchType, roles, null,internal));
135 jmachado 56
        if (searchResults.getTotalResults() > 0)
57
        {
140 fvelez 58
            List<Announcement> announcements = DaoFactory.getAnnouncementDaoImpl().searchAnnouncements(search, searchType, maxResults, page, roles, null,internal);
92 jmachado 59
            if (announcements == null)
60
                return searchResults;
135 jmachado 61
            for (Announcement a : announcements)
62
            {
92 jmachado 63
                DefaultResult defaultResult = new DefaultResult();
64
                defaultResult.setTitle(a.getTitle());
65
                defaultResult.setSubTitle("announcements.type." + a.getType());
470 jmachado 66
                defaultResult.setDate(new java.text.1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat("yyyy.MM.dd").format(a.getUpdateDate()));
67
                if(a.getOwner() != null)
68
                    defaultResult.setCreator(a.getOwner().getName());
92 jmachado 69
                defaultResult.setSubTitleKey(true);
70
                defaultResult.setText(a.getText());
165 jmachado 71
                if(from == null)
205 jmachado 72
                    defaultResult.setUrl("/startLoadAnnouncement.do?id=" + a.getId());
165 jmachado 73
                else
205 jmachado 74
                    defaultResult.setUrl("/user/startLoadAnnouncementFrom" + from + ".do?id=" + a.getId() + "&dispatch=load");
92 jmachado 75
                searchResults.addResult(defaultResult);
76
            }
90 jmachado 77
        }
78
        return searchResults;
79
    }
80
 
457 jmachado 81
 
82
 
135 jmachado 83
    public int countToDo(UserSession userSession)
84
    {
85
        return 0;  //todo To change body of implemented methods use File | Settings | File Templates.
90 jmachado 86
    }
87
 
156 jmachado 88
    /**
89
     * Not implemented
90
     * @param userSession
91
     * @return
92
     */
135 jmachado 93
    public IToDoCat getToDo(UserSession userSession)
94
    {
156 jmachado 95
        return null;
90 jmachado 96
    }
97
 
156 jmachado 98
    public List<IToDo> getAllToDos(UserSession userSession)
99
    {
100
        List<IToDo> todos = new ArrayList<IToDo>();
101
        if(userSession.getUser().hasRole(Globals.ANNOUNCEMENTS_VALIDATOR_ROLES))
102
        {
103
            List<Announcement> invalidAnnouncements = DaoFactory.getAnnouncementDaoImpl().loadInvalidAnnouncements();
104
            for(Announcement a: invalidAnnouncements)
105
            {
205 jmachado 106
                DefaultToDo defaultToDo = new DefaultToDo(a.getTitle(),a.getTargetRoles(),((UserImpl)a.getOwner()).getUsername(), "announcements.type." + a.getType(),"/user/startEditAnnouncementFromHome.do?id=" + a.getId());
156 jmachado 107
                todos.add(defaultToDo);
108
            }
109
        }
110
        return todos;
111
    }
112
 
163 jmachado 113
    /*This method is invoked from object creation*/
114
    public void adviseNew(DomainObject object)
115
    {
116
        //To change body of implemented methods use File | Settings | File Templates.
117
    }
118
 
119
    public void adviseUpdate(DomainObject object)
120
    {
121
        //To change body of implemented methods use File | Settings | File Templates.
122
    }
123
 
135 jmachado 124
    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args)
125
    {
90 jmachado 126
        AbstractDao.getCurrentSession().beginTransaction();
127
        UserSession sess = new UserSessionImpl();
128
        sess.setUser(new UserImpl());
129
        sess.getUser().setRoles("all");
92 jmachado 130
        ISearchResults searchResults = new AnnouncementsCommonServices().search("teste", SearchTypeEnum.AllWords, 0, 10, sess);
135 jmachado 131
        for (IResult result : searchResults.getResults())
132
        {
92 jmachado 133
            1.5.0/docs/api/java/lang/System.html">System.out.println("title:" + result.getTitle());
134
            1.5.0/docs/api/java/lang/System.html">System.out.println("subTitle:" + result.getSubTitle());
135
            1.5.0/docs/api/java/lang/System.html">System.out.println("text:" + result.getText());
90 jmachado 136
            1.5.0/docs/api/java/lang/System.html">System.out.println("url" + result.getUrl());
137
            1.5.0/docs/api/java/lang/System.html">System.out.println("bestFragments" + result.getBestFragments());
138
        }
139
        AbstractDao.commit();
140
    }
141
}