LINQ to SQL Table Dependency - asp.net-mvc

If I have two tables... Category and Pet.
Is there a way in LINQ to SQL to make the result of the joined query map to a another strongly typed class (such as: PetWithCategoryName) so that I can strongly pass it to a MVC View?
I currently have Category and Pet classes... should I make another one?
Maybe I missing something here. Can any of you enlighten me?
from p in petTable
join c in categoryTable on p.CategoryId equals c.Id
where (c.Id == categoryId.Value)
select new
{
p.Id,
p.Name,
p.Description,
p.Price,
CategoryName = c.Name
}
<?xml version="1.0" encoding="utf-8" ?>
<Database Name="PetShop" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
<Table Name="Category" Member="PetShop.Models.Category">
<Type Name="PetShop.Models.Category">
<Column Name="Id" Member="Id" IsDbGenerated="true" IsPrimaryKey="true" />
<Column Name="Name" Member="Name" />
<Column Name="Description" Member="Description" />
</Type>
</Table>
<Table Name="Pet" Member="PetShop.Models.Pet">
<Type Name="PetShop.Models.Pet">
<Column Name="Id" Member="Id" IsDbGenerated="true" IsPrimaryKey="true" />
<Column Name="Name" Member="Name" />
<Column Name="Description" Member="Description" />
<Column Name="ImageUrl" Member="ImageUrl" />
<Column Name="Price" Member="Price" />
<Column Name="CategoryId" Member="CategoryId" />
<Association Name="FK_Pet_Category" Member="Category" ThisKey="CategoryId" OtherKey="Id" IsForeignKey="true" />
</Type>
</Table>
</Database>

How would I go about using LoadWith? I'm not finding much help online. Any good resources?
I found this online: http://blogs.msdn.com/wriju/archive/2007/10/04/linq-to-sql-change-in-datacontext-from-beta-1-to-beta-2.aspx
You would do something like:
var loadOption = new DataLoadOptions();
loadOption.LoadWith<Pets>(p => p.Category);
db.LoadOptions = loadOption;
var pets = from p in PetStoreContext.Pets
select p;
And then your pets query will already include category, so no trip to the database happens when you try to access category.

If you use the LoadWith LoadOption then your Pet query will do an eager load on categories, so that you will be able to do
MyPet.Category.Name without incurring an extra query, so you'll have the data joined and strongly typed without the risk of running multiple queries for Categories as you loop or bind on the Pet collection.
Or you can use stored procedures in Linq To SQL,the result is strongly typed.

Related

How to build an edit form for a list of strings in Grails?

I have a Grails domain class with a list of strings in it, and I want to edit these strings. For the sake of simplicity let's assume that the list is fixed size. Here's what I have:
MyEntity.groovy:
class MyEntity {
String name
List variables = ['one', 'two', 'three']
static hasMany = [
variables: String,
]
}
_fields/myEntity/variables/_widget.gsp:
<g:textField name="variables[0]" value="${value[0]}" />
<g:textField name="variables[1]" value="${value[1]}" />
<g:textField name="variables[2]" value="${value[2]}" />
This renders text fields for each element in the list that are prefilled with the correct values. However, when I edit the values and sumbit the form my edits get discarded. What am I missing?
Ok, I found it myself. The input fields all need to have the name of the domain property, without any index:
<g:textField id="variables[0]" name="variables" value="${value[0]}" />
<g:textField id="variables[1]" name="variables" value="${value[1]}" />
<g:textField id="variables[2]" name="variables" value="${value[2]}" />

Updating data operation in JSF

