001 /***************************************************************************************************
002 * MODULE DESCRIPTION
003 ****************************************************************************************************
004 *
005 * NAME: SpaceGroupSearch.java
006 * LANGUAGE: Java2
007 * DATE: 18.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 * 18.12.2002 Initial release
020 * 14.1.2003 /mn
021 * part of functionality generalized to SearchHadler
022 *
023 ****************************************************************************************************/
024 package kiurubeans;
025 import kotkabeans.*;
026 import javax.servlet.http.HttpServletRequest;
027
028 /**
029 * Implement space group searching, ordering & filtering
030 *
031 * @author Miika Nurminen
032 */
033 public class SpaceGroupSearch extends SearchHandler {
034 // Attributes
035
036 /**
037 * Search string. Value for SQL ordering
038 */
039 private String spaceGroupName = null;
040
041 // Constructors
042
043 /** Constructs a new instance of SpaceSearch. Defines ordeding. */
044 public SpaceGroupSearch() { // upper() cannot be applied to aliases
045 Field[] s = {new TextField("sg.name"),
046 new Field("spacegrouptypename")};
047 setVisibleFields(s); // spacegroupname
048 assignDefaultOrder();
049 }
050
051
052 // Access methods
053
054 /**
055 * Getter for attribute spaceGroupName
056 * @return user's query string
057 */
058 public String getSpaceGroupName() {
059 if (this.spaceGroupName==null) return "";
060 return this.spaceGroupName;
061 }
062
063 /** Setter for attribute spaceGroupName
064 * @param name new name for spacegroup
065 */
066 public void setSpaceGroupName(String name) {
067 this.spaceGroupName = KiuruString.sqlTrim(name);
068 }
069
070
071 /** Ensures empty request parameters are clearer when entity is posted.
072 * <p>
073 * If bean is used in a JSP, this should be called in the beginning of page.
074 * If form content is "" or null it is not sent via HTTP, so those fields
075 * must be cleared manually.
076 *
077 * @param request HTTP request with parameters
078 * @see SearchHandler#clearEmptyParameters(HttpServletRequest)
079 */
080 protected void doClearEmptyParameters(HttpServletRequest request) {
081 if ((request.getParameter("spaceGroupName")!=null) && (request.getParameter("spaceGroupName").equals("") && this.spaceGroupName!=null)) {
082 setSpaceGroupName("");
083 }
084 }
085
086 /** Default implementation of clearing action state. Descendant classes may
087 * override this.
088 * @see SearchHandler#doClearActionState(HttpServletRequest)
089 * @param request The request-object of the JSP.
090 */
091 protected void doClearActionState(javax.servlet.http.HttpServletRequest request) {
092 if ((getEnumState()==KiuruHandler.NO_ACTION) && (this.spaceGroupName!=null))
093 setEnumState(SUBMIT_SEARCH); // because of a bug in Mozilla when pressing enter in searchfield.
094 else super.doClearActionState(request);
095 }
096
097 /** Returns favorite spacegroupid of user with given id, null if not exists.
098 * @return RS2 field: spacegroupid
099 * @param userId user whose favorites we are searching
100 * @throws Exception if problems with db connection
101 */
102 public static RS2 getFavoriteSpaceGroup(int userId) throws java.lang.Exception {
103 RS2 rs = SimpleDb.simpleQuery("SpaceSearch.getFavoriteSpaces db",
104 "select sg.spacegroupid from spacegroup as sg,spacegroupperson as sgp "+
105 "where sg.spacegrouptypeid=6 "+ // 6==henkilökohtainen mielisaliryhmä
106 "and sg.spacegroupid = sgp.spacegroupid "+
107 "and sgp.modifyright<>0 "+
108 "and sg.deleted=false and sgp.deleted=false "+
109 "and sgp.personid ="+userId );
110 if (rs==null || rs.count()<1) return null;
111 return rs;
112 }
113
114 /**
115 * Implementation for SUBMIT_SEARCH
116 */
117 public void submitSearch() {
118 if (this.spaceGroupName==null) setSpaceGroupName("");
119 }
120
121 /**
122 * Implementation for RESET_SEARCH
123 */
124 public void resetSearch() {
125 this.spaceGroupName=null;
126 }
127
128
129 /** Returns all accessible spacegroups with code match
130 * User must be set!
131 * @throws Exception if problems with db connection
132 * @return (spacegroupid,spacegroupname,spacegrouptypename)
133 */
134 public RS2 getSpaceGroupsMatched() throws Exception {
135 if (this.spaceGroupName==null) return null; // getspacegroupname clears null away...
136 StringPair tables[]={
137 new StringPair("sg","spacegroup"),
138 new StringPair("sgt","spacegrouptype"),
139 new StringPair("sgp","spacegroupperson")
140 };
141
142 StringBuffer sb = new StringBuffer(200);
143 // removed distinct, replaced with group by /mn
144 sb.append("select sg.spacegroupid as spacegroupid, sg.name as spacegroupname, sgt.name as spacegrouptypename ");
145 sb.append(SimpleDb.addSqlFrom(tables));
146 sb.append("where sg.spacegrouptypeid = sgt.spacegrouptypeid and sgp.spacegroupid = sg.spacegroupid ");
147 sb.append(SimpleDb.formatSearchString("sg.name", getSpaceGroupName()));
148 sb.append(SimpleDb.addSqlDeleted(tables));
149 if (!getUser().hasAdminRight()) {
150 sb.append("and (sgp.personid="+getUser().getPersonID()+" or sgt.spacegrouptypeid<5)");
151 // spacegrouptypes 1..4 are public
152 }
153 sb.append(" group by sg.spacegroupid, sg.name, sgt.name "); // no distinct here
154 sb.append(getOrderClause());
155
156 return SimpleDb.simpleQuery("Getting matched spacegroups", sb.toString());
157 }
158
159 /** Returns all spacegroups.
160 * @throws Exception if problems with db connection
161 * @return (spacegroupid,spacegroupname,spacegrouptypename)
162 */
163 public RS2 getSpaceGroups() throws Exception {
164 return SimpleDb.simpleQuery("Getting all spacegroups",
165 "select sg.spacegroupid as spacegroupid, sg.name as spacegroupname, sgt.name as spacegrouptypename "+
166 "from spacegroup as sg, spacegrouptype as sgt "+
167 "where sg.spacegrouptypeid = sgt.spacegrouptypeid and sg.deleted=false "+
168 getOrderClause());
169 }
170
171 /** Returns all spacegroups where user has access rights. If user is
172 * Administrator returns all SpaceGroups.
173 *
174 * Note! User must be set for this method!
175 * @throws Exception if problems with db connection
176 * @return (spacegroupid,spacegroupname,spacegrouptypename)
177 */
178 public RS2 getSpaceGroupsWithAccessLevel() throws java.lang.Exception {
179 if(getUser().hasAdminRight())
180 return getSpaceGroups(); // all spacegroups for administrators
181
182 return SimpleDb.simpleQuery("Getting user's spacegroups",
183 "select sg.spacegroupid as spacegroupid, sg.name as spacegroupname, sgt.name as spacegrouptypename "+
184 "from spacegroup as sg, spacegrouptype as sgt, spacegroupperson as sgp "+
185 "where sg.spacegrouptypeid = sgt.spacegrouptypeid and sg.deleted=false "+
186 "and sgp.spacegroupid = sg.spacegroupid "+
187 "and sgp.deleted = false "+
188 "and sgp.personid="+getUser().getPersonID()+
189 getOrderClause());
190 }
191
192
193 /** Returns all public spacegroups in which space of given spaceid belongs.
194 * note! user must be set for this method.
195 * @return (spacegroupid,spacegroupname,spacegrouptypename)
196 * @param spaceid input space id
197 * @throws Exception if problems with db connection
198 */
199 public RS2 getSpaceGroupsOfSpace(int spaceid) throws java.lang.Exception {
200 StringPair[] sp = { // enumerating used tables
201 new StringPair("sg","spacegroup"),
202 new StringPair("sgt","spacegrouptype"),
203 new StringPair("sgs","spacegroupspace"),
204 new StringPair("sgp","spacegroupperson")
205 };
206 StringBuffer sb = new StringBuffer(200);
207 // removed distinct to simplify ordering /mn
208 sb.append("select sg.spacegroupid as spacegroupid, sg.name as spacegroupname, sgt.name as spacegrouptypename");
209 sb.append(SimpleDb.addSqlFrom(sp));
210 sb.append("where sg.spacegrouptypeid = sgt.spacegrouptypeid and sgs.spacegroupid = sg.spacegroupid and sgs.spaceid ="+KiuruString.sqlQuote(spaceid));
211 sb.append(" and sgp.spacegroupid = sg.spacegroupid ");
212 sb.append(SimpleDb.addSqlDeleted(sp));
213 if(!getUser().hasAdminRight()) {
214 // spacegrouptypes 1..4 are public
215 sb.append("and (sgp.personid="+getUser().getPersonID()+" or sgt.spacegrouptypeid<5)");
216 }
217 sb.append(" group by sg.spacegroupid, sg.name, sgt.name "); // no distinct here
218 sb.append(getOrderClause());
219 return SimpleDb.simpleQuery("Getting space's spacegroup", sb.toString());
220 }
221
222 }
223 /***************************************************************************************************
224 * COPYRIGHT (C) KIURU-PROJECT GROUP
225 * Limited rights granted. Please refer to license
226 ****************************************************************************************************/