using late staic binding variable with another class - late-static-binding

I have created a class for writing sql query in which i have used late static binding concept and i am trying to call its insert method in different class to insert the values
here is sqlQuery class
class sqlQuery
{
public static $table=" table 1 ";
public static $colum1=" colum1 ";
public static $colum2=" colum2 ";
public static $colum3=" colum3 ";
public static $colum4=" colum4 ";
public $value1=" value1 ";
public $value2=" value2 ";
public function insert( $value1,$value2)
{
echo "INSERT INTO" .static::$table ."(" . static::$colum1 .' , ' .static ::$colum2. ") VALUES('$value1' , '$value2')" ;
}
}
And this is my second class file where i am using insert method from first class what i am trying to do, get the table name and Column from this class using late static binding....please help how can i do this...here is my 2nd class file
class gallery extends logo
{
public $object;
public static $colum1=" status ";
public static $colum2=" order ";
public static $colum3=" colum3 ";
public static $colum4=" colum4 ";
function __construct()
{
parent::__construct();
//$this->object=new sqlQuery();
}
function insert()
{
$query=new sqlQuery();
$query1=new sqlQuery();
$call=$query1->insert('active','10');
}
}
Help me thanks in advance.....

Late static binding is about parent - children relationships. It's unclear what you want to get. You should have some subclass of sqlQuery to use late static binding. Something like
class gallerySqlQuery extends sqlQuery
{
protected static $colum1=" status "; // better private or protected
protected static $colum2=" order "; // you don't need it to be public
protected static $colum3=" colum3 ";
protected static $colum4=" colum4 ";
}
in the gallery class you should call the method of this class
$query1=new gallerySqlQuery();
$call=$query1->insert('active','10');
Then, you don't need to declare all these public static $colum1 etc in the sqlQuery class.

Related

How to see arguments when creating a new class?

When creating a new class or method I used to be able to see the parameters needed. But, now they don't come up anymore. How do I view parameters when creating a class?
Running the latest windows version.
public class Main {
public static void main(String args[]) {
Case theCase = new Case("Default", "Corsair", "500W");
}
}
public class Case {
private String model;
private String manufacturer;
private String powerSupply;
public Case(String model, String manufacturer, String powerSupply,) {
this.model = model;
this.manufacturer = manufacturer;
this.powerSupply = powerSupply;
}
public void pressPowerButton() {
System.out.println("Power button pressed");
}
public String getModel() {
return model;
}
public String getManufacturer() {
return manufacturer;
}
public String getPowerSupply() {
return powerSupply;
}
}
When making theCase I can't see what my parameters are and have to move to the "Case" class back and forth
You can explicitly call Parameter Info action which is usually mapped to Ctrl/(Cmd) - p.
Nevermind in order to see the parameters as you type you must type them while in the editor without moving your cursor.

How to map object properties in an Orika custom mapper?

I tried to find an answer to this question in the Orika documentation but no luck.
I have the following classes:
public class A {
private String partNumber1;
private String partNumber2;
...
}
public class B {
private Integer shelfNumber;
private A a;
...
}
public class BDTO {
private Integer selfNumber;
private ADTO someA;
...
}
public class ADTO {
private String partNumber;
...
}
.. and the following CustomMapper's to map Objects of B to objects BDO
#Component
public class BMapper extends CustomMapper<B, BDTO> {
#Override
public void mapAtoB(B b, BDTO bdto, MappingContext context) {
super.mapAtoB(b, bdto, context);
//??? what to do here ???
}
}
#Component
public class AMapper extends CustomMapper<A, ADTO> {
#Override
public void mapAtoB(A a, ADTO adto, MappingContext context) {
super.mapAtoB(a, adto, context);
adto.setPartNumber(a.getPartNumber1() + a.getPartNumber2());
}
}
In my client code I have:
B b = new B(5, new A("100392", "100342"));
BDTO bdto = mapper.map(b, BDTO.class);
My question is, in BMapper, what is the correct way to get the AMapper to map "a" to "someA"? To put it differently, what is the correct way to map a to someA in BMapper? I suspect that it can be done through some interface in the MappingContext object.
I found an answer after some experimentation. To map property objects in the main objects mapper, i.e. the scenario explained above, one can use the protected "mapperFacade" member of CustomMapper.
So you can do something like this:
bdto.setSomeA(super.mapperFacade.map(b.getA(), ADTO.class));

Which design pattern should I use to output different content using one template?

I need to extend a piece of code that writes a paragraph using constant strings defined in an interface:
public class paragraphGenerator implements EnglishParaGraph(){
public StringBuffer outputParagraph = new StringBuffer();
public void generate(){
writeParagraph(PARA1);
//some long and complicated logic here
writeParagraph(PARA2);
//some long and complicated logic here
writeParagraph(PARA3);
}
public void writeParagraph(String content){
//manipulates the paragraph and puts it in stringbuffer
}
}
public interface EnglishParaGraph{
public static final String PARA1 = "Hello";
public static final String PARA2 = "Thank you";
public static final String PARA3 = "Goodbye";
}
Running generate() should write something like "Hello Thank you Goodbye".
Now I want to generate a French equivalent so the output looks something like "Bonjour Merci Salut".
According to Template Method Pattern can overwrite generate() in a subclass and change each writeParagraph's input argument, but that will repeat most of the code which is not desirable.
What's the most suitable design pattern to use here? I was told to use as little replicating code as possible.
The design of the interface and extending class is simply wrong. To be clear, interfaces are specifically used to avoid defining implementation details. The code you posted does the exact opposite of that and defines implementation details using an interface.
At worst, you want to make the interface define something like this:
public interface ParagraphSource{
public String getParagraph1Text();
public String getParagraph2Text();
public String getParagraph3Text();
}
public class EnglishSource extends ParagraphSource {
public String getParagraph1Text() {
return "Hello";
}
public String getParagraph2Text() {
return "Thank you";
}
public String getParagraph3Text() {
return "Goodbye";
}
}
public class FrenchSource extends ParagraphSource {
public String getParagraph1Text() {
return "Bonjour";
}
public String getParagraph2Text() {
return "Merci";
}
public String getParagraph3Text() {
return "Au revoir";
}
}
Then your paragraph generator can use different sources:
public class ParagraphGenerator {
public StringBuffer outputParagraph = new StringBuffer();
public void generate(ParagraphSource source){
writeParagraph(source.getParagraph1Text());
//some long and complicated logic here
writeParagraph(source.getParagraph2Text());
//some long and complicated logic here
writeParagraph(source.getParagraph3Text());
}
}

