How can we add one more ProductType to Product? - commercetools

I recently work with Commercetools and I have a question.
How can we add one more ProductType to Product? I've heard it can be implemented by attribute reference. So, I'm trying to do this way:
final Reference<ProductType> productTypeReference = ProductType.referenceOfId(request.getProductTypeId()).toReference();
final AttributeDraft draft = AttributeDraft.of(request.getAttributeName(), productTypeReference);
final SetAttributeInAllVariants attribute = SetAttributeInAllVariants.of(draft);
return ProductUpdateCommand.of(product, attribute);
But it does not work? How it can be implemented?

While referencing a productType is possible, it may be more advisable to create a single productType that contains all of the attributes you wish to have.
You can reference a prouductType which is outlined here: https://docs.commercetools.com/http-api-types#references
However, to return the data from the reference you'll need to expand the reference which is outlined here:
https://docs.commercetools.com/http-api#reference-expansion
Does this help?
Best Regards,
Michael

Related

FireStore and maps/arrays, document-list to array in Kotlin

I've finally started to understand a lot of info regarding FireStore, but I'm wondering if I can get some assistance.
If I had a setup similar to or like this:
          races
                Android
                      name: Android
                      size: medium
                       stats          <---- this is the map
                                str: 10
                                sex: 12.... (more values)
How would I parse this? I am looking to make specific TextViews apply values found in the database so that I can simply update the database and my app will populate those values so that hard coding and code updating won't be nearly as troublesome in the future.
I currently use something like this:
val androidRef = db.collection("races").document("Android")
androidRef.get().addOnSuccessListener { document ->
if (document != null) {
oneOfTheTextViews.text = document.getString("str")
} else {
}
The issue is currently I can only seem to access from collection (races) / document (android) / then a single field (I have "str" set as a single field, not part of a map or array)
What would the best practice be to do this? Should I not nest them at all? And if I can reference said nesting/mapping/array, what functions need to be called? (To be clear, I am not asking only whether or not it is possible - the reference guides and documents allude to such - but what property/class/method/etc needs to be called in order to access only one of those values or point to one of those values?).
Second question: Is there a way to get a list of document names? If I have several races, and simply want to make a spinner or recycler view based on document names as part of a collection, can I read that to the app?
What would the best practice be to do this?
If you want to get the value of your str property which is nested within your stats map, please change the following line of code:
oneOfTheTextViews.text = document.getString("str")
to
oneOfTheTextViews.text = document.getString("stats.str")
If your str property is a number and not a String, then instead of the above line of code please use this one:
oneOfTheTextViews.text = document.getLong("stats.str")
Should I not nest them at all?
No, you can nest as many properties as you want within a Map.
Is there a way to get a list of document names?
Yes, simply iterate the collection and get the document ids using getId() function.

Should we put an S in the name of array of things?

It is more an English question than a Coding question, but both are related :
Should we put an S in the name of array of things ? I want to know if it is just an opinion question or if there is a real English rule.
How to name an array of that array ?
Example 1
I have an object named Constraint. I want to create an array of that object. How should I name it ? ConstraintsArray or ConstraintArray ?
Answer
Do not use Hungarian Notation. Call it Constraints.
Example 2
How to name an array of that array ?
Thank you for your answers.
IMO, you should just call it "constraints", without the "array" prefix (referring the Clean code.. :) )
ConstraintsArray seems redundant in meaning.
You can use ConstraintArray or just Constraints.
I think it really depends on the coder himself. No real rules on variable on Grammar. :) . for me I like using singular form eg. ConstraintArray...
That is very much up to your personal style. I would not do it. A personArray is already a list of more than one person. You should know that.
But if the array objects are lists too it's a very different situation. Names like itemsDictionariesList, or repositoriesArray is something I used in my project.

XPages checkbox group - binding to a hashmap

