Categories
discuss

Enforcing data integrity with OrmLite

I have a Question class that contains a list of Answer objects. In my POJOs I represent this in my Question class as:

@ForeignCollectionField 
private ForeignCollection<Answer> answers;

and in my Answer class this relationship is declared as:

@DatabaseField(foreign = true, canBeNull=false)
private Question question;

I had expected that ORMlite would throw an exception if I tried to persist an Answer which referenced a Question that had not yet been persisted to the database. However, this does not appear to happen. I can persist Answer objects as I please without saving the referenced Question objects. I verified the Answers are being saved with no questions being saved by looking in my database using SQLliteBrowser. This breaks my data integrity as when I restart my program the db now contains Answers that reference non existing questions.

Is there any way to enforce this? With my experience in Hibernate it will not allow you to save the Child object without first saving the referenced “Parent” object.

Answer

Add the following columnDefinition to your field and it will not allow the object to be created if the foreign ID it references does not exist.

@DatabaseField(foreign = true, canBeNull=false,
     columnDefinition = "integer references question(id) on delete cascade")
private Question question;

See also other thread: Creating foreign key constraints in ORMLite under SQLite

Categories
discuss

Loading non amd modules with require.js

Currently I am using require.js for a fun side project I am working everything is working fine except a code syntax higlighting plugin called prism.js. I can see that the plugin is being pulled via the network tab in chrome, but the plugin isn’t initializing.

I am not sure if it’s a require problem or uf the plugin is the issue and was wondering if anyone could help.

Here is a look at my main.js:

require.config({
  // 3rd party script alias names
  paths: {
    // Core Libraries
    modernizr: "libs/modernizr",
    jquery: "libs/jquery",
    underscore: "libs/lodash",
    backbone: "libs/backbone",
    handlebars: "libs/handlebars",

    text: "libs/text",
    prism: "plugins/prism",

    templates: "../templates"
  },
  // Sets the configuration for your third party scripts that are not AMD compatible
  shim: {
    "backbone": {
      "deps": ["underscore", "jquery", "handlebars"],
      "exports": "Backbone"  //attaches "Backbone" to the window object
    }
  }
});

// Include Specific JavaScript
require(['prism', 'modernizr', 'jquery', 'backbone', 'routers/router', 'views/AppVIew' ],
  function(Prism, Modernizr, $, Backbone, Router, App) {
    this.router = new Router();
    this.App = new App();
  }
);

Answer

Change the shim section to include prism, and make sure it exports “Prism”:

shim: {
  "backbone": {
      "deps": ["underscore", "jquery", "handlebars"],
      "exports": "Backbone"  //attaches "Backbone" to the window object
  },
  "prism": {
      "exports": "Prism"
  }
}
Categories
discuss

Creating an android smart app banner

Is there any solution for Android devices similar to the iOS 6 smart app banner?

Here is the code for smart app banner

<meta name="apple-itunes-app" content="app-id=311507490, affiliate-data=partnerId=30&siteID=k1CkFsOh4nQ"/>

Answer

Since Chrome 44 Beta you can push your app in Chrome for Android with a native app install banner on your website.

Please see the answer below.


Old answer

I needed that myself, so I created a jquery plugin to mimic a smart banner for Android and older iOS versions.

http://jasny.github.com/jquery.smartbanner/#android

Categories
discuss

Change Text Color on mouseover

I’m hoping to accomplish this using pure CSS and Javascript. I’m ok with PHP as well. I’m avoiding jquery because I’m trying to learn javascript a bit more and I’ve found that in some word-press sites jquery doesn’t always work the way I need it to. As far as I can tell I haven’t made any programmatic errors, but I must be missing something because it doesn’t seem to be working correctly. First I’ll give a link where the code can be found. http://jsfiddle.net/FFCFy/13/

Next I’ll give the actual code.

Javascript:

setInterval(function() {
    var x = document.getElementById("div1");
    var y = document.getElementById("div2");

    function stext() {
        x.style.color = 'red';
        y.style.color = 'black';
    }

    function htext() {
        x.style.color = 'black';
        y.style.color = 'red';
    }
}, 250);

html:

<html>
<body>
    <span id="div1" style="color:black;" onmouseover="stext"   onmouseout="htext">TEXT1</span><p />
    <hr color="black" />
<span id="div2" style="color:red;"onmouseover="htext" onmouseout="stext">Text2</span>

</body>
</html>

Eventually I’ll be modifying this to hide and show different text, but I’ll get to that once I have this figured out.

Answer

You don’t need the setInterval.

function stext() {
    var x = document.getElementById("div1");
    var y = document.getElementById("div2");
    x.style.color = 'red';
    y.style.color = 'black';
}

Updated Working JSFiddle

Categories
discuss

Java’s lack of template inheritance is causing major code duplication headaches in Android. Any solutions?

This is Android specific.

I derive all of my Activities in Android from a custom class that provides a nice, clean place to put common code used by all layouts in the application, especially some common setContentView() override code that injects layouts into my layouts. So here is what a typical hierarchy looks like:

MyActivity extends MyBaseClass -> MyBaseClass extends Activity -> Activity

For one of my apps, I needed Google Maps support. So I tried to turn MyBaseClass into a template/generic so that I could do something like:

MyActivity extends MyBaseClass<MapActivity> -> MyBaseClass<T> extends T -> T

Which, of course, failed to compile because Java templates/generics are not nearly as useful as C++ templates as I discovered shortly after the attempt. So I ended up creating a whole separate class as a temporary workaround:

MyActivity extends MyBaseMapClass -> MyBaseMapClass extends MapActivity -> MapActivity

And copied all the code from MyBaseClass into MyBaseMapClass. The code is identical except for a few minor changes involving imports. It works, but it is hardly a good solution.

I crossed my fingers that the above would be the end of the problem and I would never have to revisit it again.

Unfortunately, I’m starting to experiment with a fragment and activity mix with a much more complex base class and the problem of code duplication with the above is getting to be a serious issue. Any time I make a change in one file, I have to remember to clone it to the other files and development is moving fast enough that I’m quickly getting out of sync.

Activity, MapActivity, FragmentActivity, ListActivity, PreferenceActivity, etc. Do I need to make a separate derived class for each and every one of those that I wish to use? I hope not and, for this reason, I’ve already limited my derivations to begin with (some of my activities don’t have certain features as a result). The problem is further exasperated by the fact I sometimes use two separate base classes (where some activities need to inflate even more views into the layout but not all activities need to do so):

MyActivity extends MyBaseClass -> MyBaseClass extends Activity -> Activity

MyActivity2 extends AnotherBaseClass -> AnotherBaseClass extends MyBaseClass -> MyBaseClass extends Activity -> Activity

If I want to use the code for AnotherBaseClass in Maps, I not only have to create MyBaseMapClass, but also AnotherBaseMapClass and copy the code. I’m up to four classes at this point where two of the classes are a couple thousand lines of cloned code. Replicated code bothers me because it makes maintenance and development that much harder to do.

Deriving Android classes introduces complexities such as findViewById() being a part of the Activity base class. That’s important because I’m not sure how I would write an interface or composite without running into the same problems I just described.

Anyone here encountered this issue and come up with a workable solution?

Answer

Why don’t you just create a helper class with a static function like styleActivity(Activity a)? This seems to be the easier solution in that case than to use inheritance.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..