4

I'm currently building an accounting app, now I want to persist this "Period End Date" that is changeable. I believe we can use global variable, however I don't know how to do so. What I have tried so far is to create a variable in application.conf like this application.date="2014/12/15".

Moreover, I'm not sure how to change the value using above approach . Is there any good approach for this problem ?

Bla...
  • 7,228
  • 7
  • 27
  • 46
  • First and foremost, `application.conf` contains constants with a global app configuration and not variables. Could you explain your problem more briefly? – Daniel Olszewski Dec 11 '14 at 11:20
  • 1
    `application.conf` is not a good place for storing changeable values. It keeps your application configs, mostly hard coded values that can change before deployment. – Mon Calamari Dec 11 '14 at 11:20
  • Well, my goal is to find a way to persist that period end date without creating any table in my database. Can you suggest a way to persist that date ? I read some articles that suggest to create `object Global extends GlobalSettings`, I'm not sure how to access or change the value. – Bla... Dec 11 '14 at 12:49
  • If you don't want to use a database to persist a changeable value, your only other option is to using a file on the file system. You can store this in another file in the `resources` folder. For example, `resources\mydata` – Soumya Simanta Dec 11 '14 at 13:04

1 Answers1

5

A possible way to go is to use a Singeleton Object which is initialized in the Global.scala.

The Global Object has to go in the scala-root of the application or to be configured through application.conf.

Singleton for shared Data

in app/shared/Shared.scala(the name is free)

package shared

object Shared {
 private var data: Int = 0

 def setData(d: Int) : Unit = data = 0 
 def getData : Int = data
}

In application.conf you can set the Global to be called on start of the application (you could also just put a file called Global.scala in app, that would be used by default)

application.global= settings.Global
shared.initial = 42

In app/settings/Global.scala

object Global extends GlobalSettings {
  override def onStart(app: Application) {
   // Here I use typesafe config to get config data out of application conf
   val cfg: Config = ConfigFactory.load()
   val initialValue = cfg.getInt(shared.initial)
   // set initial value for shared
   Shared.setData(initialValue)
  }
}

In Play code to get or set the shared data.

import shared.Shared

Shared.getData
Shared.setData( 8 )
Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52
  • So, everytime I execute `play run` it will initialise `data` to 42 ? I prefer setting it through `application.conf`, but where that `settings.` variable comes from ? – Bla... Dec 12 '14 at 02:21
  • I tried your code above, however I got an error: `not found Shared value`, when I try to get it in controller. – Bla... Dec 12 '14 at 03:07
  • `settings`is the package name of the package I put the Global object into. – Andreas Neumann Dec 12 '14 at 09:36
  • To use the Shared object you have to import it into the controller: `import package_name_you_have_chosen.Shared` – Andreas Neumann Dec 12 '14 at 09:37
  • 1
    To read the data from a configuration use typesafe config. I will extend the example. – Andreas Neumann Dec 12 '14 at 09:39
  • Ah, I see.. Thanks, I'm still a newbie here.. hahaha.. There are a lot of answer for this issue but not as clear as yours. Hope you can elaborate more for future questioner. – Bla... Dec 12 '14 at 09:42
  • Hey, I have tried your code above without the global since I don't think it will give any help.. The problem is whenever I restart my `Play`, the value will change back to default value. Is there anyway to persist the data without using DB? – Bla... Dec 17 '14 at 05:07
  • Yes. You can write the data to disk. On startup you read from disk and in the on Shutdown you write to disk. – Andreas Neumann Dec 17 '14 at 10:45
  • Could you elaborate on how can I write on disk? – Bla... Dec 17 '14 at 13:02
  • You can do this like you would with any file. On initialization of the object you read the file (could be serialized, or JSON thats all up to you) to get the value on shutdown you write to the file. There are many ways to do this, depdending on you needs, so I can not give a general soultion. For wrting and reading files with Scala look for example at http://stackoverflow.com/questions/4604237/how-to-write-to-a-file-in-scala – Andreas Neumann Dec 17 '14 at 15:05