20210112のJavaに関する記事は7件です。

Notes文書をJavaでXML文書として出力する

Notesデータベースの文書をJSON形式でやりとりできるNotesJSON****という関数があると聞いて試そうと思ったのですが……。Notes10から利用できるクラスということで、自分のNotes環境では検証できませんでした。

ただ、ヘルプなどを色々と調べているときに、generateXMLというメソッドを利用すれば、Notes文書をXML形式で出力できることが分かりました。便利そうな関数なのですが、Javaのみに用意されている関数になるため、LotusScriptは使用できません。

generateXMLクラスの使い勝手

実際に使ってみたところ、驚愕の機能です。
generateXMLクラスは文書の全てのフィールド情報と値を構造的に書き出してくれるのですが、リスト形式の複数値やリッチテキストのフィールド情報まで正確に書き出してくれます。

LotusScriptで書くと、文書を取得した後、ForAll item In doc.Items などで文書内のフィールドを取得して、フィールドごとに複数値を取得するコードなど、色々と面倒なコードをチマチマと書いたりしないといけないのですが、generateXMLクラスで書くと1行で済んでしまう………。ヘルプには少しだけしか記述が割かれていませんが、もっと知られないといけない機能だと思います。

ソースコード

import lotus.domino.*;
import java.io.*;

public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();

          // (Your code goes here)
          Database db = agentContext.getCurrentDatabase();
          View view = db.getView("Main");
          view.setAutoUpdate(false);
          Document tmpdoc;
          BufferedWriter bw = new BufferedWriter( new FileWriter("c:\\temp\\document.xml"));
          Document doc = view.getFirstDocument();
          while (doc != null) {
              System.out.println(doc.getItemValueString("Uname"));
              doc.generateXML(bw);
              tmpdoc = view.getNextDocument(doc);
              doc.recycle();
              doc = tmpdoc;
          }
          bw.close();
      } catch(Exception e) {
          e.printStackTrace();
       }
   }
}

実行結果

リッチテキストやリストなどの複数の種類が混在したテストフォームを作成して、複数文書がXML形式で出力されることを確認しました

テスト用のフォームと文書

テキスト/リッチテキスト/時刻/チェックボックスなど、様々な種類のフィールドを配置したテストフォームを用意して、複数のNotes文書を作成しました。
サンプル.png

実行結果

<document form='Main'>
<noteinfo noteid='912' unid='C7638A880050D1014925865B001F3A1B' sequence='1'>
<created><datetime>20210112T144104,91+09</datetime></created>
<modified><datetime>20210112T144104,92+09</datetime></modified>
<revised><datetime>20210112T144104,91+09</datetime></revised>
<lastaccessed><datetime>20210112T144104,92+09</datetime></lastaccessed>
<addedtofile><datetime>20210112T144104,92+09</datetime></addedtofile></noteinfo>
<updatedby><name>CN=Taro Yamada/O=Taro Yamada</name></updatedby>
<item name='Uname'><text>山田一郎</text></item>
<item name='UDate'><datetime>20210201</datetime></item>
<item name='UTimeS'><datetime>T091500,00</datetime></item>
<item name='UTimeE'><datetime>T180000,00</datetime></item>
<item name='Rtxt'><richtext>
<pardef id='1'/>
<par def='1'><run><font size='18pt' style='bold' name='メイリオ' pitch='variable'
 truetype='true' familyid='30' color='red'/>リッチテキスト</run></par>
<par def='1'><run><font size='18pt' style='bold' name='メイリオ' pitch='variable'
 truetype='true' familyid='30' color='red'/></run></par></richtext></item>
<item name='CheckBox'><textlist><text>埼玉</text><text>東京</text><text>茨木</text></textlist></item>
<item name='Num'><number>12345678.9</number></item></document>