I have a Hashmap (sessionScope) containing the names of guests and their UNIDs.
For example, in the hashmap below, Carmen Adamson is the key. The values of guests in her cabin are: Carmen Adamson and Stephen Adamson. The alias for each checkbox field is the Guest UNID and the values are separated by a "~".
CARMEN ADAMSON STEPHEN ADAMSON | 1C145A291A5DFEC188257E200071402E~CARMEN ADAMSON | 813B80C332EC192188257E2000714030
Can someone help me with some example code on how I can bind a checkbox group field directly to a hashmap?
Thanks so much,
Dan
I was able to figure it out:
<xp:checkBoxGroup id="checkBoxGroup1">
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:var iterator:java.util.Iterator = sessionScope.guestlistArray.entrySet().iterator();
while (iterator.hasNext()) {
var entry = iterator.next();
var guestlist = entry.getValue();
}
#Explode(guestlist,"~");}]]></xp:this.value>
</xp:selectItems>
</xp:checkBoxGroup>
Dan, you can certainly do this, but will have to use java to accomplish it. You can write a method to pass the hashmap and return the SelectItem objects with the data and labels. I could be wrong, but I don't think it can be done through SSJS.
See this related question that has an excellent answer from Mark Leusink and includes a code example.
Populating selectItems of the combobox (label, value) using a managed bean
This example is from my blog that gives more detail in creating SelectItems using java. My example was for a ComboBox, but the checkbox also uses the SelectItem. The question above was the basis for what I wrote.
http://notesspeak.blogspot.com/2015/05/using-db2-in-xpages-part-6-populating.html

How to declare, same webelement with different ids using Findby?

For instance, is it possible to define:
#FindBy(By.id("id1") OR By.id("form1:id1"))
public WebElement button
So that button having either "id1" or "form1:id1" should work fine?
You can use the #FindBys annotation, the syntax is:
#FindBys({#FindBy(id = "foo"),
#FindBy(className = "bar")})
The JavaDoc describes it here:
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/FindBys.html
Well,
use whathever you want, as long as it works
Personally I would use #FindBy(By.id("id1")) but it is just point of choice.
Also, there is no value added in referring same element twice with two different methods. It will only cause mess in your code
EDIT
As I understood your comment, there is element on the page which constantly changes its ID. If you need to refer to such elements, try using xPath See for example this xpath tutorial
The idea is that you will point to some place in the DOM rather than to specific ID
Use Xpath or CSS selector to do that. Or Java to store the ID name in String and then you can fill it to your Id.

C#, Linq data from EF troubles

I don't have much experience with programming. I am working (to learn) on a project. I am using C# 4.0 and WPF 4 with EF (SQLite). I am having problemw with LINQ.
Here is the code in question (I hope this is enough, let me know if more is needed)
private void cboSelectCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (ShippingEntities context = new ShippingEntities())
{
var companies = from company in context.Deliveries
where company.State == cboSelectCompany.SelectedItem.ToString()
select company;
txtDeliveryName.Text = companies.Name;
txtDeliveryState.Text = companies.State;
}
The last two lines don't work. Am I misunderstanding what LINQ is returning? I just get this error
Error 5 'System.Linq.IQueryable<SqliteDemo.Delivery>' does not contain a definition for 'State' and no extension method 'State' accepting a first argument of type 'System.Linq.IQueryable<SqliteDemo.Delivery>' could be found (are you missing a using directive or an assembly reference?) c:\users\dan\documents\visual studio 2010\Projects\SqliteDemo\SqliteDemo\DeliveryCompanies.xaml.cs 49 51 SqliteDemo
If anyone could give me some pointers or to a good reference I would appreciate it
You're close!
Linq is returning a Queryable set of Deliveries, which hasn't executed yet. My guess based on reading your code is that you're expecting at most one result, and then you want to put those values into a UI of some sort. In that case what you'll want to do is:
Take your IQueryable and execute it
Make sure you grab the first (or, alternatively enforce that there is only one) row
Set the appropriate values in the UI.
So, leave the line with the query there, and then change the rest to something like this:
var company = companies.FirstOrDefault();
txtDeliveryName.Text = company.Name;
txtDeliveryState.Text = company.State;
Insert null-checking as appropriate, and go check out the differences between First, FirstOrDefault, Single, and SingleOrDefault to see which one seems most correct for your particular situation.
Well, looks like companies.State doesn't compile, because companies is a list of SqliteDemo.Delivery (and not just a SqliteDemo.Delivery).
What do you want to achieve? Imagine that your LINQ query returns several results, is it possible?
Just use companies.FirstOrDefault() or companies.Single() to access first item becouse by default LINQ returns collection of items not just single item.

Resources