I have a simple Entity "Products" with attributes:
id int64
sku text
descript text
quantity int64
unitPrice Decimal
totalPrice Decimal
What i need is the value of totalPrice to be result of quantity + totalPrice
To do so i need probably to use subclass of NSManagedObject instead of Entity.
I generated such a class from the Entity but i don't know how to implement the class.
I want to ADD, REMOVE SET and GET records.
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Products : NSManagedObject
#property (nonatomic, retain) NSString * descript;
#property (nonatomic, retain) NSNumber * id;
#property (nonatomic, retain) NSNumber * quantity;
#property (nonatomic, retain) NSString * sku;
#property (nonatomic, retain) NSDecimalNumber * totalPrice;
#property (nonatomic, retain) NSDecimalNumber * unitPrice;
#end
#import "Products.h"
#implementation Products
#dynamic descript;
#dynamic id;
#dynamic quantity;
#dynamic sku;
#dynamic totalPrice;
#dynamic unitPrice;
#end
Just implement totalPrice as a transient property. Mark it as transient in the Core Data model editor.
Then, in your managed object subclass, just override the getter.
- (NSNumber*)totalPrice {
return #(self.unitPrice.floatValue * self.quantity.intValue);
}
If any of the other two attributes are not set it should return 0 as expected.
Related
My class:
/**
* A Customer.
*/
#Entity
#Table(name = "customer")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#Audited
#Introspected
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
#Type(type = "uuid-char")
#Column(name = "uuid", length = 36, nullable = false, unique = true)
private UUID uuid;
}
I call:
Customer result = customerRepository.save(cust);
Even if I use #Introspected annotation in my Pojo class Customer, Error I get:
Unexpected error occurred: Unable to perform beforeTransactionCompletion callback: No bean introspection available for type [class org.hibernate.envers.DefaultRevisionEntity]. Ensure the class is annotated with io.micronaut.core.annotation.Introspected
All tables related audit is created.
Using Jhipster + Micronaut + Hibernate Envers
Below is my setup info:
MHipster v1.0.2 :: Running Micronaut v2.4.4
JHipster version: 6.10.5
I've parent and child class with respective DTO's as follows
class Parent {
List<Child> children;
// setters and getters
}
class Child {
Parent parent;
}
class ParentDto {
List<ChildDto> children;
// setters and getters
}
class ChildDto {
ParentDto parent;
// setters and getters
}
When I try to map Parent to ParentDto I'm getting StackOverflowError.
Please help me in resolving the issue.
I know this post is old but I will try to give one answer in case someone from google to arrived here.
Probably you have a dynamic object creation.
Recently I had a problem like this. In my case I have a class Student that extends Person. And Person has three others attributes of type Person: father, mother and responsible. In case, I had a circularity relationship. When I ran my JUnits tests I got a circularity calls. I did't get a stakOverflow because the Spring (after some time) closes the data base connections.
I resolved this problem removing the dynamics objects creations from my Person class.
Another approach to solve this problem is adding a Condition in your mapper. You can read more about conditions here. For example, you can have some Conditions saying: "If the person attribute id is 10, then, doesn't need map it.". This way we can avoid infinity mapping.
Well, let's see some snippeds of code:
Student class:
public class Student extends Person implements IStudent {
private Person financialResponsible;
// Getters and setters.
}
Person class:
public class Person implements IPerson {
private Long id;
private String name;
private Person father;
private Person mother;
private Person responsible;
private Address address;
// Getters and setters.
// This is my real problem. I removed it and the mapper worked.
public IPerson getFather() {
if (father == null) father = new Person();
return father;
}
}
StudentDTO class:
public class StudentDTO extends PersonDTO {
private PersonDTO financialResponsible;
// Getters and setters.
}
PersonDTO class:
public class PersonDTO {
private Long id;
private String name;
private PersonDTO father;
private PersonDTO mother;
private PersonDTO responsible;
private AddressDTO address;
// Getters and setters.
}
Here is a example of conditions:
...
import org.modelmapper.Condition;
...
ModelMapper mapper = new ModelMapper();
// Define STRICT to give high precision on mapper.
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
/*
* Create conditions to avoid infinity circularity.
*/
// Condidions.
final Condition<IStudent, StudentDTO> fatherIsTen = mappingContext -> mappingContext.getSource().getFather().getId() == 10;
final Condition<IStudent, StudentDTO> motherIsTen = mappingContext -> mappingContext.getSource().getMother().getId() == 10;
final Condition<IStudent, StudentDTO> resposibleIsTen = mappingContext -> mappingContext.getSource().getResponsible().getId() == 10;
// Adding conditions on mapper.
mapper.createTypeMap(IStudent.class, StudentDTO.class) //
.addMappings(mapper -> mapper.when(fatherIsTen).skip(StudentDTO::setFather))
.addMappings(mapper -> mapper.when(motherIsTen).skip(StudentDTO::setMother))
.addMappings(mapper -> mapper.when(resposibleIsTen).skip(StudentDTO::setResponsible));
I hope help o/
I am trying to use Datastax's UDT mapper for a table which contains the list of UDT's. The driver throws an exception while trying to instantiate the UDTmapper. It seems to be unable to map the list of instances of the class which represents my UDT.
The user defined types and tables are created with the statements:
CREATE TYPE IF NOT EXISTS keyspace.value (
id uuid,
values list<text>
);
CREATE TYPE IF NOT EXISTS keyspace.epoch (
name text,
description text,
start_time timestamp,
duration int,
values list<frozen<value>>
);
CREATE TABLE IF NOT EXISTS keyspace.service_level_agreements (
id uuid,
name text,
description text,
epochs list<frozen<epoch>>,
chargeback_info uuid,
PRIMARY KEY (id)
);
The classes are:
public class Value {
#Field(name = "id")
private UUID sloId;
#Field(name = "values")
private List<String> values;
}
public class Epoch {
#Field(name = "name")
private String name;
#Field(name = "description")
private String description;
#Field(name = "start_time")
private Date startTime;
#Field(name = "duration")
private long duration;
#Field(name = "values")
private List<Value> values;
}
#UDT (keyspace = "keyspace", name = "service_level_agreements")
public class ServiceLevelAgreement e {
#Field(name = "id")
private UUID id;
#Field(name = "name")
private String name;
#Field(name = "description")
private String description;
#Field(name = "epochs")
private List<Epoch> epochs;
#Field(name = "chargeback_info")
private UUID charegebackInfo;
}
When I am trying to instantiate UDT mapper I am getting an exception:
Cannot map unknown class com.me.Epoch for field private java.util.List com.me.ServiceLevelAgreement.epochs
It seems that the UDT mapper cannot fund Epoch class although it is on the classpath. I also tried to move Epoch class inside ServiceLevelAgreement , but it did not help. Any Idea what I am doing wrong?
The issue was caused by my misunderstanding what class should be tagged with #UDT annotation. I put it instead of the table annotation, but it should in the class which describes my UDT. After I fixedthe annotations, everything started working.
I'm trying to make a binding for StickNFind to use in a Xamarin-based project.
The problematic class is LeDeviceManager, it inherits CBCentralManagerDelegate, which is an abstract class and the UpdateState(m) method is not a part of the binding.
Here's the Obj-C header for this class:
interface LeDeviceManager : NSObject <CBCentralManagerDelegate>
#property (nonatomic,strong) NSMutableArray *devList;
#property (nonatomic) CBCentralManager *btmgr;
#property (nonatomic,strong) id <LeDeviceManagerDelegate> delegate;
- (id) initWithSupportedDevices: (NSArray *) devCls delegate: (id <LeDeviceManagerDelegate>) del;
- (void) startScan;
- (void) stopScan;
#end
And this is the binding I've come up with, with Objective Sharpie's heavy assistance:
[Model, BaseType (typeof(CBCentralManagerDelegate))]
public partial interface LeDeviceManager
{
[Export ("devList", ArgumentSemantic.Retain)]
NSMutableArray DevList { get; set; }
[Export ("btmgr")]
CBCentralManager Btmgr { get; set; }
[Export ("delegate", ArgumentSemantic.Retain)]
LeDeviceManagerDelegate Delegate { get; set; }
[Export ("initWithSupportedDevices:delegate:")]
IntPtr Constructor (NSObject[] devCls, LeDeviceManagerDelegate del);
[Export ("startScan")]
void StartScan ();
[Export ("stopScan")]
void StopScan ();
[Export ("UpdatedState")]
[New] // Added as suggested by another SO post, also tried [Abstract]
void UpdatedState(CBCentralManager mgr);
}
And I get this error:
Error CS0533: StickNFind.LeDeviceManager.UpdatedState(MonoTouch.CoreBluetooth.CBCentralManager)' hides inherited abstract memberMonoTouch.CoreBluetooth.CBCentralManagerDelegate.UpdatedState(MonoTouch.CoreBluetooth.CBCentralManager)' (CS0533) (SNF_Binding)
How do I fix this:
Hello can you try this instead?
[Protocol] // Added Protocol attribute
[Model]
[BaseType (typeof(NSObject))] //Changed BaseType to NSObject
public partial interface LeDeviceManager : ICBCentralManagerDelegate
{
[Export ("devList", ArgumentSemantic.Retain)]
NSMutableArray DevList { get; set; }
[Export ("btmgr")]
CBCentralManager Btmgr { get; set; }
[Export ("delegate", ArgumentSemantic.Retain)]
LeDeviceManagerDelegate Delegate { get; set; }
[Export ("initWithSupportedDevices:delegate:")]
IntPtr Constructor (NSObject[] devCls, LeDeviceManagerDelegate del);
[Export ("startScan")]
void StartScan ();
[Export ("stopScan")]
void StopScan ();
[Export ("updatedState")] [New] // Notice I changed UpdatedState to updatedState lowercase u
void UpdatedState(CBCentralManager mgr);
}
I highly recommend reading Binding Protocols section of this doc
I'm attempting to provide a header and footer view from within an MvxCollectionViewController and I am having trouble. Normally, with a UICollectionViewController, I would override the GetViewForSupplementaryElement method like so:
public override UICollectionReusableView GetViewForSupplementaryElement (UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var someHeaderOrFooterView = (HeaderOrFooterView) collectionView.DequeueReusableSupplementaryView (elementKind, elementId, indexPath);
return someHeaderOrFooterView;
}
MvxCollectionViewControllers don't seem to get delegate callbacks to the GetViewForSupplementaryElement method like a UICollectionViewController does.
Is there another method for specifying the header and footer of a CollectionView using MvxCollectionViewController?
The standard steps should work (they work here...):
Provide a size for the header in the layout
HeaderReferenceSize = new System.Drawing.SizeF(100, 100),
Implement a class for the header
public class Header : UICollectionReusableView
{
UILabel label;
public string Text
{
get { return label.Text; }
set { label.Text = value; SetNeedsDisplay(); }
}
[Export("initWithFrame:")]
public Header(System.Drawing.RectangleF frame)
: base(frame)
{
label = new UILabel() { Frame = new System.Drawing.RectangleF(0, 0, 300, 50), BackgroundColor = UIColor.Yellow };
AddSubview(label);
}
}
register that class
CollectionView.RegisterClassForSupplementaryView(typeof(Header), UICollectionElementKindSection.Header, headerId);
Implement GetViewForSupplementaryElement
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var headerView = (Header)collectionView.DequeueReusableSupplementaryView(elementKind, headerId, indexPath);
headerView.Text = "Supplementary View";
return headerView;
}
Just tested these steps here and they work in my sample app (based on https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-11-KittenView_Collections).
Aside> There is an open issue to provide bindable supplementary views - https://github.com/slodge/MvvmCross/issues/339 - but this issue shouldn't effect the basic collection view operation.
I was having the same problem when using MvvmCross, MvxCollectionViewController never call to
public override UICollectionReusableView
GetViewForSupplementaryElement
and I resolved the problem using the answer of "Perfem_element".
public class MyCollectionViewSource: MvxCollectionViewSource
{
public MyCollectionViewSource(UICollectionView collectionView, NSString defaultCellIdentifier) : base(collectionView, defaultCellIdentifier)
{
}
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var headerView = (MyHeader)collectionView.DequeueReusableSupplementaryView(elementKind, MyHeader.Key, indexPath);
headerView.Text = "Supplementary View";
return headerView;
}
and finally in MvxViewController:
var source = new MyCollectionViewSource (CollectionView, ListadoCollectionCell.Key);
Thanks
I was having the same problem with MvvmCross. I was able to solve it by subclassing the MvxCollectionViewSource class and overriding the GetViewForSupplementaryElement method:
public class MyCollectionViewSource: MvxCollectionViewSource
{
public TimelineSource(UICollectionView collectionView, NSString defaultCellIdentifier) : base(collectionView, defaultCellIdentifier)
{
}
public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind, NSIndexPath indexPath)
{
var headerView = (MyHeader)collectionView.DequeueReusableSupplementaryView(elementKind, MyHeader.Key, indexPath);
headerView.Text = "Supplementary View";
return headerView;
}
}