<document form='Main'>
<noteinfo noteid='90e' unid='4358C36AE3645EB44925865B001F2BF9' sequence='1'>
<created><datetime>20210112T144028,73+09</datetime></created>
<modified><datetime>20210112T144028,75+09</datetime></modified>
<revised><datetime>20210112T144028,74+09</datetime></revised>
<lastaccessed><datetime>20210112T144028,75+09</datetime></lastaccessed>
<addedtofile><datetime>20210112T144028,75+09</datetime></addedtofile></noteinfo>
<updatedby><name>CN=Taro Yamada/O=Taro Yamada</name></updatedby>
<item name='Uname'><text>山田三郎</text></item>
<item name='UDate'><datetime>20210112T000000,00+09</datetime></item>
<item name='UTimeS'><datetime>T090000,00</datetime></item>
<item name='UTimeE'><datetime>T180000,00</datetime></item>
<item name='Rtxt'><richtext>
<pardef id='1' keepwithnext='true' keeptogether='true'/>
<par def='1'><run><font name='メイリオ' pitch='variable' truetype='true' familyid='30'/></run></par></richtext></item>
<item name='CheckBox'><text>東京</text></item>
<item name='Num'><number>11</number></item></document>

<document form='Main'>
<noteinfo noteid='906' unid='A871F8B1625F2B0E4925865B001EFFCF' sequence='1'>
<created><datetime>20210112T143835,67+09</datetime></created>
<modified><datetime>20210112T143835,68+09</datetime></modified>
<revised><datetime>20210112T143835,67+09</datetime></revised>
<lastaccessed><datetime>20210112T143835,68+09</datetime></lastaccessed>
<addedtofile><datetime>20210112T143835,68+09</datetime></addedtofile></noteinfo>
<updatedby><name>CN=Taro Yamada/O=Taro Yamada</name></updatedby>
<item name='Uname'><text>山田二郎</text></item>
<item name='UDate'><datetime>20210103T000000,00+09</datetime></item>
<item name='UTimeS'><datetime>T090000,00</datetime></item>
<item name='UTimeE'><datetime>T180000,00</datetime></item>
<item name='Rtxt'><richtext>
<pardef id='1' keepwithnext='true' keeptogether='true'/>
<par def='1'><run><font name='メイリオ' pitch='variable' truetype='true' familyid='30'/>ああああ</run></par></richtext></item>
<item name='CheckBox'><text/></item>
<item name='Num'><text/></item></document>
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Jersey - Which Architecture Layer is Suitable for Filter?

In DDD there are many architectures. Some of them are said to be layered, some are not. In any case, there is a layer (or slice or component or whatever you want to call it) that contains only domain code; that domain layer has not dependency to other layers, contains only pure business logic. The first client of that layer is called Application layer that contains Application services that have , among other, the authorization responsibility; it checks if a user is permitted to execute some command (modify the state of the system) or query (view some data). Although it may seem that this authorization contain business logic, this logic is different from the core business logic.

So, about your example, the domain code should not check by itself if a user has access to some computers. The repository should expose a filter that the Application services could use to filter the computers the user has access to. It is OK to put this functionality inside the repository. In fact, in my opinion, the repository does not belong in the domain layer, not even the repository interfaces.

In CQRS, the command side of the domain layer does not contain any trace of repository/infrastructure code/interfaces, only pure domain logic. The application service authorize the user, loads the aggregate then call a method on it (here stays the domain code) then persist the aggregate.

On the query side there may be infrastructure code involved in the read-model, in order to keep even the domain layer thin. Here reside the filters that can be used by the application services to filter the computers the user has access to, but those filters are no pre-applied by the domain code, they are just exposed to application services use.

So, the conclusion, is to not mix the authorization code with the domain/business logic code.

https://stackoverflow.com/questions/43484497/authorization-filter-in-layered-architecture

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Jersey - Which DDD Architecture Layer is Suitable for Filter?

In DDD there are many architectures. Some of them are said to be layered, some are not. In any case, there is a layer (or slice or component or whatever you want to call it) that contains only domain code; that domain layer has not dependency to other layers, contains only pure business logic. The first client of that layer is called Application layer that contains Application services that have , among other, the authorization responsibility; it checks if a user is permitted to execute some command (modify the state of the system) or query (view some data). Although it may seem that this authorization contain business logic, this logic is different from the core business logic.

