How can i add more than on paralel corpus or monolingual model into my custom model ? - Multiple inheritance - translation

When I create a new model, how can I add more than one MonolingualCorpus or Parallel Corpus in Java because the method only accepts one InputStream for each model.
TranslationModel modelFirst = new CreateModelOptions.Builder()
.baseModelId("en-fr")
.name(p.getName())
.forcedGlossary(new FileInputStream(new File("forced1")))
.parallelCorpus(new FileInputStream(new File("paralel1")))
.monolingualCorpus(new FileInputStream(new File("mono1")))
.build();
Because otherwise, I don't understand the utility to create each time a new model if when I want to make a translation, I need to specify the ID of my custom model.
If I create a model with a forcedGlossary and a ParallelCorpus, the function returns me a new Model ID, for example, 12345.
After that, I want to add one ParallelCorpus and one Monolingual Model to this same model (ModelFirst). So I need to recreate a new model...
TranslationModel modelSecond = new CreateModelOptions.Builder()
.baseModelId(12345)
.name(p.getName())
.parallelCorpus(new FileInputStream(new File("paralel2")))
.monolingualCorpus(new FileInputStream(new File("mono2.txt")))
.build();
Now id of ModelSecond = 4567.
If I translate with the ModelFirst(12345), will Watson use the model from the ModelFirst AND from the ModelSecond or ONLY from the ModelFirst ??
If I translate with ModelSecond, Will it inherit my ModelFirst and basic model?
Can a model have multiple inheritance ?

You can create a model with only 1 Monolingual Corpus. If you need to add more than one I would suggest you combine the models into one.
Custom models can only be created out of base models. There is no such thing as an inheritance for custom models.

Related

UI5 Can we create json model using just the entity type metadata from a service model

I want to create a model and set it as one of the global model in oninit method of component. I have a sap gateway odata service with just an entity type and do NOT have corresponding entityset.
My ui5 project has manifest.json with default model and component.js Is it possible to to create a model using just the metadata.
When you initialize the model, all you need is the metadata for initializing the SAPUI5 OData model. As long as the metadata is syntactically valid it should not be a problem. The EntitySets are only required when you assign an aggregation to a control using this model.
Update:
Its possible to get the details of the metadata from the model & create a new JSON model with it.
var oModel = new sap.ui.model.odata.ODataModel("http://services.odata.org/V2/OData/OData.svc");
var metaModel = oModel.getMetaModel();
var oEntitySet = metaModel.getODataEntitySet("Products"); //For getting properties of an Entity Set
//For Entity, you may have to provide the service namespace along, in this case ODataDemo
var oEntity = metaModel.getODataEntityType("ODataDemo.Product");
The oEntity object will look something like this
With this you can create a new JSON model as
var oODataJSONModel = new sap.ui.model.json.JSONModel({"Selection" : oEntity.property });
Here is a working example

Insert new object by attaching existing entity properties in Nhibernate

We have create screens with duplicate option for each object. For ex: While creating new Customer along with details, user can chose existing customer to copy groups associated from existing to new user. So I would like to know how to assign properties of new customer for 1:n and m:n scenario.
For "Customer" and "CustomerGroups". Will the below approach work fine?
Customer existing = repo<Customer>(id);
Customer newCust = new Customer();
for(var group in existing.Groups)
newCust.CustomerGroups.Add(new CustomerGroup(){ **AllpropertiesexceptID**, **Customer=newCust** } );
For Order and OrderItems, since its m:n relationship, just attaching existing items with new order.
Orders existing = repo(id);
Order newOrder = new Order();
for(var item in existing.Items)
newOrder.Items.Add(item);
Is it required to do Session.Evict for the existing Order or Customer for performing these operations.
You might be thinking too much. :) Just go ahead and create separate lookalike objects, pretending NHibernate isn't even there. As long as the new instances have different (unset) identity properties, NHibernate won't even realize they are "copies".

How to pass collection of anonymous objects from controller to view

