Read model properties and values using Custom Attributes - asp.net-mvc

I have custom ActionMethodSelectorAttribute. Is it possible to read the model properties and it's value in there?

Related

Can HTML hidden field hold complex types?

In in post back to server side MVC controller action, to maintain the data for a MVC model property that is not for user input, the following topic suggests to use hidden field on markup view page:
MVC3 - Why are navigation properties null on postback
But to me, such an hidden field works just for a primitive C# types like int, bool, string etc. But complex-type property such as user-defined class or collection of primitive types (like List) does not work with hidden field on post back to server. Is my thought correct?
In fact, I tried such a hidden field for complex type property and the post back model property is null. Server side session object or database re-query is a rescue to re-populate complex-type property on server post back. Is there some other way to keep data for complex-type model property between post back?
I appreciate your help.
Update:
The hidden field for complex-type property is not data sensitive, no need data security for it. What I need to use some way rather than database re-query or server-side session variable. So, instead of using the hidden property as complex type in my MVC model, I use a string property that is complex-type serialized. On post back, I deserialize that hidden property to feed data for another property. Any idea?

object property values for individuals in Jena

I have created an ontology using Protégé. Using Jena, I am creating an individual and assigning values for its properties. Datatype properties can be created using getOntProperty(String uri), but using the same method I cannot create object properties. Which method can be used to create object properties and add values for an object property?
getOntProperty(String) is an OntModel method for creating or retrieving properties from an ontology, not for making property assertions about Individuals. For datatype properties, you should probably be using getDatatypeProperty(String) instead. For object properties, you should use getObjectProperty(String). All of these methods are described in the documentation. Once you have a property, you can use the methods that an Individual inherits from Resource to add properties values. In particular, you'd want the addProperty and addLiteral methods.

Can't understand, what does method getSingularObjectFromString do?