So, about your example, the domain code should not check by itself if a user has access to some computers. The repository should expose a filter that the Application services could use to filter the computers the user has access to. It is OK to put this functionality inside the repository. In fact, in my opinion, the repository does not belong in the domain layer, not even the repository interfaces.

In CQRS, the command side of the domain layer does not contain any trace of repository/infrastructure code/interfaces, only pure domain logic. The application service authorize the user, loads the aggregate then call a method on it (here stays the domain code) then persist the aggregate.

On the query side there may be infrastructure code involved in the read-model, in order to keep even the domain layer thin. Here reside the filters that can be used by the application services to filter the computers the user has access to, but those filters are no pre-applied by the domain code, they are just exposed to application services use.

So, the conclusion, is to not mix the authorization code with the domain/business logic code.

https://stackoverflow.com/questions/43484497/authorization-filter-in-layered-architecture

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Java-強制型変換

Java-強制型変換

int型同士の計算

int number1 = 13;
int number2 = 4;

System.out.println(number1/number2);  //int型同士の割り算

//コンソール
3

int型同士の計算「13 / 4」は3になってしまう
int型の変数同士の割り算で、正確な値を出したいときはどうすれば??

キャスト

int number1 = 13;
int number2 = 4;

System.out.println((double)number1/number2);

//コンソール
3.25  //double型の計算結果

このようなときは強制的に型変換を行う
これをキャストと呼び、(変換したいデータ型)値とする

int型同士の値から、最終的にdouble型の計算結果を得たい場合
どちらか1つをキャストする(もう一方はJavaが判断してくれる)

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Java-自動型変換

Java-自動型変換

型変換について

System.out.println("内藤さんは"+26+"歳です")

//コンソール
内藤さんは26歳です

「+」などの操作は同じデータ型同士でないといけない
型の違うものを演算するときは、型を変換し同じ型にする

Javaではこの型変換方法として自動の変換と手動の変換がある
String型とint型を足すと
int型が自動でString型に変換され文字列の結合が行われる

数値の計算の注意点

System.out.println(5/2);  //int型同士の計算
System.out.println(5.0/2.0);  //double型同士の計算

//コンソール
2  int型
2.5  double型

数値計算における注意点
int型同士の計算は結果もint型になり
double型同士の計算は、結果もdouble型になる

int型の5をint型の2で割ると
結果は「2.5」ではなく「2」となることに注意

計算時の自動型変換

System.out.println(5.0/2);
                   5.0/2.0  //double型に変換

//コンソール
2.5  double型

int型とdouble型の計算では結果はdouble型になる
計算の過程でint型がdouble型に変換されるため
よって「5 / 2.0」もしくは「5.0 / 2」の結果は2.5

5を5.0にするように、整数は必ず小数でも表せるため
Javaが自動で型変換を行う

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Android Studioインストール後にコマンドラインでビルドしようとするとJDKのインストールを求められる問題の対処法

macでAndroid Studioの環境構築をした際に若干詰まった部分をメモしておきます。

問題

macにAndroid Studioをインストールしたあと、Android Studio外で以下のようなコマンドによりビルドしようとした際に、以下のエラーメッセージとスクショのようなダイアログが表示され、JDKのインストールを求められました。

ビルドコマンドとエラーメッセージ
$ ./gradlew assembleDebug
No Java runtime present, requesting install.

JDKをインストールを求めるダイアログ
スクリーンショット 2021-01-12 15.23.01.png

環境

  • macOS Catalina 10.15.17
    • Intel CPU
  • Android Studio 4.1.1

解決策

Android StudioにはすでにJDKを内包しているため、パスを通してやれば良いです。

具体的なパスは、Android Studioのメニュー > File > Project Structureの画面で、左ペインのSDK Locationを選択し、JDK Locationを見ることで確認できます。
JDKパス確認画面.png

ちなみに、ここの項目のヒントに、「外部プロセスで利用したい場合はJAVA_HOMEにこのパスを追加する」旨が記載されていました。
JDKパスヒント.png