I'm much newbie to the JSF. I am creating a simple crud app. I have done adding, and deleting but there is some problem with updating data..... when edit is clicked, bean is populated with values of whom i want to update. but it is not shown in the next page, in which it is to be edited......
this is the link which leads to editPage
<h:column>
<f:facet name="header">Edit</f:facet>
<h:form>
<h:commandLink value="Edit" action="#{personManipulate.editPerson(person)}"/>
</h:form>
</h:column>
this is the code which assign person data to person entity
public String editPerson(PersonEntity person){
this.person=person;
return "success2";
}
This is the code which updates per save person
public String savePerson(){
if(person.getPersonId() > 0){
personDao.updatePerson(person);
return "success";
}else{
personDao.addPerson(person);
return "success";
}
}
}
This is the page where values should be shown and Updated
<h:form>
<h:panelGrid columns="2">
<f:facet name="header">Person</f:facet>
<h:outputLabel for="firstName" value="First name" />
<h:inputText id="firstName" value="#{personManipulate.person.firstName}"
label="First name" />
<h:outputLabel for="lastName" value="Last name" />
<h:inputText id="lastName" value="#{personManipulate.person.lastName}"
label="Last name" />
<h:outputLabel for="address" value="Address" />
<h:inputText id="address" value="#{personManipulate.person.address}"
label="Address" />
<h:outputLabel for="phone" value="Contact Number" />
<h:inputText id="phone" value="#{personManipulate.person.phone}" />
<h:commandButton action="#{personManipulate.savePerson}" value="Submit" />
<h:button outcome="ShowPersons.xhtml" value="Cancel" />
</h:panelGrid>
</h:form>
This is the navigation rule
<navigation-rule>
<from-view-id>JSF/personManipulate.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>JSF/ShowPersons.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>JSF/ShowPersons.xhtml</from-view-id>
<navigation-case>
<from-outcome>success2</from-outcome>
<to-view-id>JSF/personManipulate.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
On Debugging everything is fine i.e. data is assigned to person object but not shown on update page
Well, you're trying to access the #{personManipulate} bean, which still has not been created when you're in the people list. Supposing it is a #ViewScoped bean as it should, that should be created when you address to JSF/personManipulate.xhtml specifically. So I would encourage you to do the People List -> Edit Person transition using a GET request, instead:
showPersons.xhtml
<h:column>
<f:facet name="header">Edit</f:facet>
<h:form>
<h:link value="Edit" outcome="personManipulate.xhtml">
<f:param name="idPerson" value="#{person.id}" />
</h:link>
</h:form>
</h:column>
That will direct you to an url similar as: personManipulate.xhtml?idPerson=1. Then, load the person before the #{personManipulate} bean makes the rendering task:
personManipulate.xhtml
<f:metadata>
<f:viewParam name="idPerson" value="#{personManipulate.idPerson}" />
<f:event listener="#{personManipulate.loadData}" type="preRenderView" />
</f:metadata>
PersonManipulate.java
public void loadData(ComponentSystemEvent event){
person = service.getPersonById(idPerson);
}
That way you keep both view beans unbound and you use urls for navigation. If you aren't interested in showing the person id in the url, then you can use the Flash scope to pass it from bean to bean. Have a look at the linked posts.
See also:
View parameter when navigating to another page
Understand Flash Scope in JSF2

How to automatically populate value from action class to the JSP TextBox

I am able to print value from Java Action Class to JSP Page without textbox, But when I want to put that value in a TextBox in JSP, its not coming.
My Code is :
<S:form>
<s:textfield name="toDate" key="td" > <s:property value="fromDate"/> </s:textfield>
<s:textfield name="fromDate" key="fd"> <s:property value="toDate" /> </s:textfield>
<s:submit value="Confirm"></s:submit>
</s:form>
toDate and fromDate is variables in Java Class.
Just dive in the Struts2 UI Tag documentation:
key : Set the key (name, value, label) for this particular component
If you want them different (eg. toDate the value to set, td the value to read, and so on), specify all of them:
<s:form>
<s:textfield name="toDate" value="td" label="To date" />
<s:textfield name="fromDate" value="fd" label="From date" />
<s:submit value="Confirm" />
</s:form>
(Obviously according to your getters and setters).
You can set value in TextBox using value attribute of <s:textfield> tag to value come from action class.
For this you have your getter and setter method for that variable in action class and read that value by below code:
For Example :
<s:textfield name="some_name" value="%{variable}"/>
your code look like this:
<s:form>
<s:textfield name="toDate" value="%{fromDate}" />
<s:textfield name="fromDate" value="%{toDate}" />
<s:submit value="Confirm" />
</s:form>