MEF Add module twice to catalog

Do you know how to add the same module twice to a catalog with different parameters?
ITest acc1 = new smalltest("a", 0)
ITest acc2 = new smalltest("b", 1)
AggregateCatalog.Catalogs.Add(??)
AggregateCatalog.Catalogs.Add(??)
Thanks in advance!
As MEF is limited to its usage of attributes and can be configured by using the Import and Export attributes unlike the flexibility usually provided by IoC Containers, just how one may extend a Part in MEF, one may extend it from a referenced DLL, you could also do something similar where a class inherits from a previous MEF Part by creating a class which exposes some properties with the [ExportAttribute]. The attribute is not limited to the usage on a class, but can be applied to properties. For example, how about something like this.
public class PartsToExport
{
[Export(typeof(ITest))]
public Implementation A
{
get { return new Implementation("A", 5); }
}
[Export(typeof(ITest))]
public Implementation B
{
get { return new Implementation("B", 10); }
}
}
public interface ITest
{
void WhoAmI(Action<string, int> action);
}
[Export]
public class Implementation : ITest
{
private string _method;
private readonly int _value;
public Implementation(string method, int value)
{
_method = method;
_value = value;
}
public void WhoAmI(Action<string, int> action)
{
action(_method, _value);
}
}
[TestClass]
public class Tests
{
[TestMethod]
public void Test()
{
var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
CompositionContainer container = new CompositionContainer(catalog);
var tests = container.GetExportedValues<ITest>();
foreach (var test in tests)
{
test.WhoAmI((s, i) => Console.WriteLine("I am {0} with a value of {1}.", s, i));
}
}
}
This outputs the following to the console:
I am A with a value of 5.
I am B with a value of 10.

How to persist an enum using NHibernate

Is there a way to persist an enum to the DB using NHibernate? That is have a table of both the code and the name of each value in the enum.
I want to keep the enum without an entity, but still have a foreign key (the int representation of the enum) from all other referencing entities to the enum's table.
Why are you guys over complicating this? It is really simple.
The mapping looks like this:
<property name="OrganizationType"></property>
The model property looks like this:
public virtual OrganizationTypes OrganizationType { get; set; }
The Enum looks like this:
public enum OrganizationTypes
{
NonProfit = 1,
ForProfit = 2
}
NHibernate will automatically figure it all out. Why type more than you need????
You can use the enum type directly: http://web.archive.org/web/20100225131716/http://graysmatter.codivation.com/post/Justice-Grays-NHibernate-War-Stories-Dont-Use-Int-If-You-Mean-Enum.aspx. If your underlying type is a string, it should use the string representation, if it is numeric, it will just use the numeric representation.
But your question wording sounds like you're looking for something different, not quite an enum. It seems that you want a lookup table without creating a separate entity class. I don't think this can be done without creating a separate entity class though.
An easy but not so beautiful solution:
Create an integer field with and set the mapping in the mapping file to the field.
Create a public property that uses the integer field.
private int myField;
public virtual MyEnum MyProperty
{
get { return (MyEnum)myField; }
set { myField = value; }
}
I am using NHibernate 3.2, and this works great:
type="NHibernate.Type.EnumStringType`1[[enum_full_type_name, enum_assembly]], NHibernate"
Not sure when the generic EnumStringType got added, though.
Try using a stategy pattern. Uou can then put logic into your inner classes. I use this quite alot espically when there is logic that should be contained in the "enum". For example the code below has the abstract IsReadyForSubmission() which is then implemented in each of the nested subclasses (only one shown). HTH
[Serializable]
public abstract partial class TimesheetStatus : IHasIdentity<int>
{
public static readonly TimesheetStatus NotEntered = new NotEnteredTimesheetStatus();
public static readonly TimesheetStatus Draft = new DraftTimesheetStatus();
public static readonly TimesheetStatus Submitted = new SubmittedTimesheetStatus();
//etc
public abstract int Id { get; protected set; }
public abstract string Description { get; protected set; }
public abstract bool IsReadyForSubmission();
protected class NotEnteredTimesheetStatus: TimesheetStatus
{
private const string DESCRIPTION = "NotEntered";
private const int ID = 0;
public override int Id
{
get { return ID; }
protected set { if (value != ID)throw new InvalidOperationException("ID for NotEnteredTimesheetStatus must be " + ID); }
}
public override string Description
{
get { return DESCRIPTION; }
protected set { if (value != DESCRIPTION)throw new InvalidOperationException("The description for NotEnteredTimesheetStatus must be " + DESCRIPTION); }
}
public override bool IsReadyForSubmission()
{
return false;
}
}
//etc
}