確認したパスを~/.zprofileファイルに以下のように書き込んだのちターミナルを再起動すると、ビルドコマンドが利用できるようになります。

.zprofile
export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"

参照

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Androidの非同期処理「AsyncTask」の基本の基

Androidの非同期処理「AsyncTask」について

AsyncTaskをなんとなく利用しておりましたので、改めて基礎をまとめてみました。

※主に自身の毎日の復習・学習の機会創出、アウトプットによる知識の定着を目的としております。
暖かい目で見ていただけますと幸いです。

そもそも非同期処理とは

同期処理: あるタスクを順番に実行すること。
非同期処理:あるタスクが実行をしている際に、他のタスクが別の処理を実行すること。

なぜ非同期処理させる必要があるのか

仮に、もし非同期処理をしなかった場合を考えてみます。
AndroidやSwift、webアプリなどででユーザーが操作している際、例えば何か情報(動画一覧・検索結果一覧など)を取得する際、同期処理だと取得の処理が終わるまではUIなどの他の処理が一切できず画面は止まったままになります。
そうなると、ユーザーからしたらアプリが止まった(バグの)ように感じてしまいます。

非同期処理で実装するとUI・画面を良い感じに見せながら、並行して情報取得(動画一覧・検索結果一覧など)の処理を実施することができます。そうすることによって、ユーザーには今情報を取得中であることを提示でき、誤解を与えずにすみます

よく見ると、YoutubeやSNSなどもロード中は画面上に動くローディング画面が表示される場面がよく見られます。

AsyncTaskとは

Androidの非同期処理にはHandlerやThreadHandlerクラスを使うことができますが、これらのHelper ClassとしてAsyncTaskがあります。

AsyncTaskは他の方法より簡単に非同期を扱うことができますます。

AsyncTaskの主なメソッド

onPreExecute()

doInBackgroundメソッドの実行前にメインスレッドで実行されます。
非同期処理前に何か処理を行いたい時などに利用。

doInBackground()

メインスレッドとは別のスレッドで実行されます。
非同期で処理したい内容を記述します。
※唯一実装が必須

onProgressUpdate()

メインスレッドで実行されます。
途中経過をメインスレッドに返します。
非同期処理の進行状況をプログレスバーで 表示したい時などに使うことができます。

onPostExecute()

doInBackgroundメソッドの実行後にメインスレッドで実行されます。
結果をメインスレッドに返します。
doInBackgroundメソッドの戻り値をこのメソッドの引数として受け取り、その結果を画面に反映させることができます。

※実行順番
1.onPreExecute()
2.doInBackground()
3.onProgressUpdate() ※doInBackground()で、publishProgress()が呼ばれた場合に処理。
4.onPostExecute()

利用シーン

STEP1:AsyncTaskを継承するクラスを作成します

class asyncTask extends android.os.AsyncTask{
    @Override
    protected Object doInBackground(Object... obj){

   }

STEP2: 各メソッド内容を入力(doInBackgroundは必須)

class asyncTask extends android.os.AsyncTask{
    //※この引数はObject... ですが通常通り、型・個数を指定することもできます
    @Override
    protected Object doInBackground(Object... obj){
      //ここに処理を記入
      System.out.plintln("非同期処理の内容をここに記載します")
      //引数を取得することもできます
      String message = (String)obj[0];

   }

STEP3: メインスレッドで、STEP1,2で作成したクラスをインスタンス化し、executeメソッドで呼び出す

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // タスクの生成
        mAsyncTask = new asyncTask(textView);
        //タスクの実行
        mAsyncTask.execute("ここにasyncTaskに渡したい引数");
    }
}

利用時の注意点

doInBackgroundで直接メインスレッドに対しての処理を行うと例外が発生します。

まとめ

・AsyncTaskは非同期処理に利用できる
・AsyncTaskは他の方法より簡易に非同期処理を利用できる
・AsyncTaskを利用の際は、doInBackgroundは必須

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む