I am new to MVC, so please excuse me if my question sounds silly or too simple. I am using Entity Data Model for database access. So in my Models folder, I have added an EDMX file and I can access the model classes from my controllers and strongly typed views. The problem arises when I access more than one table in my controller e.g.
If I have following tables in my DB :
Departments(DepartmentID, DepartmentName, DepartmentPhone)
Insurances(InsuranceID, InsuranceName, InsuranceAddress)
Employees(EmployeeID, EmpFName, EmpLName, DepartmentID, InsuranceID)
And I want to display a list of Employees with their department and insurance information.
In my Controller's Action Method I access the DB using EDM and get the information in an anonymous type:
using (var context = new MyEntities())
{
var model = (from d in context.Departments
join e in context.Employees on d.DepartmentID equals e.DepartmentID
join I in context.Insurances on I.InsuranceID equals e.InsuranceID
select new
{
DepartmentID = d.DepartmentID,
EmployeeID= e.EmployeeID,
EmpFName= e.EmpFName,
EmpLName= e.EmpLName,
DepartmentName= d.DepartmentName,
InsuranceName= I.InsuranceName
}).ToList();
return View(model);
}
I don't have a class of this anonymous type in my Model folder so I can't create a strongly typed view. So what is the best way to pass this list to the View?. Using viewbag will be an overkill if the collection is too large. Creating a new Model for this anonymous class doesn't sound right as it needs to be updated all the time if I change my selection in Controllers Action Method.
All suggestions are welcomed. I tried looking through other questions on SO but couldn't find anything relevant.
Thanks.
I don't have a class of this anonymous type in my Model folder so I
can't create a strongly typed view
Right click on your project, Add New Class ... and now you have a type in your Model folder. This is the way to go in ASP.NET MVC => view models.
And then obviously you pass this type to your view:
select new MyViewModel
{
DepartmentID = d.DepartmentID,
EmployeeID = e.EmployeeID,
EmpFName = e.EmpFName,
EmpLName = e.EmpLName,
DepartmentName = d.DepartmentName,
InsuranceName = I.InsuranceName
}).ToList();
And of course now your view becomes strongly typed to this view model:
#model IEnumerable<MyViewModel>
...
I'm afraid that predefined strongly-typed ViewModels are the way to go. It is a pain to have to update seemingly duplicate code in multiple places but in my experience it's only a problem for smaller projects. As the project grows you begin to see differences between database model objects (entities) and the viewmodels passed to your views, such as Validation and processing attributes and view-specific data, and when you get to that point you begin to prefer having separate Entity and ViewModel definitions.
However, back on-topic: an alternative solution to your problem is to use reflection to convert an anonymous type into a Dictionary<String,Object> object. Note that ASP.NET MVC does this for converting new { foo = "bar" }-syntax expressions into dictionaries for Route Values and HTML attributes already. Performance is acceptable, but don't try to do it for 10,000 objects for a single HTTP request otherwise you might get bogged down.
Here's what the code for that would look like:
Object anonymousType = new { whatever = "foo" };
Dictionary<String,Object> dict = new Dictionary<String,Object>();
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(anonymousType )) {
Object value = descriptor.GetValue(anonymousType );
dict.Add( descriptor.Name, value );
}
Of course this means that your Views won't benefit from compile-time type-checking and you'll have to maintain a documented list of dictionary keys (assuming you aren't iterating over keys in your view).
I'll note that I am surprised Microsoft didn't make anonymous types automatically implement IDictionary because it seems natural.
dynamic type is your friend.
You can declare your view as loosely typed, having a dynamic as your model
#model dynamic
You will access model properties as you do in strongly typed view
<h1>Model.DepartmentId</h1> - <h2>Model.DepartmentName</h2>
<span>Model.EmployeeId</span>
The problem thought, that dynamics contains internal properties, if you are using MVC2 you need a little trick to make it work. But seems for MVC3 and higher, it's not longer required.

repository validation

What's the best way to validate a model when using mvc with repository?
I look for examples but I didn't find any that is exactly what I need.
Assuming I have a model with 5 properties.. 3 of them with dataannotations.. and I have some validations that I need to check in database before insert.
I need something like 'User.IsValidToInsert' to check if its valid. But I want to use 'ModelState.IsValid' too, cause I dont want to check manually all properties with dataannotations in 'IsValidToInsert'.
How can I do this? Should I set validations that access database in 'IsValidToInsert'? Should I pass 'User' and 'ModelState' like parameters to repository class?
You should be using a View Model that is specific to the view. If you have a Create action to create a Product, create a ProductCreate view model. You can put the data annotations (or Fluent Validation, etc.) that is specific to creating a product. This will be the model for your View/Controller. If you have an Edit page, then create a ProductEdit view model.
Now if you have additional logic (such as validating if a user already exists, then you should put that in a service layer. Your controller should be as simple as possible. You post your view model, convert it to a domain model and pass it to the service layer if necessary.
[HttpPost]
public ActionResult Create(ProductCreate model)
{
// simple validations
if( !ModelState.IsValid )
{
return View(model);
}
// Domain Model / Entity
Product product = // create a product from your model here
// service layer
ProductService.Add(product);
return RedirectToAction("Index");
}

Newbie - combining parent and child views into one page

I am trying to learn mvc - I apologize in advance for all the silly questions. What I've done is created an mvc proejct based on an exsiting database, and then i've been disecting it to try to understand what's been created for me, and how to create my own stuff. Unfortunately, because I'm a new stackoverflow user, i can't post a picture of my project structure.
i have a parent controller and a child controller created using the wizard based on 2 separate tables i have in my model. I want to display both of them in one view - ultimately in a webgrid and be able to change data for any parent item, or child item. You can ignore the CombinedController for now. I've been doing some reading and i've learned that i should be creating a viewmodel that combines both the parent and child at the model level, and then go from there.
so i created this class:
Imports System.Data.Objects.DataClasses
Public Class ParentAndChild
Public Property myChildren As IEnumerable(Of Child)
Public Property myParent as Parent
End Class
I have a few questions:
Question 1
Do i have to add this ParentAndChild entity into the .edmx file in order to create a controller and view for it? I guess what I'm really asking is do i have to create a view in the sql database first, have the entity show up in the .edmx and then create a controller? Or can I just combine the two entities in a class? That's what i've done so far. I don't have a sql View in my database combining these two tables. The reason why I ask is because when i create my controller, if i want to get all the CRUD for free, i have to create using the Entity Framework. But I don't know what to specify for the data context.
Question 2
If i want a webgrid to somehow show all my parents and all their children, will the new ParentAndChild class work? I think that will only show the details for One parent and its children. I think i need to create a list of parents.. and then in the parentlist for each parent, add a collection of modules. But i don't know how to do this..
Question 3
How does the entityframework know which modules to return when i'm using my new class? I don't define the relationship anywhere... Is it because in the .edmx file, the system knows the relationships between the tables?
If you simply need to access the information of a parent entity and its child entity's you should be able to send your view the parent entity. A ViewModel is not absolutely necessary for this if Entity Framework knows about the relationship between the two entity's. If the relationship between the 2 tables exists at the database level then Entity Framework should have modeled it automatically.
An example of a controller would be:
public ActionResult Parent(int id)
{
var parent = context.Foo.Single(x => x.Id == id);
return View(parent);
}
If you need to create a table of all the child entity's you could do something like this:
#foreach(var item in Model.Children)
{
<td>#item.Property</td>
<td>#item.Property2</td>
}

Resources