001    /***************************************************************************************************
002     *               MODULE DESCRIPTION
003     ****************************************************************************************************
004     *
005     *               NAME:           PersonSearch.java
006     *               LANGUAGE:       Java2
007     *               DATE:           9.12.2002
008     *               AUTHOR:         Miika Nurminen, Jyväskylän yliopisto
009     *
010     ****************************************************************************************************
011     *               COPYRIGHT (C) KIURU-PROJEKTIRYHMÄ
012     *               Limited rights granted. Please refer to license
013     ****************************************************************************************************
014     *  
015     ****************************************************************************************************
016     *               UPDATES
017     ****************************************************************************************************
018     *
019     *   5.12.2002 Initial release
020     *   14.1.2003 /mn
021     *    part of functionality generalized to SearchHadler
022     *
023     ****************************************************************************************************/
024    package kiurubeans;
025    
026    import kotkabeans.*;
027    import kiurubeans.*;
028    
029    /**
030     * Used for searches on the person table.
031     */
032    public class PersonSearch extends SearchHandler {
033    
034    // Attributes
035    
036      /**
037       * Search string. Value for SQL ordering
038       */
039      private String name; 
040      
041    // Constructors
042    
043      /** Constructs a new instance of PersonSearch. Defines ordeding. */
044      public PersonSearch() {
045        Field[] s = {new TextField("p.lastname"),
046                     new TextField("p.firstnames"),
047                     new TextField("ot.name")};
048        setVisibleFields(s);                                                    // organisationname
049        assignDefaultOrder();
050      }
051    
052      
053     
054    // Access methods
055    
056      /**
057       * Getter for attribute name
058       * @return person name used in query string
059       */  
060      public String getPersonName() {
061        if (name==null) return "";
062        return this.name;
063      }
064    
065      /**
066       * Setter for attribute name
067       * @param name person name used in query string
068       */  
069      public void setPersonName(String name) {
070        this.name = kotkabeans.Encoder.SQLEncode(name).trim();
071      }
072    
073      
074    // Operations
075    
076      /**
077       * Implementation for SUBMIT_SEARCH
078       */
079       public void submitSearch() {
080         if (this.name==null) setPersonName("");
081       }
082       
083      /**
084       * Implementation for RESET_SEARCH
085       */
086       public void resetSearch() {
087         this.name=null;
088       }
089      
090       /**
091        * Returns person information for given spacegroup.
092        * 
093        * @param spacegroupid spacegroup to be searched
094        * @throws Exception if problems with db connection
095        * @return (personid,accesslevel,modifyright)
096        */   
097      public static RS2 getPersonIdsFromSpaceGroup(int spacegroupid) throws java.lang.Exception {
098        return SimpleDb.simpleQuery("Personsearch getReserverPersonsFromSpaceGroup",
099          "select sgp.personid, sgp.accesslevel, sgp.modifyright "+
100          "from person as p, spacegroupperson as sgp "+
101          "where p.deleted=false and sgp.deleted=false and sgp.personid=p.personid "+
102          "and sgp.spacegroupid="+Integer.toString(spacegroupid));
103      }
104      
105       /**
106        * Returns person information for given spacegroup.
107        * 
108        * @param spacegroupid spacegroup to be searched
109        * @throws Exception if problems with db connection
110        * @return (personid,personname,personfirstname,organisationname,accesslevel,modifyright)
111        */   
112      public RS2 getPersonsFromSpaceGroup(int spacegroupid) throws java.lang.Exception {
113        return SimpleDb.simpleQuery("Personsearch getReserverPersonsFromSpaceGroup",
114          "select p.personid, p.lastname as personname,p.firstnames as personfirstname, ot.name as organisationname, sgp.accesslevel, sgp.modifyright "+
115          "from person as p, spacegroupperson as sgp, organisationtranslation as ot "+
116          "where p.deleted=false and sgp.deleted=false and sgp.personid=p.personid "+
117          "and ot.organisationid=p.homeorganisationid and sgp.spacegroupid="+Integer.toString(spacegroupid)+
118          " and ot.languageid="+getLangId()+
119          getOrderClause());
120      }
121    
122      
123      /**  Returns all persons with code match. Note! This might be called automatically
124       * within performaction. Currently must be called manually.
125       * @throws Exception if problems with db connection
126       * @return matched spaces (code,spaceid,spacetypename)
127       */
128      public RS2 getPersons() throws Exception {
129        if (this.name==null) return null; // getpersonname clears null away...
130        String t=SimpleDb.formatSearchString("p.lastname", getPersonName());
131        return SimpleDb.simpleQuery("PersonSearch getPersons","select p.personid, p.lastname as personName,p.firstnames as personfirstname, ot.name as organisationName "+
132          "from person as p, organisationtranslation as ot "+
133          "where p.deleted=false and ot.organisationid=p.homeorganisationid "+
134          "and ot.languageid="+getLangId()+" "+t+
135          getOrderClause());
136      }
137      
138      /** Default implementation of clearing action state. Descendant classes may
139       * override this.
140       * @see SearchHandler#doClearActionState(HttpServletRequest)
141       * @param request The request-object of the JSP.
142       */
143      protected void doClearActionState(javax.servlet.http.HttpServletRequest request) {
144        if ((getEnumState()==KiuruHandler.NO_ACTION) && (this.name!=null)) 
145          setEnumState(SUBMIT_SEARCH); // because of a bug in Mozilla when pressing enter in searchfield.
146        else super.doClearActionState(request);
147      }
148      
149      /** Ensures empty request parameters are cleared when entity is posted.
150       * <p>
151       * If bean is used in a JSP, this should be called in the beginning of page.
152       * If form content is "" or null it is not sent via HTTP, so those fields
153       * must be cleared manually.
154       *
155       * @param request HTTP request with parameters
156       * @see SearchHandler#clearEmptyParameters(HttpServletRequest)
157       */
158      protected void doClearEmptyParameters(javax.servlet.http.HttpServletRequest request) {
159        if ((request.getParameter("personName")!=null) && (request.getParameter("personName").equals("") && this.name!=null)) {
160          setPersonName("");
161        }
162      }
163      
164    }
165    /***************************************************************************************************
166     *               COPYRIGHT (C) KIURU-PROJEKTIRYHMÄ
167     *               Limited rights granted. Please refer to license
168     ****************************************************************************************************/