I'm developing own custom field type in JIRA.
My class is very simple, it extends GenericTextCFType.
My goal is to store some identifier (ID) of field value in database but to show human-readable caption of the field value on Issue form.
I searched methods of GenericTextCFType class and found method getSingularObjectFromString, and I don't understand, what it does.
JIRA javadoc says: "Returns a Singular Object, given the string value as passed by the presentation tier"
But what is the Singular Object and what is it needed for?
Yes, it's not a great name. I wrote about it in detail in "Practical JIRA Plugins"
(O'Reilly). Here's an extract from there describing many of the methods in detail (sorry about the formatting). The book also has worked examples available at https://bitbucket.org/mdoar/practical-jira-plugins
CustomFieldType Methods
The example’s custom field type class will implement the CustomFieldType interface as usual, but instead will extend a class higher up in the inheritance hierarchy than NumberCFType. The class we will extend is AbstractCustomFieldType and it’s at the root of most classes that implement CustomFieldType.
The methods in the CustomFieldType interface with “SingularObject” in their name refer to the singular object, in this example a Carrier object. All other methods in JIRA 4 custom fields that refer to an Object are referring the transport object, e.g., a Collection of Carrier objects. JIRA 5 removed the use of Object in most custom field methods.
For more information about what changed in JIRA 5.0 with custom fields see https://developer.atlassian.com/display/JIRADEV/Java+API+Changes+in+JIRA+5.0#JavaAPIChangesinJIRA5.0-CustomFieldTypes. There were some major changes in the class hierarchy, and most classes now have a Java generic as a parameter instead of just using an Object as before.
There are two objects that are typically injected into the constructor of a custom field type’s class. The first is a CustomFieldValuePersister persister object, which is what will actually interact with the database. The second is a GenericConfigManager object that is used for storing and retrieving default values for the custom field. Other objects are injected into the constructor as needed—for example, the DoubleConverter in Example 2-2.
The first set of methods to consider are the ones that the custom field type uses to interact with the database in some way.
getSingularObjectFromString()
This method converts a string taken from the database such as “42.0###The answer” into a Carrier object. A null value means that there is no such object defined.
Fields with Multiple Values
Collection<Carrier> getValueFromIssue(CustomField field, Issue issue)
This is the main method for extracting what a field contains for a given issue. It uses the persister to retrieve the values from the database for the issue, converts each value into a Carrier object and then puts all the Carrier objects into a trans- port object Collection. A null value means that this field has no value stored for the given issue. This is one of the methods that used to return an Object before JIRA 5.0
createValue(CustomField field, Issue issue, Collection<Carrier> value)
updateValue(CustomField field, Issue issue, Collection<Carrier> value)
These methods create a new value or update an existing value for the field in the given issue. The persister that does this expects a Collection of Strings to store, so both of these methods call the method getDbValueFromCollection to help with that.
getDbValueFromCollection()
A private convenience method found in many custom field type classes, sometimes with a different name. It is used to convert a transport object (e.g., a Collection of Carrier objects) to a Collection of Strings for storing in the database.
setDefaultValue(FieldConfig fieldConfig, Collection<Carrier> value)
Convert a transport object (a Collection of Carrier objects) to its database repre- sentation and store it in the database in the genericconfiguration table.
Collection<Carrier> getDefaultValue(FieldConfig fieldConfig)
Retrieve a default value, if any, from the database and convert it to a transport object (a Collection of Carrier objects). The FieldConfig object is what represents the context of each default value in a custom field.
The next set of methods to consider are the ones that interact with a web page in some way. All values from web pages arrive at a custom field type object as part of a Custom FieldParams object, which is a holder for a Map of the values of the HTML input elements.
validateFromParams(CustomFieldParams params, ErrorCollection errors, FieldConfig config)
This is the first method that is called after a user has edited a custom field’s value. Any errors recorded here will be nicely displayed next to the field in the edit page.
getValueFromCustomFieldParams(CustomFieldParams customFieldParams)
This method is where a new value for a field that has been accepted by validate FromParams is cleaned and converted into a transport object. The custom FieldParams object will only contain strings for the HTML elements with a name attribute that is the custom field ID—e.g., customfield_10010. A null value means that there is no value for this field.
getStringValueFromCustomFieldParams(CustomFieldParams parameters)
This method returns an object that may be a String, a Collection of Strings or even a CustomFieldParams object. It’s used to populate the value variable used in Chapter 3: Advanced Custom Field Types Velocity templates. It’s also used in the Provider classes that are used by custom field searchers.
String getStringFromSingularObject(Carrier singularObject)
This method is not the direct opposite of getSingularObjectFromString as you might expect. Instead, it is used to convert a singular object (Carrier) to the string that is used in the web page, not to the database value. The returned String is also sometimes what is stored in the Lucene indexes for searching (“More Complex Searchers” on page 57). The singular object was passed into this method as an Object before JIRA 5.0.
The final set of CustomFieldType methods to consider are:
Set<Long> remove(CustomField field)
This method is called when a custom field is entirely removed from a JIRA instance, and returns the issue ids that were affected by the removal. The correct method to use for deleting a value from a field is updateValue.
String getChangelogValue(CustomField field, Object value)
String getChangelogString(CustomField field, Object value)
These methods are how the text that is seen in the History tab of an issue is gen- erated. When a custom field of this type changes, these methods are called with the before and after values of the field. The difference between the two methods is that the if the value later becomes invalid, the string will be shown instead (https://developer.atlassian.com/display/JIRADEV/Database+Schema#DatabaseSchema-ChangeHistory).
extractTransferObjectFromString()
extractStringFromTransferObject()
These methods are not from the CustomFieldType interface but they exist in the standard Multi fields for use during project imports.
Other Interfaces
There are a few other interfaces that are commonly implemented by custom field types.
ProjectImportableCustomField
The getProjectImporter method from this interface is used to implement how the custom field is populated during importing a project from an XML backup. If you don’t implement this interface then project imports will not import values for your custom field.
MultipleCustomFieldType
MultipleSettableCustomFieldType
These two interfaces are used by custom fields with options and that furthermore can have more than one option. For these classes, the values can be accessed using the Options class, which is a simple subclass of a Java List. These interfaces are not really intended for use by general-purpose multiple value custom field types.
Fields with Multiple Values | 41
SortableCustomField
This interface contains a compare method for comparing two singular objects. This is used by the Issue Navigator when you click on a column’s heading to sort a page of issues. This is actually a slower fallback for custom fields that don’t have a searcher associated with them (see Chapter 4).
RestAwareCustomFieldType
RestCustomFieldTypeOperations
These two interfaces are how the JIRA REST API knows which fields can be retrieved or updated. New in JIRA 5.0.

Default value to core data attributes (column) for iOS programmatically

Current Approach
I set the default value of the attribute directly to the data model file using the inspector.
Problem
I have enum for the values a particular attribute can take.
I am worried maintaining them at a later stage might be difficult.
Suppose if I decide to change the enum values, then I would have to manually go the inspector and change it.
Since I have quite a number of attributes based on enum values, it becomes difficult.
Question
How can I add default values to core data attributes programmatically ?
Is there any alternative to do this, so that maintenance would be easier ?
Everything you can do graphically in the Core Data model editor you can do using the classes Core Data provides for creating/introspecting a managed object model. For this use case, you can use NSEntityDescription to look up an entity, its properties or propertiesByName accessors to find the NSAttributeDescription for the attribute you're interested, and setDefaultValue: to do the same thing the Core Data model editor does.
You might find this the most appropriate way to do what you're looking for. Or, as #DimitryShevchenko notes, you can initialize values in your NSManagedObject subclass' awakeFromInsert method -- which way you choose might depend on your workflow or other requirements of your application.
You can subclass your NSManagedObject and set default values in awakeFromInsert
Related docs (see Object Life-Cycle)

Should my viewmodel value type properties be nullable?

I've been making my viewmodel properties nullable for quite some time now. My reasoning is that when a validation occurs, I don't want default values inserted into the fields that the user left empty, but are required.
I mark my required fields with required, for course, but this got me to thinking that i'm losing a great deal of fidelity in the object model by doing this.
Of course my domain classes are only nullable when they can actually be null.
Should my viewmodel properties be nullable when the domain model requires them?
Should my viewmodel properties be nullable when the domain model requires them?
Yes, they should in order to properly perform validation on the view model. When you ensure that the view model is valid and map this view model back to the actual domain model in your mapping layer you will be certain that a value would be provided for this property.

Resources