Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to implement custom permissions?

Status
Not open for further replies.

RicardoPereira

Programmer
Jun 3, 2003
255
PT
Hi,

Anyone could tell me what is the best way to implement a custom permission form?
For each user i want to add or remove access to some fields in a form and in the text boxes each user has a default text witch should be changed by the administrator.
I am using an sql server database with an table where i put all the required parameters for each user (about 32 fields), than when a user logs in, all the 32 parameters are loaded and used in the jsp with if's.
I know that this is not the best idea to implement this custom form but is the only idea i had. Anyone has an better suggestion?

Thanks
Ricardo
 
Hi,

I have couple of question before we can implement the above soultion.

1.What kind of Input fileds do you need to have on the JSP page eg: text,hidden,select etc., ?

2.Is the layout should be same for all the users ? I mean the order of diplay of the text fields.

Cheers,
Venu


 
I have text,hidden and select input fields.
The layout should be the same for all users but some users could't have permission to write in some fields.

What i need is an website where the clientes send a form with the new articles. My articles table have 32 fields but they aren't all required for all clients and some fields have different values for each cliente.

 
Hi,

It is just an idea of showing the input fields insted of
using so many If's and Else conditions for every input field.

Define methods for different types of input fields which
returns you a HTML Input string. And add the string to an ArrayList with an Index. We need Index, so that we can display the Input fields in same oreder.

Once you add all the Input String the ArrayList you can iterate it in the JSP page to display the required Fields.

public class Criteria
{

/** The name for this hidden field, such as userID */
String name = null;

/** The value of this hidden field, such as Administrator */
String value = null;

String htmlString = null;


/** Return the HTML input String on the Presentaion Layer */
String inputString = null;

String hiddenString = null;

public Criteria(String name, String value) {
this.name = name;
this.value = value;

}

/**
* Returns the name.
* @return
*/
public String getName() {
return name;
}

/**
* Returns the value
* @return the value
*/
public String getValue() {
return value;
}

public String getInputString() {
htmlString = &quot;<input type='text' name='&quot;+ getName()+&quot;' value='&quot;+getValue()+&quot;'>&quot;;
return htmlString;
}
public String getHiddenString() {
htmlString = &quot;<input type='hidden' name='&quot;+ getName()+&quot;' value='&quot;+getValue()+&quot;'>&quot;;
return htmlString;
}


}


And then Add the String to ArrayList.

ArraryList list = new ArrayList();
list.add(1, new HiddenCriteria(name,value).getInputString());
list.add(2, new HiddenCriteria(name,value).getHiddenString());

In the JSP Page you can get the List and diplay the fields.

It is little difficult to explain. Its little long process, Hope this helps you.


Cheers,
Venu
 
It could be a good ideia.
But i have a doubt. Now, i'am using a class file where i put all the database connections strings and request them using the beans. With your suggestion what should i have to put in my database connection class file and in my jsp?

Thanks for your help

Ricardo
 
Hi,

You dont have to change any thing in the way you are connection to the DB. Make the Java Bean little more intelligent insted of returning you the value make it return the HTML string.

Or

Write a Java Method which takes the JavaBean and returns you the ArrayList with all the Input values in it.


I would preffer 2nd one as you will have the arrayList and just iterate it over to diplay the input fields.

Cheers,
Venu
 
could you please give me an pratical example so i can understand how it works?
 
Hi,

Sorry for the late response. All you need to do in the JSP is need to call the function dbConneciton(ResultSet rs); You can make that function much better this is just for Example.

In JSP page once you have the ArrayList you can display the String by using out.println() statement so that it will display you the Input values.

Hope this will help you.

import org.apache.struts.util.LabelValueBean;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
*
* User: venu reddy
* Date: Jan 8, 2004
* Time: 11:45:59 AM
* Java: ${filename}
*/
public class TekBean {

private static final int HIDDEN = 1;
private static final int TEXT = 2;
private static final int SELECT = 3;
private ArrayList arrayList = new ArrayList();

public TekBean() {

}

public void addToList(int type, String name, String value, List list) {
StringBuffer buffer = new StringBuffer();
if (type == HIDDEN)
{
buffer.append(&quot;<input type=\&quot;hidden\&quot; name=\&quot;&quot; + name + &quot;\&quot; value=\&quot;&quot;+ value +&quot;\&quot;>&quot;);
}else if(type == TEXT)
{
buffer.append(&quot;<input type=\&quot;text\&quot; name=\&quot;&quot; + name + &quot;\&quot; value=\&quot;&quot;+ value +&quot;\&quot;>&quot;);
}else if (type == SELECT)
{

buffer.append(&quot;<select name=\&quot;&quot; + name + &quot;\&quot;>&quot;);
LabelValueBean labelValueBean = null;
String option = &quot;&quot;;
if (null != list) {
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
labelValueBean = (LabelValueBean) iterator.next();
if(value.equals(labelValueBean.getValue()))
option = &quot;selected&quot;;
else
option = &quot;&quot;;
buffer.append(&quot;<option value=\&quot;&quot; + labelValueBean.getValue() + &quot;\&quot; &quot;+ option+&quot;>&quot; + labelValueBean.getLabel() + &quot;</option>&quot;);
}
buffer.append(&quot;</select>&quot;);
}
}
arrayList.add(buffer.toString());
}

public ArrayList getArrayList() {
return arrayList;
}

/**
*
* @param rs
* @return
* @throws SQLException
*/

public ArrayList dbConneciton(ResultSet rs) throws SQLException {
TekBean tekBean = new TekBean();
/**
* if you have the resultSet you are going to add the values in the while loop
*/
/* while(rs.next())
{
tekBean.addToList(rs.getInt(1), rs.getString(2), rs.getString(3), null);
}*/

tekBean.addToList(1,&quot;text1&quot;,&quot;textValue&quot;,null);
tekBean.addToList(2,&quot;hidden1&quot;,&quot;hiddenValue&quot;, null);
List list = new ArrayList();
list.add(new LabelValueBean(&quot;one&quot;,&quot;1&quot;));
list.add(new LabelValueBean(&quot;two&quot;,&quot;2&quot;));
list.add(new LabelValueBean(&quot;three&quot;,&quot;3&quot;));
tekBean.addToList(3,&quot;select1&quot;,&quot;2&quot;, list);

return tekBean.getArrayList();
}

public static void main(String[] args) {
// below code need to be in the JSP page
TekBean tekBean = new TekBean();
ArrayList arrayList = null;
try {
arrayList = tekBean.dbConneciton(null);
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
Iterator iterator = arrayList.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
}

}

OUTPUT :

<input type=&quot;hidden&quot; name=&quot;text1&quot; value=&quot;textValue&quot;>
<input type=&quot;text&quot; name=&quot;hidden1&quot; value=&quot;hiddenValue&quot;>
<select name=&quot;select1&quot;><option value=&quot;1&quot; >one</option><option value=&quot;2&quot; selected>two</option><option value=&quot;3&quot; >three</option></select>

Cheers,
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top