Saving Set of Domains For a Parent

I have the following Domains:
class Attribute {
static hasMany = [attributeParameters: AttributeParameter]
}
class AttributeParameter {
String value
Integer sequenceNo
static belongsTo = [attribute: Attribute]
}
I have a form where I want to display all the existing AttributeParameters for an Attribute and allow the user to populate their values and click Save. On Save, each AttributeParameter (which already has an ID) needs to be updated.
I'm currently drawing a blank on how I need to create the HTML for this to work. I've tried this:
Code simplified to clarity:
<form>
<input type="hidden" name="attributeParameters[0].id" value="1" />
<input type="text" name="attributeParameters[0].value" value="1234567" />
<input type="hidden" name="attributeParameters[1].id" value="2" />
<input type="text" name="attributeParameters[1].value" value="name" />
</form>
def save() {
def attribute = Attribute.get(params.id)
attribute.properties = params
}
and it populates the collection correctly, but it doesn't work because the AttributeParameter isn't being fetched before the save, so it is failing with an error:
A collection with cascade="all-delete-orphan" was no longer referenced
by the owning entity instance: com.foo.Attribute.attributeParameters
UPDATE:
I modified the HTML to the following:
<form>
<input type="hidden" name="attributeParameters.id" value="1" />
<input type="text" name="attributeParameters.value" value="1234567" />
<input type="hidden" name="attributeParameters.id" value="2" />
<input type="text" name="attributeParameters.value" value="name" />
</form>
And the controller:
params.list('attributeParameters').each {
def ap = AttributeParameter.get(it.id)
ap.value = it.value
ap.save()
}
This works. My only concern is the order in which the parameters come in. If they always come into the params object in the same order they show up on the form, then I should be ok. but if they ever come in differently, I could be modifying the value of the wrong AttributeParameter.
So still looking for a better way or some sort of verification that they params will always be first in-first out.
UPDATE 2:
I ran across this post and it is what I want but I cannot change Attribute.attributeParameters into a List. They need to stay as a Set.
Could you do something like this:
<form>
<input type="text" name="attributeParameter.1" value="1234567" />
<input type="text" name="attributeParameter.2" value="name" />
</form>
Where you create the names and values dynamically from each AttributeParameter:
<g:textField name="attributeParameter.${attributeParameterInstance.id}" value="${attributeParameterInstance.value}" />
And then in your controller
params.attributeParameter.each {id, val->
def ap = AttributeParameter.get(id)
ap.value = val
ap.save()
}
That way you have the actual id of each parameter directly and it wouldn't matter which order they were processed.

How to get rid of the auto generated j_idt ID in composite component

I'm using below composite component:
<composite:interface>
<composite:attribute name="inputId" />
</composite:interface>
<composite:implementation>
<h:panelGrid id="myTableId">
<h:inputText id="#{cc.attrs.inputId}" value="..." />
...
</h:panelGrid>
</composite:implementation>
And I'm using it in my form as below:
<h:form id="myForm">
<myCompositeComp:test inputId="myInputTextBoxId" />
<h:form>
I've verified the view source for the page and this is how it is generated:
<table id="myForm:j_idt90:myTableId">
...
<input type="text" id="myForm:j_idt90:myInputTextBoxId" />
</table>
How can I get rid of j_idt90 here? Is it the id of my composite component? I read from one of BalusC post that this issue will get fixed if I declare id as static. But I'm not able to identify the place to declare it in my code. Also can I assume <h:panelGrid> is a kind of UINamingContainer?
Yes it is the id of your composite component, <h:panelGrid> is not a UINaminContainer but the composite component is (it has to be, otherwise you would have duplicate IDs if you use it several times inside the same form for example).
Why do you need to get rid of the ID? You can set it yourself if that solves your problem:
<h:form id="myForm">
<myCompositeComp:test id="myComp" attr1="" attr2="" />
<h:form>
the genereated html should look like this:
<table id="myForm:myComp:myTableId">
....
<input type="text" id="myForm:myComp:myInputTextBoxId"
</table>

Resources